From 91addbd431c4444b59346239f6fe60a6e0e92690 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Mon, 31 Jan 2022 16:19:35 +0200 Subject: [PATCH 01/17] Added check for log level value and test --- lib/config/validation.spec.ts | 28 +++++++++++++++++++++++++ lib/config/validation.ts | 39 +++++++++++++++++++++++++++++++++++ lib/logger/index.ts | 5 +++++ 3 files changed, 72 insertions(+) diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts index 1f31f88bd5d500..1482ad05f420d3 100644 --- a/lib/config/validation.spec.ts +++ b/lib/config/validation.spec.ts @@ -1,3 +1,4 @@ +import bunyan from 'bunyan'; import type { RenovateConfig } from './types'; import * as configValidation from './validation'; @@ -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(); + }); }); }); diff --git a/lib/config/validation.ts b/lib/config/validation.ts index 98d7750cefd7df..1e68bfcc8292d0 100644 --- a/lib/config/validation.ts +++ b/lib/config/validation.ts @@ -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'; @@ -570,3 +571,41 @@ export async function validateConfig( warnings.sort(sortAll); return { errors, warnings }; } + +export function isValidLogLevel( + 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; +} diff --git a/lib/logger/index.ts b/lib/logger/index.ts index 66f91378fdc236..46f91b870b5e76 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -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'; @@ -13,6 +14,10 @@ let curMeta: Record = {}; const problems = new ProblemStream(); +process.env.LOG_LEVEL.toLocaleLowerCase(); +if (!isValidLogLevel(process.env.LOG_LEVEL as bunyan.LogLevel)) { + process.exit(1); +} const stdout: bunyan.Stream = { name: 'stdout', level: From 7bf20c90886e31d4efd7b971db0c2d12d517de6b Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Tue, 1 Feb 2022 13:33:51 +0200 Subject: [PATCH 02/17] Moved function to /logger/utils --- lib/config/validation.spec.ts | 28 ------------------------- lib/config/validation.ts | 39 ----------------------------------- lib/logger/index.spec.ts | 28 +++++++++++++++++++++++++ lib/logger/index.ts | 2 +- lib/logger/utils.ts | 38 ++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 68 deletions(-) diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts index 1482ad05f420d3..1f31f88bd5d500 100644 --- a/lib/config/validation.spec.ts +++ b/lib/config/validation.spec.ts @@ -1,4 +1,3 @@ -import bunyan from 'bunyan'; import type { RenovateConfig } from './types'; import * as configValidation from './validation'; @@ -717,32 +716,5 @@ 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(); - }); }); }); diff --git a/lib/config/validation.ts b/lib/config/validation.ts index 1e68bfcc8292d0..98d7750cefd7df 100644 --- a/lib/config/validation.ts +++ b/lib/config/validation.ts @@ -1,5 +1,4 @@ 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'; @@ -571,41 +570,3 @@ export async function validateConfig( warnings.sort(sortAll); return { errors, warnings }; } - -export function isValidLogLevel( - 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; -} diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts index 3a3e10966360e1..ce04ec5ef29160 100644 --- a/lib/logger/index.spec.ts +++ b/lib/logger/index.spec.ts @@ -1,4 +1,5 @@ import _fs from 'fs-extra'; +import bunyan from 'bunyan'; import { add } from '../util/host-rules'; import { add as addSecret } from '../util/sanitize'; import { @@ -182,4 +183,31 @@ describe('logger/index', () => { expect(logged.someFn).toBe('[function]'); expect(logged.someObject.field).toBe('**redacted**'); }); + + 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(); + }); }); diff --git a/lib/logger/index.ts b/lib/logger/index.ts index 46f91b870b5e76..42f03e4fe9edb4 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -1,7 +1,7 @@ import is from '@sindresorhus/is'; import * as bunyan from 'bunyan'; import { nanoid } from 'nanoid'; -import { isValidLogLevel } from '../config/validation'; +import { isValidLogLevel } from './utils'; import cmdSerializer from './cmd-serializer'; import configSerializer from './config-serializer'; import errSerializer from './err-serializer'; diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index cb0de0a395ba7d..091fdc4aff28a6 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -206,3 +206,41 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream { throw new Error("Missing 'stream' or 'path' for bunyan stream"); } + +export function isValidLogLevel( + 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, + }, + ], + }); + Logger.fatal(`${logLevelToCheck} is not a valid log level. terminating...`); + } + + return result; +} From 5df01d37704b7d2300fa0845fa00559618b61974 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Tue, 1 Feb 2022 14:19:25 +0200 Subject: [PATCH 03/17] Fixed imports --- lib/logger/index.spec.ts | 35 ++++++++++------------------------- lib/logger/index.ts | 3 +-- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts index ce04ec5ef29160..5a8463dcefdb9d 100644 --- a/lib/logger/index.spec.ts +++ b/lib/logger/index.spec.ts @@ -1,7 +1,8 @@ -import _fs from 'fs-extra'; import bunyan from 'bunyan'; +import _fs from 'fs-extra'; import { add } from '../util/host-rules'; import { add as addSecret } from '../util/sanitize'; +import { isValidLogLevel } from './utils'; import { addMeta, addStream, @@ -185,29 +186,13 @@ describe('logger/index', () => { }); 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(); + expect(isValidLogLevel('warning' as bunyan.LogLevel)).toBeFalsy(); + expect(isValidLogLevel('warn' as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel('trace' as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel(' ' as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel('' as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel(20 as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel(10 as bunyan.LogLevel)).toBeTruthy(); + expect(isValidLogLevel(100 as bunyan.LogLevel)).toBeFalsy(); }); }); diff --git a/lib/logger/index.ts b/lib/logger/index.ts index 42f03e4fe9edb4..9c85b5d9db9c65 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -1,13 +1,12 @@ import is from '@sindresorhus/is'; import * as bunyan from 'bunyan'; import { nanoid } from 'nanoid'; -import { isValidLogLevel } from './utils'; import cmdSerializer from './cmd-serializer'; 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, isValidLogLevel, withSanitizer } from './utils'; let logContext: string = process.env.LOG_CONTEXT ?? nanoid(); let curMeta: Record = {}; From fc97e3f563ee7a0c9e120a08bea321e7eaa5dc16 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Tue, 1 Feb 2022 15:12:15 +0200 Subject: [PATCH 04/17] yarn stuff --- lib/config/migration.spec.ts | 1 + lib/logger/utils.ts | 12 ++++-- tsconfig.json | 1 - tsconfig.spec.json | 5 ++- yarn.lock | 77 ++++++++++++++++++++---------------- 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/config/migration.spec.ts b/lib/config/migration.spec.ts index ea60ebd9c099cc..a8767750c6916d 100644 --- a/lib/config/migration.spec.ts +++ b/lib/config/migration.spec.ts @@ -1,3 +1,4 @@ +import { expect } from '@jest/globals'; import { PlatformId } from '../constants'; import { getConfig } from './defaults'; import { GlobalConfig } from './global'; diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 091fdc4aff28a6..169d479dcbf87b 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -224,11 +224,15 @@ export function isValidLogLevel( bunyan.ERROR, bunyan.FATAL, ]; - const result: boolean = - allowedValues.indexOf(logLevelToCheck) !== -1 || - (typeof logLevelToCheck === 'string' && - logLevelToCheck.trim().length === 0); + if ( + typeof logLevelToCheck === 'string' && + logLevelToCheck.trim().length === 0 + ) { + // if the log level is empty string then return true + return true; + } + const result: boolean = allowedValues.indexOf(logLevelToCheck) !== -1; if (!result) { const Logger = bunyan.createLogger({ name: 'log level error log', diff --git a/tsconfig.json b/tsconfig.json index 636b8bb24d90b4..c00c0b323296c3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,7 +22,6 @@ "checkJs": true }, "exclude": [ - "node_modules", "./.cache", "./dist", "**/__mocks__/*", diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 101c5a0ad2f8fb..d0463cc8f9e8a2 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -1,7 +1,8 @@ { "extends": "./tsconfig", "compilerOptions": { - "allowJs": false, - "checkJs": false + "allowJs": true, + "checkJs": true, + "types": ["node", "jest", "jest-extended", "expect-more-jest"] } } diff --git a/yarn.lock b/yarn.lock index 15c84931e661fc..db3131e331812c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -965,19 +965,19 @@ integrity sha512-m7OkX0IdKLKPpBlJtF561YJal5y/jyI5fNfWbPxh2D/nbzzGI4qRyrD8xO2jB24u7l+5I2a43scCG2IrfjC50Q== "@babel/core@^7.1.0", "@babel/core@^7.12.3", "@babel/core@^7.7.2", "@babel/core@^7.8.0": - version "7.16.7" - resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.16.7.tgz#db990f931f6d40cb9b87a0dc7d2adc749f1dcbcf" - integrity sha512-aeLaqcqThRNZYmbMqtulsetOQZ/5gbR/dWruUCJcpas4Qoyy+QeagfDsPdMrqwsPRDNxJvBlRiZxxX7THO7qtA== + version "7.16.12" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.16.12.tgz#5edc53c1b71e54881315923ae2aedea2522bb784" + integrity sha512-dK5PtG1uiN2ikk++5OzSYsitZKny4wOCD0nrO4TqnW4BVBTQ2NGS3NgilvT/TEyxTST7LNyWV/T4tXDoD3fOgg== dependencies: "@babel/code-frame" "^7.16.7" - "@babel/generator" "^7.16.7" + "@babel/generator" "^7.16.8" "@babel/helper-compilation-targets" "^7.16.7" "@babel/helper-module-transforms" "^7.16.7" "@babel/helpers" "^7.16.7" - "@babel/parser" "^7.16.7" + "@babel/parser" "^7.16.12" "@babel/template" "^7.16.7" - "@babel/traverse" "^7.16.7" - "@babel/types" "^7.16.7" + "@babel/traverse" "^7.16.10" + "@babel/types" "^7.16.8" convert-source-map "^1.7.0" debug "^4.1.0" gensync "^1.0.0-beta.2" @@ -985,7 +985,7 @@ semver "^6.3.0" source-map "^0.5.0" -"@babel/generator@^7.16.7", "@babel/generator@^7.16.8", "@babel/generator@^7.7.2": +"@babel/generator@^7.16.8", "@babel/generator@^7.7.2": version "7.16.8" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.16.8.tgz#359d44d966b8cd059d543250ce79596f792f2ebe" integrity sha512-1ojZwE9+lOXzcWdWmO6TbUzDfqLD39CmEhN8+2cX9XkDo5yW1OpgfejfliysR2AWLpMamTiOiAp/mtroaymhpw== @@ -1094,18 +1094,18 @@ "@babel/types" "^7.16.7" "@babel/highlight@^7.16.7": - version "7.16.7" - resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.16.7.tgz#81a01d7d675046f0d96f82450d9d9578bdfd6b0b" - integrity sha512-aKpPMfLvGO3Q97V0qhw/V2SWNWlwfJknuwAunU7wZLSfrM4xTBvg7E5opUVi1kJTBKihE38CPg4nBiqX83PWYw== + version "7.16.10" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.16.10.tgz#744f2eb81579d6eea753c227b0f570ad785aba88" + integrity sha512-5FnTQLSLswEj6IkgVw5KusNUUFY9ZGqe/TRFnP/BKYHYgfh7tc+C7mwiy95/yNP7Dh9x580Vv8r7u7ZfTBFxdw== dependencies: "@babel/helper-validator-identifier" "^7.16.7" chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.7", "@babel/parser@^7.16.8": - version "7.16.8" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.16.8.tgz#61c243a3875f7d0b0962b0543a33ece6ff2f1f17" - integrity sha512-i7jDUfrVBWc+7OKcBzEe5n7fbv3i2fWtxKzzCvOjnzSxMfWMigAhtfJ7qzZNGFNMsCCd67+uz553dYKWXPvCKw== +"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.10", "@babel/parser@^7.16.12", "@babel/parser@^7.16.7": + version "7.16.12" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.16.12.tgz#9474794f9a650cf5e2f892444227f98e28cdf8b6" + integrity sha512-VfaV15po8RiZssrkPweyvbGVSe4x2y+aciFCgn0n0/SJMR22cwofRV1mtnJQYcSB1wUTaA/X1LnA3es66MCO5A== "@babel/plugin-syntax-async-generators@^7.8.4": version "7.8.4" @@ -1207,10 +1207,10 @@ "@babel/parser" "^7.16.7" "@babel/types" "^7.16.7" -"@babel/traverse@^7.16.7", "@babel/traverse@^7.7.2": - version "7.16.8" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.16.8.tgz#bab2f2b09a5fe8a8d9cad22cbfe3ba1d126fef9c" - integrity sha512-xe+H7JlvKsDQwXRsBhSnq1/+9c+LlQcCK3Tn/l5sbx02HYns/cn7ibp9+RV1sIUqu7hKg91NWsgHurO9dowITQ== +"@babel/traverse@^7.16.10", "@babel/traverse@^7.16.7", "@babel/traverse@^7.7.2": + version "7.16.10" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.16.10.tgz#448f940defbe95b5a8029975b051f75993e8239f" + integrity sha512-yzuaYXoRJBGMlBhsMJoUW7G1UmSb/eXr/JHYM/MsOJgavJibLwASijW7oXBdw3NQ6T0bW7Ty5P/VarOs9cHmqw== dependencies: "@babel/code-frame" "^7.16.7" "@babel/generator" "^7.16.8" @@ -1218,7 +1218,7 @@ "@babel/helper-function-name" "^7.16.7" "@babel/helper-hoist-variables" "^7.16.7" "@babel/helper-split-export-declaration" "^7.16.7" - "@babel/parser" "^7.16.8" + "@babel/parser" "^7.16.10" "@babel/types" "^7.16.8" debug "^4.1.0" globals "^11.1.0" @@ -2340,9 +2340,9 @@ "@types/node" "*" "@types/node@*": - version "17.0.8" - resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.8.tgz#50d680c8a8a78fe30abe6906453b21ad8ab0ad7b" - integrity sha512-YofkM6fGv4gDJq78g4j0mMuGMkZVxZDgtU0JRdx6FgiJDG+0fY0GKVolOV8WqVmEhLCXkQRjwDdKyPxJp/uucg== + version "17.0.14" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.14.tgz#33b9b94f789a8fedd30a68efdbca4dbb06b61f20" + integrity sha512-SbjLmERksKOGzWzPNuW7fJM7fk3YXVTFiZWB/Hs99gwhk+/dnrQRPBQjPW9aO+fi1tAffi9PrwFvsmOKmDTyng== "@types/node@16.11.19": version "16.11.19" @@ -3209,9 +3209,9 @@ camelcase@^6.2.0: integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== caniuse-lite@^1.0.30001286: - version "1.0.30001299" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001299.tgz#d753bf6444ed401eb503cbbe17aa3e1451b5a68c" - integrity sha512-iujN4+x7QzqA2NCSrS5VUy+4gLmRd4xv6vbBBsmfVqTx8bLAD8097euLqQgKxSVLvxjSDcvF1T/i9ocgnUFexw== + version "1.0.30001304" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001304.tgz#38af55ed3fc8220cb13e35e6e7309c8c65a05559" + integrity sha512-bdsfZd6K6ap87AGqSHJP/s1V+U6Z5lyrcbBu3ovbCCf8cSYpwTtGrCBObMpJqwxfTbLW6YTIdbb1jEeTelcpYQ== cardinal@^2.1.1: version "2.1.1" @@ -3931,9 +3931,9 @@ editorconfig@0.15.3: sigmund "^1.0.1" electron-to-chromium@^1.4.17: - version "1.4.46" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.46.tgz#c88a6fedc766589826db0481602a888864ade1ca" - integrity sha512-UtV0xUA/dibCKKP2JMxOpDtXR74zABevuUEH4K0tvduFSIoxRVcYmQsbB51kXsFTX8MmOyWMt8tuZAlmDOqkrQ== + version "1.4.59" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.59.tgz#657f2588c048fb95975779f8fea101fad854de89" + integrity sha512-AOJ3cAE0TWxz4fQ9zkND5hWrQg16nsZKVz9INOot1oV//u4wWu5xrj9CQMmPTYskkZRunSRc9sAnr4EkexXokg== email-addresses@5.0.0: version "5.0.0" @@ -5198,7 +5198,7 @@ is-cidr@^4.0.2: dependencies: cidr-regex "^3.1.1" -is-core-module@^2.5.0, is-core-module@^2.8.0: +is-core-module@^2.5.0, is-core-module@^2.8.0, is-core-module@^2.8.1: version "2.8.1" resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211" integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA== @@ -7603,9 +7603,9 @@ pify@^3.0.0: integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY= pirates@^4.0.4: - version "4.0.4" - resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.4.tgz#07df81e61028e402735cdd49db701e4885b4e6e6" - integrity sha512-ZIrVPH+A52Dw84R0L3/VS9Op04PuQ2SEoJL6bkshmiTic/HldyW9Tf7oH5mhJZBK7NmDx27vSMrYEXPXclpDKw== + version "4.0.5" + resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.5.tgz#feec352ea5c3268fb23a37c702ab1699f35a5f3b" + integrity sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ== pkg-conf@^2.1.0: version "2.1.0" @@ -8072,7 +8072,7 @@ resolve.exports@^1.1.0: resolved "https://registry.yarnpkg.com/resolve.exports/-/resolve.exports-1.1.0.tgz#5ce842b94b05146c0e03076985d1d0e7e48c90c9" integrity sha512-J1l+Zxxp4XK3LUDZ9m60LRJF/mAe4z6a4xyabPHk7pvK5t35dACV32iIjJDFeWZFfZlO29w6SZ67knR0tHzJtQ== -resolve@^1.1.6, resolve@^1.10.0, resolve@^1.20.0: +resolve@^1.1.6, resolve@^1.10.0: version "1.21.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.21.0.tgz#b51adc97f3472e6a5cf4444d34bc9d6b9037591f" integrity sha512-3wCbTpk5WJlyE4mSOtDLhqQmGFi0/TD9VPwmiolnk8U0wRgMEktqCXd3vy5buTO3tljvalNvKrjHEfrd2WpEKA== @@ -8081,6 +8081,15 @@ resolve@^1.1.6, resolve@^1.10.0, resolve@^1.20.0: path-parse "^1.0.7" supports-preserve-symlinks-flag "^1.0.0" +resolve@^1.20.0: + version "1.22.0" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198" + integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw== + dependencies: + is-core-module "^2.8.1" + path-parse "^1.0.7" + supports-preserve-symlinks-flag "^1.0.0" + responselike@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/responselike/-/responselike-2.0.0.tgz#26391bcc3174f750f9a79eacc40a12a5c42d7723" From 09750a2d19ff162ef6404e602c5955c21a910ca1 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Wed, 2 Feb 2022 13:25:18 +0200 Subject: [PATCH 05/17] Applied pr changes --- lib/config/migration.spec.ts | 1 - lib/logger/index.spec.ts | 40 ++++++++++++++++++++++++------- lib/logger/index.ts | 7 ++---- lib/logger/utils.ts | 46 ++++++++++++++++++++---------------- tsconfig.json | 1 + tsconfig.spec.json | 5 ++-- 6 files changed, 61 insertions(+), 39 deletions(-) diff --git a/lib/config/migration.spec.ts b/lib/config/migration.spec.ts index b932ce68f75215..542fc7d70c3cbe 100644 --- a/lib/config/migration.spec.ts +++ b/lib/config/migration.spec.ts @@ -1,4 +1,3 @@ -import { expect } from '@jest/globals'; import { PlatformId } from '../constants'; import { getConfig } from './defaults'; import { GlobalConfig } from './global'; diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts index 0c66418ea2ce6f..1b88e309ae599c 100644 --- a/lib/logger/index.spec.ts +++ b/lib/logger/index.spec.ts @@ -2,7 +2,7 @@ import bunyan from 'bunyan'; import _fs from 'fs-extra'; import { add } from '../util/host-rules'; import { addSecretForSanitizing as addSecret } from '../util/sanitize'; -import { isValidLogLevel } from './utils'; +import { validateLogLevel } from './utils'; import { addMeta, addStream, @@ -186,13 +186,35 @@ describe('logger/index', () => { }); it('checks for valid log level', () => { - expect(isValidLogLevel('warning' as bunyan.LogLevel)).toBeFalsy(); - expect(isValidLogLevel('warn' as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel('trace' as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel(' ' as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel('' as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel(20 as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel(10 as bunyan.LogLevel)).toBeTruthy(); - expect(isValidLogLevel(100 as bunyan.LogLevel)).toBeFalsy(); + expect(validateLogLevel(undefined)).toBeTruthy(); + expect(validateLogLevel('')).toBeTruthy(); + expect(validateLogLevel(' ')).toBeTruthy(); + expect(validateLogLevel('warn')).toBeTruthy(); + expect(validateLogLevel('debug')).toBeTruthy(); + expect(validateLogLevel('trace')).toBeTruthy(); + expect(validateLogLevel('info' as bunyan.LogLevel)).toBeTruthy(); + expect(validateLogLevel(10)).toBeTruthy(); + // Mock when the function exits + const mockExit = jest + .spyOn(process, 'exit') + .mockImplementation((number) => { + throw new Error('process.exit: ' + number); + }); + expect(() => { + validateLogLevel('warning'); + }).toThrow(); + expect(mockExit).toHaveBeenCalledWith(1); + mockExit.mockRestore(); + + const mockExit2 = jest + .spyOn(process, 'exit') + .mockImplementation((number) => { + throw new Error('process.exit: ' + number); + }); + expect(() => { + validateLogLevel('100'); + }).toThrow(); + expect(mockExit2).toHaveBeenCalledWith(1); + mockExit2.mockRestore(); }); }); diff --git a/lib/logger/index.ts b/lib/logger/index.ts index 9c85b5d9db9c65..cad20d5022c244 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -6,17 +6,14 @@ import configSerializer from './config-serializer'; import errSerializer from './err-serializer'; import { RenovateStream } from './pretty-stdout'; import type { BunyanRecord, Logger } from './types'; -import { ProblemStream, isValidLogLevel, withSanitizer } from './utils'; +import { ProblemStream, validateLogLevel, withSanitizer } from './utils'; let logContext: string = process.env.LOG_CONTEXT ?? nanoid(); let curMeta: Record = {}; const problems = new ProblemStream(); -process.env.LOG_LEVEL.toLocaleLowerCase(); -if (!isValidLogLevel(process.env.LOG_LEVEL as bunyan.LogLevel)) { - process.exit(1); -} +validateLogLevel(process.env.LOG_LEVEL); const stdout: bunyan.Stream = { name: 'stdout', level: diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 169d479dcbf87b..8be19ef138bd77 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -207,9 +207,15 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream { throw new Error("Missing 'stream' or 'path' for bunyan stream"); } -export function isValidLogLevel( - logLevelToCheck: bunyan.LogLevel -): logLevelToCheck is bunyan.LogLevel { +/** + * A function that terminates exeution if the log level that was entered is + * not a valid value for the bunyan logger. + * @param logLevelToCheck + * @returns true when the logLevelToCheck is valid. Else it stops execution + */ +export function validateLogLevel( + logLevelToCheck: string | undefined | bunyan.LogLevel +) { const allowedValues: bunyan.LogLevel[] = [ 'trace', 'debug', @@ -225,26 +231,24 @@ export function isValidLogLevel( bunyan.FATAL, ]; if ( - typeof logLevelToCheck === 'string' && - logLevelToCheck.trim().length === 0 + !logLevelToCheck || + (typeof logLevelToCheck === 'string' && + logLevelToCheck.trim().length === 0) || + allowedValues.indexOf(logLevelToCheck as bunyan.LogLevel) !== -1 ) { - // if the log level is empty string then return true + // if the log level is empty string then return true. return true; } - const result: boolean = allowedValues.indexOf(logLevelToCheck) !== -1; - if (!result) { - const Logger = bunyan.createLogger({ - name: 'log level error log', - streams: [ - { - level: 'fatal', - stream: process.stdout, - }, - ], - }); - Logger.fatal(`${logLevelToCheck} is not a valid log level. terminating...`); - } - - return result; + 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); } diff --git a/tsconfig.json b/tsconfig.json index c00c0b323296c3..636b8bb24d90b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,7 @@ "checkJs": true }, "exclude": [ + "node_modules", "./.cache", "./dist", "**/__mocks__/*", diff --git a/tsconfig.spec.json b/tsconfig.spec.json index d0463cc8f9e8a2..101c5a0ad2f8fb 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -1,8 +1,7 @@ { "extends": "./tsconfig", "compilerOptions": { - "allowJs": true, - "checkJs": true, - "types": ["node", "jest", "jest-extended", "expect-more-jest"] + "allowJs": false, + "checkJs": false } } From 7621568512c89186e651164d3f8cab90d0e83a26 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Wed, 2 Feb 2022 15:56:07 +0200 Subject: [PATCH 06/17] Refactored test and function --- lib/logger/index.spec.ts | 35 --------------------------------- lib/logger/utils.spec.ts | 42 ++++++++++++++++++++++++++++++++++++++++ lib/logger/utils.ts | 8 ++++---- 3 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 lib/logger/utils.spec.ts diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts index 1b88e309ae599c..4a1173ebe1b4fa 100644 --- a/lib/logger/index.spec.ts +++ b/lib/logger/index.spec.ts @@ -1,8 +1,6 @@ -import bunyan from 'bunyan'; import _fs from 'fs-extra'; import { add } from '../util/host-rules'; import { addSecretForSanitizing as addSecret } from '../util/sanitize'; -import { validateLogLevel } from './utils'; import { addMeta, addStream, @@ -184,37 +182,4 @@ describe('logger/index', () => { expect(logged.someFn).toBe('[function]'); expect(logged.someObject.field).toBe('**redacted**'); }); - - it('checks for valid log level', () => { - expect(validateLogLevel(undefined)).toBeTruthy(); - expect(validateLogLevel('')).toBeTruthy(); - expect(validateLogLevel(' ')).toBeTruthy(); - expect(validateLogLevel('warn')).toBeTruthy(); - expect(validateLogLevel('debug')).toBeTruthy(); - expect(validateLogLevel('trace')).toBeTruthy(); - expect(validateLogLevel('info' as bunyan.LogLevel)).toBeTruthy(); - expect(validateLogLevel(10)).toBeTruthy(); - // Mock when the function exits - const mockExit = jest - .spyOn(process, 'exit') - .mockImplementation((number) => { - throw new Error('process.exit: ' + number); - }); - expect(() => { - validateLogLevel('warning'); - }).toThrow(); - expect(mockExit).toHaveBeenCalledWith(1); - mockExit.mockRestore(); - - const mockExit2 = jest - .spyOn(process, 'exit') - .mockImplementation((number) => { - throw new Error('process.exit: ' + number); - }); - expect(() => { - validateLogLevel('100'); - }).toThrow(); - expect(mockExit2).toHaveBeenCalledWith(1); - mockExit2.mockRestore(); - }); }); diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts new file mode 100644 index 00000000000000..9b6cc9fffcc913 --- /dev/null +++ b/lib/logger/utils.spec.ts @@ -0,0 +1,42 @@ +import bunyan from 'bunyan'; +import { validateLogLevel } from './utils'; + +describe('validate log level', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('checks for valid log levels', () => { + const lel = validateLogLevel(undefined); + console.log(lel); + expect(validateLogLevel(undefined)).toBe(undefined); + expect(validateLogLevel('')).toBe(undefined); + expect(validateLogLevel(' ')).toBe(undefined); + expect(validateLogLevel('warn')).toBe(undefined); + expect(validateLogLevel('debug')).toBe(undefined); + expect(validateLogLevel('trace')).toBe(undefined); + expect(validateLogLevel('info' as bunyan.LogLevel)).toBe(undefined); + expect(validateLogLevel(10)).toBe(undefined); + }); + + it('checks for invalid log levels', () => { + // Mock when the function exits + const mockExit = jest.spyOn(process, 'exit'); + mockExit.mockImplementationOnce((number) => { + throw new Error(`process.exit: ${number}`); + }); + expect(() => { + validateLogLevel('warning'); + }).toThrow(); + expect(mockExit).toHaveBeenCalledWith(1); + mockExit.mockClear(); + + mockExit.mockImplementationOnce((number) => { + throw new Error(`process.exit: ${number}`); + }); + expect(() => { + validateLogLevel('100'); + }).toThrow(); + expect(mockExit).toHaveBeenCalledWith(1); + }); +}); diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 8be19ef138bd77..57cce6aff6c92e 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -211,11 +211,11 @@ export function withSanitizer(streamConfig: bunyan.Stream): 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 true when the logLevelToCheck is valid. Else it stops execution + * @returns returns when the logLevelToCheck is valid. Else it stops execution. */ export function validateLogLevel( logLevelToCheck: string | undefined | bunyan.LogLevel -) { +): void { const allowedValues: bunyan.LogLevel[] = [ 'trace', 'debug', @@ -236,8 +236,8 @@ export function validateLogLevel( logLevelToCheck.trim().length === 0) || allowedValues.indexOf(logLevelToCheck as bunyan.LogLevel) !== -1 ) { - // if the log level is empty string then return true. - return true; + // if the log level is empty string then return. + return; } const logger = bunyan.createLogger({ From 54a26d8735f17f5a1c8321d6969d5b0fd01f287b Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Wed, 2 Feb 2022 16:14:18 +0200 Subject: [PATCH 07/17] Fixed test and added documnetation --- lib/logger/utils.spec.ts | 16 ++++++++-------- lib/logger/utils.ts | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 9b6cc9fffcc913..37ca735467933e 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -9,14 +9,14 @@ describe('validate log level', () => { it('checks for valid log levels', () => { const lel = validateLogLevel(undefined); console.log(lel); - expect(validateLogLevel(undefined)).toBe(undefined); - expect(validateLogLevel('')).toBe(undefined); - expect(validateLogLevel(' ')).toBe(undefined); - expect(validateLogLevel('warn')).toBe(undefined); - expect(validateLogLevel('debug')).toBe(undefined); - expect(validateLogLevel('trace')).toBe(undefined); - expect(validateLogLevel('info' as bunyan.LogLevel)).toBe(undefined); - expect(validateLogLevel(10)).toBe(undefined); + expect(validateLogLevel(undefined)).toBeUndefined(); + expect(validateLogLevel('')).toBeUndefined(); + expect(validateLogLevel(' ')).toBeUndefined(); + expect(validateLogLevel('warn')).toBeUndefined(); + expect(validateLogLevel('debug')).toBeUndefined(); + expect(validateLogLevel('trace')).toBeUndefined(); + expect(validateLogLevel('info' as bunyan.LogLevel)).toBeUndefined(); + expect(validateLogLevel(10)).toBeUndefined(); }); it('checks for invalid log levels', () => { diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 57cce6aff6c92e..aa9ca368e1e95c 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -211,7 +211,7 @@ export function withSanitizer(streamConfig: bunyan.Stream): 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 when the logLevelToCheck is valid. Else it stops execution. + * @returns returns undefined when the logLevelToCheck is valid. Else it stops execution. */ export function validateLogLevel( logLevelToCheck: string | undefined | bunyan.LogLevel From 5c8e269a44f0f9a29f2f1fdb84645893ec7d571d Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Wed, 2 Feb 2022 16:26:19 +0200 Subject: [PATCH 08/17] test suite name as requested --- lib/logger/utils.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 37ca735467933e..28a5554571c894 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -1,7 +1,7 @@ import bunyan from 'bunyan'; import { validateLogLevel } from './utils'; -describe('validate log level', () => { +describe('logger/utils', () => { afterEach(() => { jest.restoreAllMocks(); }); From 054a3d97360a84b351880b08a138497c3ffc2450 Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Wed, 2 Feb 2022 16:27:59 +0200 Subject: [PATCH 09/17] removed my log --- lib/logger/utils.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 28a5554571c894..a4fbb76bdb8e32 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -7,8 +7,6 @@ describe('logger/utils', () => { }); it('checks for valid log levels', () => { - const lel = validateLogLevel(undefined); - console.log(lel); expect(validateLogLevel(undefined)).toBeUndefined(); expect(validateLogLevel('')).toBeUndefined(); expect(validateLogLevel(' ')).toBeUndefined(); From b633bc10bff49d8b7c92332d63bad3435a6e66f2 Mon Sep 17 00:00:00 2001 From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com> Date: Thu, 3 Feb 2022 10:04:59 +0200 Subject: [PATCH 10/17] Update lib/logger/utils.spec.ts Co-authored-by: Michael Kriese --- lib/logger/utils.spec.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index a4fbb76bdb8e32..319a317076a0b0 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -17,23 +17,18 @@ describe('logger/utils', () => { expect(validateLogLevel(10)).toBeUndefined(); }); - it('checks for invalid log levels', () => { + 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('warning'); - }).toThrow(); - expect(mockExit).toHaveBeenCalledWith(1); - mockExit.mockClear(); - - mockExit.mockImplementationOnce((number) => { - throw new Error(`process.exit: ${number}`); - }); - expect(() => { - validateLogLevel('100'); + validateLogLevel(input); }).toThrow(); expect(mockExit).toHaveBeenCalledWith(1); }); From 4e5bd8ef7a486f4f1a65a49b56cd0deea7c0734a Mon Sep 17 00:00:00 2001 From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com> Date: Thu, 3 Feb 2022 10:07:56 +0200 Subject: [PATCH 11/17] Update lib/logger/utils.ts Co-authored-by: Michael Kriese --- lib/logger/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index aa9ca368e1e95c..960c407d89eb6b 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -234,7 +234,7 @@ export function validateLogLevel( !logLevelToCheck || (typeof logLevelToCheck === 'string' && logLevelToCheck.trim().length === 0) || - allowedValues.indexOf(logLevelToCheck as bunyan.LogLevel) !== -1 + !allowedValues.includes(logLevelToCheck as bunyan.LogLevel) ) { // if the log level is empty string then return. return; From b0f0f0cca9cf9aaff7fbc965983761317938ce1d Mon Sep 17 00:00:00 2001 From: Hasan Awad Date: Thu, 3 Feb 2022 10:47:15 +0200 Subject: [PATCH 12/17] Refactored and removed unnecessary values --- lib/logger/utils.spec.ts | 2 +- lib/logger/utils.ts | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 319a317076a0b0..a89add7d21d9b0 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -13,8 +13,8 @@ describe('logger/utils', () => { expect(validateLogLevel('warn')).toBeUndefined(); expect(validateLogLevel('debug')).toBeUndefined(); expect(validateLogLevel('trace')).toBeUndefined(); + expect(validateLogLevel('TRACE')).toBeUndefined(); expect(validateLogLevel('info' as bunyan.LogLevel)).toBeUndefined(); - expect(validateLogLevel(10)).toBeUndefined(); }); it.each` diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 960c407d89eb6b..8efa62822a6cac 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -223,22 +223,19 @@ export function validateLogLevel( 'warn', 'error', 'fatal', - bunyan.TRACE, - bunyan.DEBUG, - bunyan.INFO, - bunyan.WARN, - bunyan.ERROR, - bunyan.FATAL, ]; - if ( - !logLevelToCheck || - (typeof logLevelToCheck === 'string' && - logLevelToCheck.trim().length === 0) || - !allowedValues.includes(logLevelToCheck as bunyan.LogLevel) - ) { - // if the log level is empty string then return. + if (!logLevelToCheck) { return; } + if (typeof logLevelToCheck === 'string') { + logLevelToCheck = logLevelToCheck.toLocaleLowerCase(); + if ( + allowedValues.includes(logLevelToCheck as bunyan.LogLevel) || + logLevelToCheck.trim().length === 0 + ) { + return; + } + } const logger = bunyan.createLogger({ name: 'renovate', From ec6900f404a03556cb15bcfbec2ab9ace2e3ac09 Mon Sep 17 00:00:00 2001 From: hasanwhitesource Date: Thu, 3 Feb 2022 13:03:21 +0200 Subject: [PATCH 13/17] Added test to new changes --- lib/logger/utils.spec.ts | 4 ++-- lib/logger/utils.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index a89add7d21d9b0..09bea8a46339c6 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -8,8 +8,6 @@ describe('logger/utils', () => { it('checks for valid log levels', () => { expect(validateLogLevel(undefined)).toBeUndefined(); - expect(validateLogLevel('')).toBeUndefined(); - expect(validateLogLevel(' ')).toBeUndefined(); expect(validateLogLevel('warn')).toBeUndefined(); expect(validateLogLevel('debug')).toBeUndefined(); expect(validateLogLevel('trace')).toBeUndefined(); @@ -21,6 +19,8 @@ describe('logger/utils', () => { input ${'warning'} ${'100'} + ${''} + ${' '} `('checks for invalid log level: $input', (input) => { // Mock when the function exits const mockExit = jest.spyOn(process, 'exit'); diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 8efa62822a6cac..ba3ee78c1ae7a3 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -225,14 +225,14 @@ export function validateLogLevel( 'fatal', ]; if (!logLevelToCheck) { + // when logLevel is undefined return; } if (typeof logLevelToCheck === 'string') { + logLevelToCheck = logLevelToCheck.trim(); logLevelToCheck = logLevelToCheck.toLocaleLowerCase(); - if ( - allowedValues.includes(logLevelToCheck as bunyan.LogLevel) || - logLevelToCheck.trim().length === 0 - ) { + if (allowedValues.includes(logLevelToCheck as bunyan.LogLevel)) { + // log level is in the allowed values return; } } From 65d25f0ec0c23cf4196abb40ce495925f44d295a Mon Sep 17 00:00:00 2001 From: hasanwhitesource Date: Thu, 3 Feb 2022 16:58:00 +0200 Subject: [PATCH 14/17] Refactored and added logic in index --- lib/logger/index.ts | 4 ++++ lib/logger/utils.spec.ts | 1 - lib/logger/utils.ts | 16 ++++++---------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/logger/index.ts b/lib/logger/index.ts index cad20d5022c244..27e5d265d1916b 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -13,6 +13,10 @@ let curMeta: Record = {}; const problems = new ProblemStream(); +if (is.string(process.env.LOG_LEVEL)) { + process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase(); + process.env.LOG_LEVEL = process.env.LOG_LEVEL.trim(); +} validateLogLevel(process.env.LOG_LEVEL); const stdout: bunyan.Stream = { name: 'stdout', diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 09bea8a46339c6..12067033848bdd 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -11,7 +11,6 @@ describe('logger/utils', () => { expect(validateLogLevel('warn')).toBeUndefined(); expect(validateLogLevel('debug')).toBeUndefined(); expect(validateLogLevel('trace')).toBeUndefined(); - expect(validateLogLevel('TRACE')).toBeUndefined(); expect(validateLogLevel('info' as bunyan.LogLevel)).toBeUndefined(); }); diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index ba3ee78c1ae7a3..919144ccdee2ad 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -224,18 +224,14 @@ export function validateLogLevel( 'error', 'fatal', ]; - if (!logLevelToCheck) { - // when logLevel is undefined + if ( + is.undefined(logLevelToCheck) || + (is.string(logLevelToCheck) && + allowedValues.includes(logLevelToCheck as bunyan.LogLevel)) + ) { + // log level is in the allowed values or its undefined return; } - if (typeof logLevelToCheck === 'string') { - logLevelToCheck = logLevelToCheck.trim(); - logLevelToCheck = logLevelToCheck.toLocaleLowerCase(); - if (allowedValues.includes(logLevelToCheck as bunyan.LogLevel)) { - // log level is in the allowed values - return; - } - } const logger = bunyan.createLogger({ name: 'renovate', From 1848db3f5529aa9d7c8d3af53edc172940fdd5af Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Fri, 4 Feb 2022 11:14:54 +0100 Subject: [PATCH 15/17] Update lib/logger/utils.ts Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> --- lib/logger/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 919144ccdee2ad..442b8bea828ec2 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -209,7 +209,7 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream { /** * A function that terminates exeution if the log level that was entered is - * not a valid value for the bunyan logger. + * not a valid value for the Bunyan logger. * @param logLevelToCheck * @returns returns undefined when the logLevelToCheck is valid. Else it stops execution. */ From 838d09958835d9772136952eedbed54273cd3030 Mon Sep 17 00:00:00 2001 From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com> Date: Fri, 4 Feb 2022 13:48:25 +0200 Subject: [PATCH 16/17] Update lib/logger/index.ts Also trim Co-authored-by: Rhys Arkins --- lib/logger/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/logger/index.ts b/lib/logger/index.ts index 27e5d265d1916b..ceee032d872cf8 100644 --- a/lib/logger/index.ts +++ b/lib/logger/index.ts @@ -14,8 +14,7 @@ let curMeta: Record = {}; const problems = new ProblemStream(); if (is.string(process.env.LOG_LEVEL)) { - process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase(); - process.env.LOG_LEVEL = process.env.LOG_LEVEL.trim(); + process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase().trim(); } validateLogLevel(process.env.LOG_LEVEL); const stdout: bunyan.Stream = { From 3a18a91ad477e1b2a46423128b74044ccd98e699 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Fri, 4 Feb 2022 13:08:41 +0100 Subject: [PATCH 17/17] simplify types --- lib/logger/utils.spec.ts | 3 +-- lib/logger/utils.ts | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/logger/utils.spec.ts b/lib/logger/utils.spec.ts index 12067033848bdd..ad2af427f99881 100644 --- a/lib/logger/utils.spec.ts +++ b/lib/logger/utils.spec.ts @@ -1,4 +1,3 @@ -import bunyan from 'bunyan'; import { validateLogLevel } from './utils'; describe('logger/utils', () => { @@ -11,7 +10,7 @@ describe('logger/utils', () => { expect(validateLogLevel('warn')).toBeUndefined(); expect(validateLogLevel('debug')).toBeUndefined(); expect(validateLogLevel('trace')).toBeUndefined(); - expect(validateLogLevel('info' as bunyan.LogLevel)).toBeUndefined(); + expect(validateLogLevel('info')).toBeUndefined(); }); it.each` diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts index 442b8bea828ec2..8d290268d02e6c 100644 --- a/lib/logger/utils.ts +++ b/lib/logger/utils.ts @@ -213,9 +213,7 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream { * @param logLevelToCheck * @returns returns undefined when the logLevelToCheck is valid. Else it stops execution. */ -export function validateLogLevel( - logLevelToCheck: string | undefined | bunyan.LogLevel -): void { +export function validateLogLevel(logLevelToCheck: string | undefined): void { const allowedValues: bunyan.LogLevel[] = [ 'trace', 'debug',