From 5e5e93e0f8ed112ddbdf14f2ec1205c466b3ab03 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 12 Jan 2020 00:03:19 +0800 Subject: [PATCH] feat(core): core implementation of logger --- packages/koishi-core/src/context.ts | 32 +++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/koishi-core/src/context.ts b/packages/koishi-core/src/context.ts index 059e285be0..36f49f37ee 100644 --- a/packages/koishi-core/src/context.ts +++ b/packages/koishi-core/src/context.ts @@ -1,4 +1,4 @@ -import { contain, union, intersection, difference, noop } from 'koishi-utils' +import { contain, union, intersection, difference } from 'koishi-utils' import { Command, CommandConfig, ParsedCommandLine } from './command' import { MessageMeta, Meta, contextTypes } from './meta' import { EventEmitter } from 'events' @@ -44,16 +44,35 @@ export namespace ContextScope { const noopScope: ContextScope = [[[], null], [[], null], [[], null]] const noopIdentifier = ContextScope.stringify(noopScope) +export interface Logger { + warn: (format: any, ...param: any) => void + info: (format: any, ...param: any) => void + debug: (format: any, ...param: any) => void + success: (format: any, ...param: any) => void + error: (format: any, ...param: any) => void +} + +export const logTypes: (keyof Logger)[] = ['warn', 'info', 'debug', 'success', 'error'] + export class Context { public app: App public sender: Sender public database: Database + public logger: (scope?: string) => Logger public receiver: Receiver = new EventEmitter() constructor (public readonly identifier: string, private readonly _scope: ContextScope) { - // prevent event emitter from crashing - // https://nodejs.org/api/events.html#events_error_events - this.receiver.on('error', noop) + this.receiver.on('error', (error) => { + this.logger().warn(error) + }) + + this.logger = (scope = '') => { + const logger = {} as Logger + for (const type of logTypes) { + logger[type] = (...args) => this.app.receiver.emit(`logger/${type}` as any, scope, format(...args)) + } + return logger + } } inverse () { @@ -263,6 +282,11 @@ export interface EventMap { 'error' (error: Error): any 'error/command' (error: Error): any 'error/middleware' (error: Error): any + 'logger/debug' (scope: string, message: string): any + 'logger/info' (scope: string, message: string): any + 'logger/error' (scope: string, message: string): any + 'logger/warn' (scope: string, message: string): any + 'logger/success' (scope: string, message: string): any 'ready' (): any 'before-connect' (): any 'connect' (): any