Skip to content

Commit

Permalink
feat(logger): prefix log env settings with RENOVATE_ (#32499)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
viceice and rarkins authored Nov 13, 2024
1 parent 894250d commit 42b448b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/logger/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getContext,
getProblems,
levels,
logLevel,
logger,
removeMeta,
setContext,
Expand Down Expand Up @@ -53,7 +54,9 @@ describe('logger/index', () => {
});

it('sets level', () => {
expect(logLevel()).toBeDefined(); // depends on passed env
expect(() => levels('stdout', 'debug')).not.toThrow();
expect(logLevel()).toBe('debug');
});

it('saves problems', () => {
Expand Down
48 changes: 33 additions & 15 deletions lib/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ import { once, reset as onceReset } from './once';
import { RenovateStream } from './pretty-stdout';
import { getRemappedLevel } from './remap';
import type { BunyanRecord, Logger } from './types';
import { ProblemStream, validateLogLevel, withSanitizer } from './utils';

let logContext: string = process.env.LOG_CONTEXT ?? nanoid();
import {
ProblemStream,
getEnv,
validateLogLevel,
withSanitizer,
} from './utils';

let logContext: string = getEnv('LOG_CONTEXT') ?? nanoid();
let curMeta: Record<string, unknown> = {};

const problems = new ProblemStream();

// istanbul ignore if: not easily testable
if (is.string(process.env.LOG_LEVEL)) {
process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase().trim();
}

let stdoutLevel = validateLogLevel(getEnv('LOG_LEVEL'), 'info');
const stdout: bunyan.Stream = {
name: 'stdout',
level: validateLogLevel(process.env.LOG_LEVEL, 'info'),
level: stdoutLevel,
stream: process.stdout,
};

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

// istanbul ignore if: not testable
if (process.env.LOG_FORMAT !== 'json') {
if (getEnv('LOG_FORMAT') !== 'json') {
// TODO: typings (#9615)
const prettyStdOut = new RenovateStream() as any;
prettyStdOut.pipe(process.stdout);
Expand Down Expand Up @@ -122,16 +127,17 @@ loggerLevels.forEach((loggerLevel) => {
logger.once[loggerLevel] = logOnceFn as never;
});

const logFile = getEnv('LOG_FILE');
// istanbul ignore if: not easily testable
if (is.string(process.env.LOG_FILE)) {
if (is.string(logFile)) {
// ensure log file directory exists
const directoryName = upath.dirname(process.env.LOG_FILE);
const directoryName = upath.dirname(logFile);
fs.ensureDirSync(directoryName);

addStream({
name: 'logfile',
path: process.env.LOG_FILE,
level: validateLogLevel(process.env.LOG_FILE_LEVEL, 'debug'),
path: logFile,
level: validateLogLevel(getEnv('LOG_FILE_LEVEL'), 'debug'),
});
}

Expand Down Expand Up @@ -168,8 +174,20 @@ export /* istanbul ignore next */ function addStream(
bunyanLogger.addStream(withSanitizer(stream));
}

export function levels(name: string, level: bunyan.LogLevel): void {
/**
* For testing purposes only
* @param name stream name
* @param level log level
* @private
*/
export function levels(
name: 'stdout' | 'logfile',
level: bunyan.LogLevelString,
): void {
bunyanLogger.levels(name, level);
if (name === 'stdout') {
stdoutLevel = level;
}
}

export function getProblems(): BunyanRecord[] {
Expand Down
6 changes: 6 additions & 0 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,9 @@ export function sanitizeUrls(text: string): string {
})
.replace(dataUriCredRe, '$1**redacted**');
}

export function getEnv(key: string): string | undefined {
return [process.env[`RENOVATE_${key}`], process.env[key]]
.map((v) => v?.toLowerCase().trim())
.find(is.nonEmptyStringAndNotWhitespace);
}
1 change: 1 addition & 0 deletions lib/workers/global/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const initPlatform = jest.spyOn(platform, 'initPlatform');
describe('workers/global/index', () => {
beforeEach(() => {
logger.getProblems.mockImplementation(() => []);
logger.logLevel.mockImplementation(() => 'info');
initPlatform.mockImplementation((input) => Promise.resolve(input));
delete process.env.AWS_SECRET_ACCESS_KEY;
delete process.env.AWS_SESSION_TOKEN;
Expand Down
7 changes: 3 additions & 4 deletions lib/workers/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
import { pkg } from '../../expose.cjs';
import { instrument } from '../../instrumentation';
import { exportStats, finalizeReport } from '../../instrumentation/reporting';
import { getProblems, logger, setMeta } from '../../logger';
import { getProblems, logLevel, logger, setMeta } from '../../logger';
import { setGlobalLogLevelRemaps } from '../../logger/remap';
import * as hostRules from '../../util/host-rules';
import * as queue from '../../util/http/queue';
Expand Down Expand Up @@ -228,10 +228,9 @@ export async function start(): Promise<number> {
}
} finally {
await globalFinalize(config!);
const logLevel = process.env.LOG_LEVEL ?? 'info';
if (logLevel === 'info') {
if (logLevel() === 'info') {
logger.info(
`Renovate was run at log level "${logLevel}". Set LOG_LEVEL=debug in environment variables to see extended debug logs.`,
`Renovate was run at log level "${logLevel()}". Set LOG_LEVEL=debug in environment variables to see extended debug logs.`,
);
}
}
Expand Down

0 comments on commit 42b448b

Please sign in to comment.