diff --git a/lib/config.js b/lib/config.js index c3062863..12052162 100644 --- a/lib/config.js +++ b/lib/config.js @@ -10,6 +10,8 @@ const path = require('path') const fs = require('fs') const defaultConfig = require('./pinpoint-config-default') const log = require('./utils/logger') +const { setLog } = require('./supports') +const { makeLogLevelLog } = require('./utils/log/log-level-logger') const valueOfString = (envName) => { return () => { @@ -142,6 +144,7 @@ const init = (initOptions = {}) => { readConfigJson(initOptions)) log.init(agentConfig.logLevel) + setLog(makeLogLevelLog(agentConfig.logLevel)) Object.entries(REQUIRE_CONFIG).forEach(([propertyName, description]) => { if (agentConfig.enable && !agentConfig[propertyName]) { @@ -247,15 +250,10 @@ function hasDockerCGroup() { } } -function needsLoadConfig() { - return !agentConfig -} - module.exports = { getConfig, clear, readConfigJson, readRootConfigFile, - getMainModulePath, - needsLoadConfig + getMainModulePath } diff --git a/lib/supports.js b/lib/supports.js index 9d87ac99..e3b5a440 100644 --- a/lib/supports.js +++ b/lib/supports.js @@ -7,8 +7,6 @@ 'use strict' const Logger = require('./utils/log/logger2') -const { getConfig, needsLoadConfig } = require('./config') -const defaultLog = require('loglevel').getLogger('default_logger') let log = undefined module.exports = { @@ -17,8 +15,8 @@ module.exports = { return log } - if (needsLoadConfig()) { - return new Logger.NoneBuilder({ + if (!log) { + log = new Logger.NoneBuilder({ output: console, debug: function (message) { this.output.debug(message) @@ -34,29 +32,9 @@ module.exports = { } }).build() } - - const config = getConfig() - if (!log) { - 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 + setLog: function(logger) { + log = logger } } \ No newline at end of file diff --git a/lib/utils/log/log-level-logger.js b/lib/utils/log/log-level-logger.js new file mode 100644 index 00000000..206995e9 --- /dev/null +++ b/lib/utils/log/log-level-logger.js @@ -0,0 +1,31 @@ +/** + * Pinpoint Node.js Agent + * Copyright 2022-present NAVER Corp. + * Apache License v2.0 + */ + +'use strict' + +const defaultLog = require('loglevel').getLogger('pinpoint-console-logger') +const Logger = require('./logger2') + +module.exports = { + makeLogLevelLog: function (logLevel) { + defaultLog.setLevel(Logger.logTypeNameOf(logLevel)) + return Logger.makeBuilder(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() + } +} \ No newline at end of file diff --git a/test/utils/log/logger.integration.test.js b/test/utils/log/logger.integration.test.js index 5f1478bb..d7584d23 100644 --- a/test/utils/log/logger.integration.test.js +++ b/test/utils/log/logger.integration.test.js @@ -6,7 +6,7 @@ 'use strict' const test = require('tape') -const { getLog, clearLog } = require('../../../lib/supports') +const { getLog, setLog } = require('../../../lib/supports') const Agent = require('../../../lib/agent') const { clear, getConfig } = require('../../../lib/config') @@ -44,7 +44,7 @@ test('no config logger', (t) => { test('logger full cycle', (t) => { clear() - clearLog() + setLog(null) let actual = getLog() t.true(actual.adaptor.output.output === console, 'when no configuration loaded, logs use console output') getConfig()