diff options
Diffstat (limited to 'lib/logger/index.spec.ts')
-rw-r--r-- | lib/logger/index.spec.ts | 105 |
1 files changed, 95 insertions, 10 deletions
diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts index a2eb3e3beb1..6fc7c67dead 100644 --- a/lib/logger/index.spec.ts +++ b/lib/logger/index.spec.ts @@ -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, @@ -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', () => { @@ -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', () => { @@ -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' }, |