From 0ed81b2ff4d67dca17f4771aeab65e8723408d16 Mon Sep 17 00:00:00 2001 From: agorskiy12 Date: Fri, 19 Apr 2024 15:46:00 -0400 Subject: [PATCH] feat(logging): demo logger log --- .../server/config/env/__snapshots__/argv.spec.js.snap | 2 ++ .../server/utils/logging/monkeyPatchConsole.spec.js | 10 ++++++++++ __tests__/server/utils/pollModuleMap.spec.js | 4 +++- src/server/config/env/argv.js | 2 +- src/server/utils/logging/config/base.js | 3 ++- src/server/utils/logging/config/development.js | 3 ++- src/server/utils/pollModuleMap.js | 3 ++- 7 files changed, 22 insertions(+), 5 deletions(-) diff --git a/__tests__/server/config/env/__snapshots__/argv.spec.js.snap b/__tests__/server/config/env/__snapshots__/argv.spec.js.snap index 8b496c71a..bba8f8da9 100644 --- a/__tests__/server/config/env/__snapshots__/argv.spec.js.snap +++ b/__tests__/server/config/env/__snapshots__/argv.spec.js.snap @@ -66,6 +66,7 @@ exports[`argv config has development options and defaults 6`] = ` "error", "warn", "log", + "dev", "info", "debug", "trace", @@ -112,6 +113,7 @@ exports[`argv config has production options and defaults 2`] = ` "error", "warn", "log", + "dev", "info", "debug", "trace", diff --git a/__tests__/server/utils/logging/monkeyPatchConsole.spec.js b/__tests__/server/utils/logging/monkeyPatchConsole.spec.js index ae0c8e444..16adc1947 100644 --- a/__tests__/server/utils/logging/monkeyPatchConsole.spec.js +++ b/__tests__/server/utils/logging/monkeyPatchConsole.spec.js @@ -14,6 +14,8 @@ * permissions and limitations under the License. */ +const { default: logger } = require('../../../../src/server/utils/logging/logger'); + jest.mock('yargs', () => ({ argv: { logLevel: 'trace', @@ -21,6 +23,8 @@ jest.mock('yargs', () => ({ })); describe('monkeyPatchConsole', () => { + jest.spyOn(logger, 'log').mockImplementation(() => {}); + const logMethods = ['error', 'warn', 'log', 'info', 'debug', 'trace']; const originalConsole = logMethods.reduce((acc, curr) => ({ ...acc, @@ -43,4 +47,10 @@ describe('monkeyPatchConsole', () => { expect(console[method]).not.toBe(originalConsole[method]); }); }); + + it('allows to use logger methods and not console', () => { + logger.log('testing'); + + expect(logger.log).toHaveBeenCalledTimes(1); + }); }); diff --git a/__tests__/server/utils/pollModuleMap.spec.js b/__tests__/server/utils/pollModuleMap.spec.js index 0f80746ed..82beaf680 100644 --- a/__tests__/server/utils/pollModuleMap.spec.js +++ b/__tests__/server/utils/pollModuleMap.spec.js @@ -14,6 +14,7 @@ * permissions and limitations under the License. */ import util from 'node:util'; +import logger from '../../../src/server/utils/logging/logger'; jest.useFakeTimers(); jest.spyOn(global, 'setTimeout'); @@ -22,6 +23,7 @@ jest.spyOn(global, 'setImmediate'); describe('pollModuleMap', () => { jest.spyOn(console, 'log').mockImplementation(util.format); + jest.spyOn(logger, 'log').mockImplementation(util.format); jest.spyOn(console, 'warn').mockImplementation(util.format); jest.spyOn(console, 'error').mockImplementation(util.format); let loadModules; @@ -110,7 +112,7 @@ describe('pollModuleMap', () => { const { default: pollModuleMap } = load(); await pollModuleMap(); - expect(console.log).toHaveBeenCalledWith('pollModuleMap: polling...'); + expect(logger.log).toHaveBeenCalledWith('pollModuleMap: polling...'); expect(loadModules).toHaveBeenCalledTimes(1); expect(incrementCounter).toHaveBeenCalledTimes(1); expect(incrementCounter).toHaveBeenCalledWith(holocronMetrics.moduleMapPoll); diff --git a/src/server/config/env/argv.js b/src/server/config/env/argv.js index 8f0105fec..960f3c7dd 100644 --- a/src/server/config/env/argv.js +++ b/src/server/config/env/argv.js @@ -83,7 +83,7 @@ yargs .option('log-level', { describe: 'Lowest level of log entries to show', type: 'string', - choices: ['error', 'warn', 'log', 'info', 'debug', 'trace'], + choices: ['error', 'warn', 'log', 'dev', 'info', 'debug', 'trace'], default: process.env.NODE_ENV === 'development' ? 'log' : 'info', }); diff --git a/src/server/utils/logging/config/base.js b/src/server/utils/logging/config/base.js index 670f1df64..9371c5b48 100644 --- a/src/server/utils/logging/config/base.js +++ b/src/server/utils/logging/config/base.js @@ -18,8 +18,9 @@ import util from 'node:util'; import { argv } from 'yargs'; export default { - level: argv.logLevel, + level: argv.logLevel || 'info', customLevels: { + dev: 34, log: 35, }, dedupe: true, diff --git a/src/server/utils/logging/config/development.js b/src/server/utils/logging/config/development.js index e09bbc855..bad026473 100644 --- a/src/server/utils/logging/config/development.js +++ b/src/server/utils/logging/config/development.js @@ -28,12 +28,13 @@ export const pinoPrettyOptions = { trace: 10, debug: 20, info: 30, + dev: 34, log: 35, warn: 40, error: 50, fatal: 60, }, - customColors: 'trace:white,debug:green,info:gray,log:blue,warn:yellow,error:red,fatal:bgRed', + customColors: 'trace:white,debug:green,info:gray,log:blue,dev:magenta,warn:yellow,error:red,fatal:bgRed', messageFormat(log, messageKey) { if (log.request) { if (log.request.direction === 'out') { diff --git a/src/server/utils/pollModuleMap.js b/src/server/utils/pollModuleMap.js index 0def60cb1..a3fad9c3a 100644 --- a/src/server/utils/pollModuleMap.js +++ b/src/server/utils/pollModuleMap.js @@ -24,6 +24,7 @@ import { holocron as holocronMetrics, } from '../metrics'; +import logger from './logging/logger'; let moduleMapHealthy = null; export const getModuleMapHealth = () => moduleMapHealthy; @@ -126,7 +127,7 @@ async function pollModuleMap() { recordPollingForMonitor(); startPollingMonitorIfNotAlready(); try { - console.log('pollModuleMap: polling...'); + logger.log('pollModuleMap: polling...'); incrementCounter(holocronMetrics.moduleMapPoll); const { loadedModules = {}, rejectedModules = {} } = await loadModules();