Skip to content

Commit

Permalink
feat(core): refactor debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 18, 2020
1 parent 169089e commit 068135e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
15 changes: 7 additions & 8 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ export class App extends Context {
this.receiver.on('before-group', Command.attachGroupFields)
this.middleware(this._preprocess)

this.receiver.on('logger', (scope, message) => {
debug('koishi:' + scope)(message)
})
// apply default logger
this.receiver.on('logger', (scope, message) => debug(scope)(message))
}

get users () {
Expand Down Expand Up @@ -236,7 +235,7 @@ export class App extends Context {
}
await Promise.all(tasks)
this.status = Status.open
this.logger('app').debug('started')
this.logger('koishi:app').debug('started')
this.receiver.emit('connect')
if (this.selfId && !this._isReady) {
this.receiver.emit('ready')
Expand All @@ -261,7 +260,7 @@ export class App extends Context {
this.server.close()
}
this.status = Status.closed
this.logger('app').debug('stopped')
this.logger('koishi:app').debug('stopped')
this.receiver.emit('disconnect')
if (appList.every(app => app.status === Status.closed)) {
onStopHooks.forEach(hook => hook(...appList))
Expand All @@ -270,15 +269,15 @@ export class App extends Context {

emitEvent <K extends Events> (meta: Meta, event: K, ...payload: Parameters<EventMap[K]>) {
if (!meta.$ctxType) {
this.logger('receiver').debug('/', 'emits', event)
this.logger('koishi:receiver').debug('/', 'emits', event)
this.receiver.emit(event, ...payload)
return
}

for (const path in this._contexts) {
const context = this._contexts[path]
if (!context.match(meta)) continue
this.logger('receiver').debug(path, 'emits', event)
this.logger('koishi:receiver').debug(path, 'emits', event)
context.receiver.emit(event, ...payload)
}
}
Expand Down Expand Up @@ -434,7 +433,7 @@ export class App extends Context {
let index = 0
const next = async (fallback?: NextFunction) => {
if (!this._middlewareSet.has(counter)) {
return this.logger().warn(new Error(errors.ISOLATED_NEXT))
return this.logger('koishi').warn(new Error(errors.ISOLATED_NEXT))
}
if (fallback) middlewares.push((_, next) => fallback(next))
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class Command {
if (code) return this._sendHint(code, meta)

// execute command
this.context.logger('command').debug('execute %s', this.name)
this.context.logger('koishi:command').debug('execute %s', this.name)
this.app.emitEvent(meta, 'command', argv)

let skipped = false
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Context {

constructor (public readonly identifier: string, private readonly _scope: ContextScope) {
this.receiver.on('error', (error) => {
this.logger().warn(error)
this.logger('koishi').warn(error)
})

this.logger = (scope = '') => {
Expand Down Expand Up @@ -145,7 +145,7 @@ export class Context {
middleware (middleware: Middleware) {
const { maxMiddlewares } = this.app.options
if (this.app._middlewares.length >= maxMiddlewares) {
this.logger().warn(new Error(format(errors.MAX_MIDDLEWARES, maxMiddlewares)))
this.logger('koishi').warn(new Error(format(errors.MAX_MIDDLEWARES, maxMiddlewares)))
} else {
this.app._middlewares.push([this, middleware])
}
Expand All @@ -159,7 +159,7 @@ export class Context {
prependMiddleware (middleware: Middleware) {
const { maxMiddlewares } = this.app.options
if (this.app._middlewares.length >= maxMiddlewares) {
this.logger().warn(new Error(format(errors.MAX_MIDDLEWARES, maxMiddlewares)))
this.logger('koishi').warn(new Error(format(errors.MAX_MIDDLEWARES, maxMiddlewares)))
} else {
this.app._middlewares.unshift([this, middleware])
}
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export class Sender {
}

async get <T = any> (action: string, params: Record<string, any> = {}, silent = false): Promise<T> {
this.app.logger('sender').debug('request %s %o', action, params)
this.app.logger('koishi:sender').debug('request %s %o', action, params)
const response = await this._get(action, snakeCase(params))
this.app.logger('sender').debug('response %o', response)
this.app.logger('koishi:sender').debug('response %o', response)
const { data, retcode } = response
if (retcode === 0 && !silent) {
return camelCase(data)
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export abstract class Server {
}

protected debug (format: any, ...params: any[]) {
this.app?.logger('sender').debug(format, ...params)
this.app?.logger('koishi:sender').debug(format, ...params)
}

protected prepareMeta (data: any) {
Expand Down

0 comments on commit 068135e

Please sign in to comment.