Skip to content

Commit

Permalink
feat(loader): support logger.prolog
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 27, 2023
1 parent 1303744 commit 8b44675
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"dependencies": {
"@koishijs/core": "4.13.5",
"@koishijs/loader": "3.3.0",
"@koishijs/loader": "4.0.0",
"@koishijs/utils": "^7.0.2",
"@satorijs/satori": "^2.4.0",
"cac": "^6.7.14",
Expand Down
11 changes: 1 addition & 10 deletions packages/koishi/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ process.on('unhandledRejection', (error) => {
new Logger('app').warn(error)
})

namespace addons {
export const name = 'CLI'

export function apply(ctx: Context, config: Context.Config) {
logger.apply(ctx.root)
ctx.plugin(daemon, config.daemon)
}
}

async function start() {
const loader = new Loader()
await loader.init(process.env.KOISHI_CONFIG_FILE)
Expand All @@ -57,7 +48,7 @@ async function start() {
}

const app = await loader.createApp()
app.plugin(addons, app.config)
app.plugin(daemon, config.daemon)
await app.start()
}

Expand Down
21 changes: 1 addition & 20 deletions packages/koishi/src/worker/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, defineProperty, Logger, remove, Schema, version } from '@koishijs/core'
import { Context, defineProperty, Logger, Schema } from '@koishijs/core'

interface LogLevelConfig {
// a little different from @koishijs/utils
Expand Down Expand Up @@ -27,15 +27,6 @@ Context.Config.list.push(Schema.object({
logger: Config,
}))

const prolog: Logger.Record[] = []

const target: Logger.Target = {
colors: 3,
record(record) {
prolog.push(record)
},
}

export function prepare(config: Config = {}) {
const { levels } = config
// configurate logger levels
Expand Down Expand Up @@ -71,15 +62,5 @@ export function prepare(config: Config = {}) {
}
}

Logger.targets.push(target)

new Logger('app').info('%C', `Koishi/${version}`)
Logger.timestamp = Date.now()
}

export function apply(app: Context) {
app.loader.prolog = prolog
app.on('ready', () => {
remove(Logger.targets, target)
})
}
2 changes: 1 addition & 1 deletion packages/loader/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/loader",
"description": "Config Loader for Koishi",
"version": "3.3.0",
"version": "4.0.0",
"main": "lib/index.js",
"module": "lib/shared.mjs",
"typings": "lib/index.d.ts",
Expand Down
15 changes: 13 additions & 2 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, ForkScope, interpolate, isNullable, Logger, Plugin, resolveConfig, valueMap } from '@koishijs/core'
import { Context, Dict, ForkScope, interpolate, isNullable, Logger, Plugin, resolveConfig, valueMap, version } from '@koishijs/core'
import { constants, promises as fs } from 'fs'
import * as yaml from 'js-yaml'
import path from 'path'
Expand Down Expand Up @@ -124,13 +124,24 @@ export abstract class Loader {
public filename: string
public envFiles: string[]
public cache: Dict<string> = Object.create(null)
public prolog?: Logger.Record[]
public prolog: Logger.Record[] = []

private store = new WeakMap<Plugin, string>()

abstract import(name: string): Promise<any>
abstract fullReload(code?: number): void

constructor() {
Logger.targets.push({
colors: 3,
record: (record) => {
this.prolog.push(record)
this.prolog = this.prolog.slice(-1000)
},
})
new Logger('app').info('%C', `Koishi/${version}`)
}

async init(filename?: string) {
if (filename) {
filename = path.resolve(this.baseDir, filename)
Expand Down

0 comments on commit 8b44675

Please sign in to comment.