Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(logger): improve readability and modularity #33058

Merged
merged 20 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading