Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Ladzaretti committed Dec 13, 2024
1 parent af464f9 commit 97922a3
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 169 deletions.
2 changes: 1 addition & 1 deletion lib/logger/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 '.';
import type { RenovateLogger } from './renovate-logger';
import {
addMeta,
addStream,
Expand Down
188 changes: 27 additions & 161 deletions lib/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import upath from 'upath';
import cmdSerializer from './cmd-serializer';
import configSerializer from './config-serializer';
import errSerializer from './err-serializer';
import { once, reset as onceReset } from './once';
import { RenovateStream } from './pretty-stdout';
import { getRemappedLevel } from './remap';
import { RenovateLogger } from './renovate-logger';
import type { BunyanRecord, Logger } from './types';
import {
ProblemStream,
Expand All @@ -20,19 +19,11 @@ import {
const problems = new ProblemStream();
let stdoutLevel = validateLogLevel(getEnv('LOG_LEVEL'), 'info');

export function getProblems(): BunyanRecord[] {
return problems.getProblems();
}

export function clearProblems(): void {
return problems.clearProblems();
}

export function logLevel(): bunyan.LogLevelString {
return stdoutLevel;
}

function createDefaultStreams(
export function createDefaultStreams(
stdoutLevel: bunyan.LogLevelString,
problems: ProblemStream,
logFile: string | undefined,
Expand Down Expand Up @@ -82,164 +73,31 @@ export function createLogFileStream(logFile: string): bunyan.Stream {
};
}

type loggerFunction = (p1: string | Record<string, any>, p2?: string) => void;

function logFactory(
bunyanLogger: bunyan,
_level: bunyan.LogLevelString,
curMeta: Record<string, unknown>,
logContext: string,
): loggerFunction {
return function (p1: string | Record<string, any>, p2?: string): void {
const meta: Record<string, unknown> = {
logContext,
...curMeta,
...toMeta(p1),
};
const msg = getMessage(p1, p2);
let level = _level;

if (is.string(msg)) {
const remappedLevel = getRemappedLevel(msg);
// istanbul ignore if: not testable
if (remappedLevel) {
meta.oldLevel = level;
level = remappedLevel;
}
bunyanLogger[level](meta, msg);
} else {
bunyanLogger[level](meta);
}
};
}

function getMessage(
p1: string | Record<string, any>,
p2?: string,
): string | undefined {
return is.string(p1) ? p1 : p2;
}

function toMeta(p1: string | Record<string, any>): Record<string, unknown> {
return is.object(p1) ? p1 : {};
}

const loggerLevels: bunyan.LogLevelString[] = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
];

export class RenovateLogger implements Logger {
logger: Logger = { once: { reset: onceReset } } as any;

constructor(
private readonly bunyanLogger: bunyan,
private context: string,
private meta: Record<string, unknown>,
) {
for (const level of loggerLevels) {
this.logger[level] = this.logFactory(level) as never;
this.logger.once[level] = this.logOnceFn(level);
}
}

addStream(stream: bunyan.Stream): void {
this.bunyanLogger.addStream(withSanitizer(stream));
}

childLogger(): RenovateLogger {
return new RenovateLogger(
this.bunyanLogger.child({}),
this.context,
this.meta,
);
}

trace = this.log.bind(this, 'trace');
debug = this.log.bind(this, 'debug');
info = this.log.bind(this, 'info');
warn = this.log.bind(this, 'warn');
error = this.log.bind(this, 'error');
fatal = this.log.bind(this, 'fatal');

once = this.logger.once;

get logContext(): string {
return this.context;
}

set logContext(context: string) {
this.context = context;
}

setMeta(obj: Record<string, unknown>): void {
this.meta = { ...obj };
}

addMeta(obj: Record<string, unknown>): void {
this.meta = { ...this.meta, ...obj };
}

removeMeta(fields: string[]): void {
for (const key of Object.keys(this.meta)) {
if (fields.includes(key)) {
delete this.meta[key];
}
}
}

private logFactory(level: bunyan.LogLevelString): loggerFunction {
return logFactory(this.bunyanLogger, level, this.meta, this.context);
}

private logOnceFn(level: bunyan.LogLevelString): loggerFunction {
const logOnceFn = (p1: string | Record<string, any>, p2?: string): void => {
once(() => {
this.log(level, p1, p2);
}, logOnceFn);
};
return logOnceFn;
}

private log(
level: bunyan.LogLevelString,
p1: string | Record<string, any>,
p2?: string,
): void {
const logFn = this.logger[level];
if (is.string(p1)) {
logFn(p1);
} else {
logFn(p1, p2);
}
}
export function createSanitizerLogger(streams: bunyan.Stream[]): bunyan {
return bunyan.createLogger({
name: 'renovate',
serializers: {
body: configSerializer,
cmd: cmdSerializer,
config: configSerializer,
migratedConfig: configSerializer,
originalConfig: configSerializer,
presetConfig: configSerializer,
oldConfig: configSerializer,
newConfig: configSerializer,
err: errSerializer,
},
streams: streams.map(withSanitizer),
});
}

const defaultStreams = createDefaultStreams(
stdoutLevel,
problems,
getEnv('LOG_FILE'),
);
const bunyanLogger = bunyan.createLogger({
name: 'renovate',
serializers: {
body: configSerializer,
cmd: cmdSerializer,
config: configSerializer,
migratedConfig: configSerializer,
originalConfig: configSerializer,
presetConfig: configSerializer,
oldConfig: configSerializer,
newConfig: configSerializer,
err: errSerializer,
},
streams: defaultStreams.map(withSanitizer),
});

const bunyanLogger = createSanitizerLogger(defaultStreams);
const logContext: string = getEnv('LOG_CONTEXT') ?? nanoid();
const loggerInternal = new RenovateLogger(bunyanLogger, logContext, {});

Expand Down Expand Up @@ -289,3 +147,11 @@ export function levels(
stdoutLevel = level;
}
}

export function getProblems(): BunyanRecord[] {
return problems.getProblems();
}

export function clearProblems(): void {
return problems.clearProblems();
}
13 changes: 6 additions & 7 deletions lib/logger/once.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { once, reset } from './once';
import type { RenovateLogger } from '.';
import { logger } from '.';

jest.unmock('.');
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('logger/once', () => {

describe('logger', () => {
it('logs once per function call', () => {
const debug = jest.spyOn((logger as RenovateLogger).logger, 'debug');
const debug = jest.spyOn(logger, 'debug');

function doSomething() {
logger.once.debug('test');
Expand All @@ -69,8 +68,8 @@ describe('logger/once', () => {
});

it('distincts between log levels', () => {
const debug = jest.spyOn((logger as RenovateLogger).logger, 'debug');
const info = jest.spyOn((logger as RenovateLogger).logger, 'info');
const debug = jest.spyOn(logger, 'debug');
const info = jest.spyOn(logger, 'info');

function doSomething() {
logger.once.debug('test');
Expand All @@ -85,7 +84,7 @@ describe('logger/once', () => {
});

it('distincts between different log statements', () => {
const debug = jest.spyOn((logger as RenovateLogger).logger, 'debug');
const debug = jest.spyOn(logger, 'debug');

function doSomething() {
logger.once.debug('foo');
Expand All @@ -102,7 +101,7 @@ describe('logger/once', () => {
});

it('allows mixing single-time and regular logging', () => {
const debug = jest.spyOn((logger as RenovateLogger).logger, 'debug');
const debug = jest.spyOn(logger, 'debug');

function doSomething() {
logger.once.debug('foo');
Expand All @@ -124,7 +123,7 @@ describe('logger/once', () => {
});

it('supports reset method', () => {
const debug = jest.spyOn((logger as RenovateLogger).logger, 'debug');
const debug = jest.spyOn(logger, 'debug');

function doSomething() {
logger.once.debug('foo');
Expand Down
Loading

0 comments on commit 97922a3

Please sign in to comment.