Skip to content

Commit

Permalink
refactor(logger): improve readability and modularity (#33058)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
  • Loading branch information
Gabriel-Ladzaretti and viceice authored Dec 17, 2024
1 parent af6a80e commit 3b9464c
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 127 deletions.
105 changes: 95 additions & 10 deletions lib/logger/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { WriteStream } from 'node:fs';
import bunyan from 'bunyan';
import fs from 'fs-extra';
import { partial } from '../../test/util';
import { add } from '../util/host-rules';
import { addSecretForSanitizing as addSecret } from '../util/sanitize';
import type { RenovateLogger } from './renovate-logger';
import {
addMeta,
addStream,
Expand All @@ -17,16 +19,38 @@ import {
setMeta,
} from '.';

const initialContext = 'initial_context';

jest.unmock('.');
jest.mock('nanoid', () => ({
nanoid: () => 'initial_context',
}));

const bunyanDebugSpy = jest.spyOn(bunyan.prototype, 'debug');

describe('logger/index', () => {
it('inits', () => {
expect(logger).toBeDefined();
});

it('uses an auto-generated log context', () => {
logger.debug('');

expect(bunyanDebugSpy).toHaveBeenCalledWith(
{ logContext: initialContext },
'',
);
});

it('sets and gets context', () => {
setContext('123test');
expect(getContext()).toBe('123test');
const logContext = '123test';
const msg = 'test';
setContext(logContext);

logger.debug(msg);

expect(getContext()).toBe(logContext);
expect(bunyanDebugSpy).toHaveBeenCalledWith({ logContext }, msg);
});

it('supports logging with metadata', () => {
Expand All @@ -41,16 +65,62 @@ describe('logger/index', () => {
expect(() => logger.debug('some meta')).not.toThrow();
});

it('sets meta', () => {
expect(() => setMeta({ any: 'test' })).not.toThrow();
});
describe('meta functions', () => {
beforeEach(() => {
setContext(initialContext);
});

it('adds meta', () => {
expect(() => addMeta({ new: 'test' })).not.toThrow();
});
it('sets meta', () => {
const logMeta = { foo: 'foo' };
const meta = { bar: 'bar' };
setMeta(meta);

logger.debug(logMeta, '');

expect(bunyanDebugSpy).toHaveBeenCalledWith(
{ logContext: initialContext, ...meta, ...logMeta },
'',
);
expect(bunyanDebugSpy).toHaveBeenCalledTimes(1);
});

it('removes meta', () => {
expect(() => removeMeta(['new'])).not.toThrow();
it('adds meta', () => {
const logMeta = { foo: 'foo' };
const meta = { bar: 'bar' };
addMeta(meta);

logger.debug(logMeta, '');

expect(bunyanDebugSpy).toHaveBeenCalledWith(
{ logContext: initialContext, ...meta, ...logMeta },
'',
);
expect(bunyanDebugSpy).toHaveBeenCalledTimes(1);
});

it('removes meta', () => {
const logMeta = { foo: 'foo' };
const meta = { bar: 'bar' };
setMeta(meta);

logger.debug(logMeta, '');

expect(bunyanDebugSpy).toHaveBeenCalledWith(
{ logContext: initialContext, ...meta, ...logMeta },
'',
);
expect(bunyanDebugSpy).toHaveBeenCalledTimes(1);

removeMeta(Object.keys(meta));

logger.debug(logMeta, '');

expect(bunyanDebugSpy).toHaveBeenCalledWith(
{ logContext: initialContext, ...logMeta },
'',
);
expect(bunyanDebugSpy).toHaveBeenCalledTimes(2);
});
});

it('sets level', () => {
Expand All @@ -59,15 +129,30 @@ describe('logger/index', () => {
expect(logLevel()).toBe('debug');
});

it('should create a child logger', () => {
const childLogger = (logger as RenovateLogger).childLogger();
const loggerSpy = jest.spyOn(logger, 'debug');
const childLoggerSpy = jest.spyOn(childLogger, 'debug');

childLogger.debug('test');

expect(loggerSpy).toHaveBeenCalledTimes(0);
expect(childLoggerSpy).toHaveBeenCalledTimes(1);
expect(childLoggerSpy).toHaveBeenCalledWith('test');
});

it('saves problems', () => {
addSecret('p4$$w0rd');
levels('stdout', 'fatal');
logger.fatal('fatal error');
logger.error('some meta');
logger.error({ some: 'meta', password: 'super secret' });
logger.error({ some: 'meta' }, 'message');
logger.warn('a warning with a p4$$w0rd');
logger.trace('ignored');
logger.info('ignored');
expect(getProblems()).toMatchObject([
{ msg: 'fatal error' },
{ msg: 'some meta' },
{ some: 'meta', password: '***********' },
{ some: 'meta', msg: 'message' },
Expand Down
Loading

0 comments on commit 3b9464c

Please sign in to comment.