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

fix(logging): Added check for log level value #13941

Merged
merged 22 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
6 changes: 5 additions & 1 deletion lib/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import configSerializer from './config-serializer';
import errSerializer from './err-serializer';
import { RenovateStream } from './pretty-stdout';
import type { BunyanRecord, Logger } from './types';
import { ProblemStream, withSanitizer } from './utils';
import { ProblemStream, validateLogLevel, withSanitizer } from './utils';

let logContext: string = process.env.LOG_CONTEXT ?? nanoid();
let curMeta: Record<string, unknown> = {};

const problems = new ProblemStream();

if (is.string(process.env.LOG_LEVEL)) {
process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase().trim();
}
validateLogLevel(process.env.LOG_LEVEL);
const stdout: bunyan.Stream = {
name: 'stdout',
level:
Expand Down
33 changes: 33 additions & 0 deletions lib/logger/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { validateLogLevel } from './utils';

describe('logger/utils', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('checks for valid log levels', () => {
expect(validateLogLevel(undefined)).toBeUndefined();
expect(validateLogLevel('warn')).toBeUndefined();
expect(validateLogLevel('debug')).toBeUndefined();
expect(validateLogLevel('trace')).toBeUndefined();
expect(validateLogLevel('info')).toBeUndefined();
});

it.each`
input
${'warning'}
${'100'}
${''}
${' '}
`('checks for invalid log level: $input', (input) => {
// Mock when the function exits
const mockExit = jest.spyOn(process, 'exit');
mockExit.mockImplementationOnce((number) => {
throw new Error(`process.exit: ${number}`);
});
expect(() => {
validateLogLevel(input);
}).toThrow();
expect(mockExit).toHaveBeenCalledWith(1);
});
});
37 changes: 37 additions & 0 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,40 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream {

throw new Error("Missing 'stream' or 'path' for bunyan stream");
}

/**
* A function that terminates exeution if the log level that was entered is
* not a valid value for the Bunyan logger.
* @param logLevelToCheck
* @returns returns undefined when the logLevelToCheck is valid. Else it stops execution.
*/
export function validateLogLevel(logLevelToCheck: string | undefined): void {
const allowedValues: bunyan.LogLevel[] = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
];
if (
is.undefined(logLevelToCheck) ||
(is.string(logLevelToCheck) &&
allowedValues.includes(logLevelToCheck as bunyan.LogLevel))
) {
// log level is in the allowed values or its undefined
return;
}

const logger = bunyan.createLogger({
name: 'renovate',
streams: [
{
level: 'fatal',
stream: process.stdout,
},
],
});
logger.fatal(`${logLevelToCheck} is not a valid log level. terminating...`);
process.exit(1);
}