Skip to content

Commit

Permalink
[pinpoint-apm#117] loglevel default logger integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Sep 12, 2023
1 parent 7f53e64 commit 32131b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
53 changes: 35 additions & 18 deletions lib/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,7 @@

const Logger = require('./utils/log/logger2')
const { getConfig, needsLoadConfig } = require('./config')

const noneConfigurationLog = new Logger.NoneBuilder({
output: console,
debug: function (message) {
this.output.debug(message)
},
info: function (message) {
this.output.info(message)
},
warn: function (message) {
this.output.warn(message)
},
error: function (message) {
this.output.error(message)
}
}).build()
const defaultLog = require('loglevel').getLogger('default_logger')

let log = undefined
module.exports = {
Expand All @@ -33,13 +18,45 @@ module.exports = {
}

if (needsLoadConfig()) {
return noneConfigurationLog
return new Logger.NoneBuilder({
output: console,
debug: function (message) {
this.output.debug(message)
},
info: function (message) {
this.output.info(message)
},
warn: function (message) {
this.output.warn(message)
},
error: function (message) {
this.output.error(message)
}
}).build()
}

const config = getConfig()
if (!log) {
log = Logger.makeBuilder(config.logLevel, {}).build()
defaultLog.setLevel(Logger.logTypeNameOf(config.logLevel))
log = Logger.makeBuilder(config.logLevel, {
log: defaultLog,
debug: function (message) {
this.log.debug(message)
},
info: function (message) {
this.log.info(message)
},
warn: function (message) {
this.log.warn(message)
},
error: function (message) {
this.log.error(message)
}
}).build()
}
return log
},
clearLog: function () {
log = undefined
}
}
8 changes: 6 additions & 2 deletions lib/utils/log/logger2.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ class Logger {
return Logger.Builder(LogType.noneConfig)
}

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

static logTypeNameOf(logType) {
return LogType.valueOf(logType).name
}

debug(message) {
Expand Down
16 changes: 9 additions & 7 deletions test/utils/log/logger.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'
const test = require('tape')
const { getLog } = require('../../../lib/supports')
const { getLog, clearLog } = require('../../../lib/supports')
const Agent = require('../../../lib/agent')
const { clear, getConfig } = require('../../../lib/config')

Expand Down Expand Up @@ -42,13 +42,15 @@ test('no config logger', (t) => {
t.end()
})

test('config loading logger', (t) => {
test('logger full cycle', (t) => {
clear()
clearLog()
let actual = getLog()
t.true(actual.adaptor.output.output === console, 'when no configuration loaded, logs use console output')
getConfig()
const actual = getLog()
actual.debug('a debug message')
actual.info('a info message')
actual.warn('a warn message')
actual.error('a error message')

actual = getLog()
t.equal(actual.adaptor.output.log.constructor.name, 'Logger', 'default logger use LogLevel')
t.equal(actual.adaptor.output.log.getLevel(), 3, 'configuration log level from pinpoint-node-agent.json')
t.end()
})

0 comments on commit 32131b9

Please sign in to comment.