Skip to content

Commit

Permalink
utils: enum logger levels
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 25, 2020
1 parent 6047061 commit 398abe5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/tests/hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from 'chai'

const app = new App()

Logger.baseLevel = 1
Logger.baseLevel = Logger.ERROR
const appLogger = new Logger('app')
const appWarn = spyOn(appLogger, 'warn')
const midLogger = new Logger('middleware')
Expand Down
16 changes: 11 additions & 5 deletions packages/koishi-utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const instances: Record<string, Logger> = {}
type LogFunction = (format: any, ...param: any[]) => void

export class Logger {
static readonly SUCCESS = 1
static readonly ERROR = 1
static readonly INFO = 2
static readonly WARN = 2
static readonly DEBUG = 3

static baseLevel = 2
static showDiff = false
static levels: Record<string, number> = {}
Expand Down Expand Up @@ -54,11 +60,11 @@ export class Logger {
instances[name] = this
this.code = colors[Math.abs(hash) % colors.length]
this.displayName = name ? this.color(name + ' ', ';1') : ''
this.createMethod('success', '[S] ', 1)
this.createMethod('error', '[E] ', 1)
this.createMethod('info', '[I] ', 2)
this.createMethod('warn', '[W] ', 2)
this.createMethod('debug', '[D] ', 3)
this.createMethod('success', '[S] ', Logger.SUCCESS)
this.createMethod('error', '[E] ', Logger.ERROR)
this.createMethod('info', '[I] ', Logger.INFO)
this.createMethod('warn', '[W] ', Logger.WARN)
this.createMethod('debug', '[D] ', Logger.DEBUG)
}

private color(value: any, decoration = '') {
Expand Down

0 comments on commit 398abe5

Please sign in to comment.