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 1 commit
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/config/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bunyan from 'bunyan';
import type { RenovateConfig } from './types';
import * as configValidation from './validation';

Expand Down Expand Up @@ -716,5 +717,32 @@ describe('config/validation', () => {
},
]);
});

it('checks for valid log level', () => {
expect(
configValidation.isValidLogLevel('warning' as bunyan.LogLevel)
).toBeFalsy();
expect(
configValidation.isValidLogLevel('warn' as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel('trace' as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel(' ' as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel('' as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel(20 as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel(10 as bunyan.LogLevel)
).toBeTruthy();
expect(
configValidation.isValidLogLevel(100 as bunyan.LogLevel)
).toBeFalsy();
});
});
});
39 changes: 39 additions & 0 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import bunyan from 'bunyan';
import { getLanguageList, getManagerList } from '../manager';
import { configRegexPredicate, isConfigRegex, regEx } from '../util/regex';
import * as template from '../util/template';
Expand Down Expand Up @@ -570,3 +571,41 @@ export async function validateConfig(
warnings.sort(sortAll);
return { errors, warnings };
}

export function isValidLogLevel(
hasanwhitesource marked this conversation as resolved.
Show resolved Hide resolved
logLevelToCheck: bunyan.LogLevel
): logLevelToCheck is bunyan.LogLevel {
const allowedValues: bunyan.LogLevel[] = [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
bunyan.TRACE,
bunyan.DEBUG,
bunyan.INFO,
bunyan.WARN,
bunyan.ERROR,
bunyan.FATAL,
];
const result: boolean =
allowedValues.indexOf(logLevelToCheck) !== -1 ||
(typeof logLevelToCheck === 'string' &&
logLevelToCheck.trim().length === 0);

if (!result) {
const Logger = bunyan.createLogger({
name: 'log level error log',
streams: [
{
level: 'fatal',
stream: process.stdout, // log INFO and above to stdout
},
],
});
Logger.fatal(`${logLevelToCheck} is not a valid log level terminating`);
}

return result;
}
5 changes: 5 additions & 0 deletions lib/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import is from '@sindresorhus/is';
import * as bunyan from 'bunyan';
import { nanoid } from 'nanoid';
import { isValidLogLevel } from '../config/validation';
import cmdSerializer from './cmd-serializer';
import configSerializer from './config-serializer';
import errSerializer from './err-serializer';
Expand All @@ -13,6 +14,10 @@ let curMeta: Record<string, unknown> = {};

const problems = new ProblemStream();

process.env.LOG_LEVEL.toLocaleLowerCase();
hasanwhitesource marked this conversation as resolved.
Show resolved Hide resolved
if (!isValidLogLevel(process.env.LOG_LEVEL as bunyan.LogLevel)) {
hasanwhitesource marked this conversation as resolved.
Show resolved Hide resolved
process.exit(1);
hasanwhitesource marked this conversation as resolved.
Show resolved Hide resolved
}
const stdout: bunyan.Stream = {
name: 'stdout',
level:
Expand Down