Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(logging): demo logger log
Browse files Browse the repository at this point in the history
  • Loading branch information
agorskiy12 committed Apr 19, 2024
1 parent f9b13e5 commit 0ed81b2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions __tests__/server/config/env/__snapshots__/argv.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ exports[`argv config has development options and defaults 6`] = `
"error",
"warn",
"log",
"dev",
"info",
"debug",
"trace",
Expand Down Expand Up @@ -112,6 +113,7 @@ exports[`argv config has production options and defaults 2`] = `
"error",
"warn",
"log",
"dev",
"info",
"debug",
"trace",
Expand Down
10 changes: 10 additions & 0 deletions __tests__/server/utils/logging/monkeyPatchConsole.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
* permissions and limitations under the License.
*/

const { default: logger } = require('../../../../src/server/utils/logging/logger');

jest.mock('yargs', () => ({
argv: {
logLevel: 'trace',
},
}));

describe('monkeyPatchConsole', () => {
jest.spyOn(logger, 'log').mockImplementation(() => {});

const logMethods = ['error', 'warn', 'log', 'info', 'debug', 'trace'];
const originalConsole = logMethods.reduce((acc, curr) => ({
...acc,
Expand All @@ -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);
});
});
4 changes: 3 additions & 1 deletion __tests__/server/utils/pollModuleMap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/config/env/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
3 changes: 2 additions & 1 deletion src/server/utils/logging/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/server/utils/logging/config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion src/server/utils/pollModuleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {

holocron as holocronMetrics,
} from '../metrics';
import logger from './logging/logger';

let moduleMapHealthy = null;
export const getModuleMapHealth = () => moduleMapHealthy;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0ed81b2

Please sign in to comment.