Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
feat: add onDebug overload
Browse files Browse the repository at this point in the history
This support the most common usage.
  • Loading branch information
unional committed Feb 20, 2018
1 parent 54ed4c6 commit b1c33b2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ log.onWarn(...)
log.onInfo(...)
log.onDebug(...)

// or
log.onError(() => getLogMessageThatIsTimeConsumingToCreate())

function getLogMessageThatIsTimeConsumingToCreate() {
...
}
Expand Down
7 changes: 5 additions & 2 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export class LoggerImpl implements ALogger {
}

private on(logLevel, logFunction) {
if (this.level >= logLevel)
logFunction(this.logger[logLevelNameMap[logLevel]].bind(this.logger))
if (this.level >= logLevel) {
const logFn = this.logger[logLevelNameMap[logLevel]].bind(this.logger)
const result = logFunction(logFn)
if (result) logFn(result)
}
}
}
15 changes: 15 additions & 0 deletions src/getLogger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,18 @@ test('on???() logs to corresponding log', t => {
'error'
])
})

test(`pass '() => string' into on???() will log the result`, t => {
const log = getLogger('onXXX with () => string')
log.setLevel(logLevel.debug)
log.onDebug(() => 'debug')
log.onInfo(() => 'info')
log.onWarn(() => 'warn')
log.onError(() => 'error')
t.deepEqual(appender.logs.map(l => l.messages[0]), [
'debug',
'info',
'warn',
'error'
])
})
8 changes: 4 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface Logger extends ALogger {
error(...args: any[]): void
setLevel(level: number): void

onError(logFunction: (log: logMethod) => void): void
onWarn(logFunction: (log: logMethod) => void): void
onInfo(logFunction: (log: logMethod) => void): void
onDebug(logFunction: (log: logMethod) => void): void
onError(logFunction: ((log: logMethod) => void) | (() => string)): void
onWarn(logFunction: ((log: logMethod) => void) | (() => string)): void
onInfo(logFunction: ((log: logMethod) => void) | (() => string)): void
onDebug(logFunction: ((log: logMethod) => void) | (() => string)): void
}

0 comments on commit b1c33b2

Please sign in to comment.