Skip to content

Commit

Permalink
refactor(logger): Update logger internals (#12842)
Browse files Browse the repository at this point in the history
* refactor(logger): Update logger internals

* Check the entire logger directory

* Refactor sanitizeValue function

* Backport changes

* Backport test too
  • Loading branch information
zharinov committed Nov 27, 2021
1 parent fb8715c commit e7a9690
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
20 changes: 11 additions & 9 deletions lib/logger/config-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export default function configSerializer(
const arrayFields = ['packageFiles', 'upgrades'];

return traverse(config).map(function scrub(val: string) {
if (val && templateFields.includes(this.key)) {
this.update('[Template]');
}
if (val && contentFields.includes(this.key)) {
this.update('[content]');
}
// istanbul ignore if
if (val && arrayFields.includes(this.key)) {
this.update('[Array]');
if (this.key && val) {
if (templateFields.includes(this.key)) {
this.update('[Template]');
}
if (contentFields.includes(this.key)) {
this.update('[content]');
}
// istanbul ignore if
if (arrayFields.includes(this.key)) {
this.update('[Array]');
}
}
});
}
14 changes: 7 additions & 7 deletions lib/logger/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ describe('logger/index', () => {
});

it('supports file-based logging', () => {
let chunk = null;
let chunk = '';
fs.createWriteStream.mockReturnValueOnce({
writable: true,
write(x) {
write(x: string) {
chunk = x;
},
});
Expand All @@ -112,10 +112,10 @@ describe('logger/index', () => {
});

it('handles cycles', () => {
let logged = null;
let logged: Record<string, any> = {};
fs.createWriteStream.mockReturnValueOnce({
writable: true,
write(x) {
write(x: string) {
logged = JSON.parse(x);
},
});
Expand All @@ -126,7 +126,7 @@ describe('logger/index', () => {
level: 'error',
});

const meta = { foo: null, bar: [] };
const meta: Record<string, any> = { foo: null, bar: [] };
meta.foo = meta;
meta.bar.push(meta);
logger.error(meta, 'foo');
Expand All @@ -137,10 +137,10 @@ describe('logger/index', () => {
});

it('sanitizes secrets', () => {
let logged = null;
let logged: Record<string, any> = {};
fs.createWriteStream.mockReturnValueOnce({
writable: true,
write(x) {
write(x: string) {
logged = JSON.parse(x);
},
});
Expand Down
2 changes: 1 addition & 1 deletion lib/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { BunyanRecord, Logger } from './types';
import { ProblemStream, withSanitizer } from './utils';

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

const problems = new ProblemStream();

Expand Down
6 changes: 5 additions & 1 deletion lib/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ export interface BunyanRecord extends Record<string, any> {

export type BunyanStream = (NodeJS.WritableStream | Stream) & {
writable?: boolean;
write: (chunk: BunyanRecord, enc, cb) => void;
write: (
chunk: BunyanRecord,
enc: BufferEncoding,
cb: (err?: Error | null) => void
) => void;
};
13 changes: 9 additions & 4 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ export default function prepareError(err: Error): Record<string, unknown> {
};
response.options = options;

for (const k of ['username', 'password', 'method', 'http2']) {
options[k] = err.options[k];
}
options.username = err.options.username;
options.password = err.options.password;
options.method = err.options.method;
options.http2 = err.options.http2;

// istanbul ignore else
if (err.response) {
Expand Down Expand Up @@ -174,7 +175,11 @@ export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream {

const stream = streamConfig.stream as BunyanStream;
if (stream?.writable) {
const write = (chunk: BunyanRecord, enc, cb): void => {
const write = (
chunk: BunyanRecord,
enc: BufferEncoding,
cb: (err?: Error | null) => void
): void => {
const raw = sanitizeValue(chunk);
const result =
streamConfig.type === 'raw'
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
"./lib/globals.d.ts",
"./lib/logger/__mocks__/index.ts",
"./lib/logger/cmd-serializer.ts",
"./lib/logger/config-serializer.ts",
"./lib/logger/err-serializer.ts",
"./lib/logger/index.ts",
"./lib/logger/pretty-stdout.ts",
"./lib/logger/types.ts",
"./lib/logger/utils.ts",
"./lib/manager/argocd/types.ts",
"./lib/manager/azure-pipelines/types.ts",
"./lib/manager/bazel/types.ts",
Expand Down

0 comments on commit e7a9690

Please sign in to comment.