From e7a969016d4a00f77f677713c547c33f199936ba Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 27 Nov 2021 12:22:58 +0300
Subject: [PATCH] refactor(logger): Update logger internals (#12842)

* refactor(logger): Update logger internals

* Check the entire logger directory

* Refactor sanitizeValue function

* Backport changes

* Backport test too
---
 lib/logger/config-serializer.ts | 20 +++++++++++---------
 lib/logger/index.spec.ts        | 14 +++++++-------
 lib/logger/index.ts             |  2 +-
 lib/logger/types.ts             |  6 +++++-
 lib/logger/utils.ts             | 13 +++++++++----
 tsconfig.strict.json            |  6 ++++++
 6 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/lib/logger/config-serializer.ts b/lib/logger/config-serializer.ts
index 240cbd2f73..cc54936e58 100644
--- a/lib/logger/config-serializer.ts
+++ b/lib/logger/config-serializer.ts
@@ -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]');
+      }
     }
   });
 }
diff --git a/lib/logger/index.spec.ts b/lib/logger/index.spec.ts
index d55c13a68b..3a3e109663 100644
--- a/lib/logger/index.spec.ts
+++ b/lib/logger/index.spec.ts
@@ -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;
       },
     });
@@ -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);
       },
     });
@@ -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');
@@ -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);
       },
     });
diff --git a/lib/logger/index.ts b/lib/logger/index.ts
index 5dbd16ff66..66f91378fd 100644
--- a/lib/logger/index.ts
+++ b/lib/logger/index.ts
@@ -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();
 
diff --git a/lib/logger/types.ts b/lib/logger/types.ts
index a7d081e0a2..a4fa07fe2d 100644
--- a/lib/logger/types.ts
+++ b/lib/logger/types.ts
@@ -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;
 };
diff --git a/lib/logger/utils.ts b/lib/logger/utils.ts
index ab00f9746e..0bb5ffdaf3 100644
--- a/lib/logger/utils.ts
+++ b/lib/logger/utils.ts
@@ -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) {
@@ -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'
diff --git a/tsconfig.strict.json b/tsconfig.strict.json
index dbadb581e3..2f67feec4c 100644
--- a/tsconfig.strict.json
+++ b/tsconfig.strict.json
@@ -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",
-- 
GitLab