Skip to content

Commit

Permalink
[pinpoint-apm#117] build from a string log level
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Oct 19, 2022
1 parent 733f7b5 commit b7ace81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/utils/log/logger2.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Logger {
return Logger.Builder(LogType.noneConfig)
}

static makeBuilder(name, adaptor) {
return new (Logger.Builder(LogType.valueOf(name)))(adaptor)
}

debug(message) {
this.adaptor.debug(message)
}
Expand Down Expand Up @@ -95,6 +99,18 @@ class LogType {
return new LogType('NONE')
}

static valueOf(name) {
if (name.toUpperCase) {
name = name.toUpperCase()
}
const type = [this.debug, this.info, this.warn, this.error].find(element => element.name === name)
if (!type) {
return this.noneConfig
}

return type
}

constructor(name) {
this.name = name
}
Expand Down
6 changes: 6 additions & 0 deletions test/utils/log/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ test('Logger.Builder', (t) => {
actual = new Logger.NoneBuilder(expectedAdaptor).build()
t.equal(actual.type.name, 'NONE', 'none name match')

t.end()
})

test('Logger by a log level', (t) => {
let actual = Logger.makeBuilder('debug', {}).build()
t.equal(actual.type.name, 'DEBUG')
t.end()
})

0 comments on commit b7ace81

Please sign in to comment.