From 939b12c13200bb9c017648c3cfc4b3c6d79347c1 Mon Sep 17 00:00:00 2001 From: Valeriy Sidorenko Date: Mon, 28 Aug 2023 19:19:57 +0200 Subject: [PATCH] feat(logging): add option to control logging level --- src/lib/logging.ts | 12 +++++++++--- src/nodekit.ts | 2 ++ src/types.ts | 4 +++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib/logging.ts b/src/lib/logging.ts index 0d9b57d..3b22fb2 100644 --- a/src/lib/logging.ts +++ b/src/lib/logging.ts @@ -1,12 +1,18 @@ import pino from 'pino'; +/** + * workaround to provide IntelliSense hints https://stackoverflow.com/a/61048124 + */ +export type LoggingLevel = pino.LevelWithSilent | (string & {}); + interface InitLoggerOptions { appName: string; devMode: boolean; destination?: pino.DestinationStream; + level?: LoggingLevel; } -export function initLogger({appName, devMode, destination}: InitLoggerOptions) { +export function initLogger({appName, devMode, destination, level = 'debug'}: InitLoggerOptions) { const transportConfig = devMode ? { target: 'pino-pretty', @@ -18,10 +24,10 @@ export function initLogger({appName, devMode, destination}: InitLoggerOptions) { } : undefined; - const options = { + const options: pino.LoggerOptions = { name: appName, safe: true, - leve: 'debug', + level, serializers: { error: pino.stdSerializers.err, }, diff --git a/src/nodekit.ts b/src/nodekit.ts index 8d1ba5c..58e1e2a 100644 --- a/src/nodekit.ts +++ b/src/nodekit.ts @@ -61,12 +61,14 @@ export class NodeKit { appInstallation, appEnv, appDevMode, + appLoggingLevel: process.env.APP_LOGGING_LEVEL || fileConfig.appLoggingLevel, }); this.logger = initLogger({ appName: this.config.appName as string, devMode: appDevMode, destination: this.config.appLoggingDestination, + level: this.config.appLoggingLevel, }); const redactSensitiveQueryParams = prepareSensitiveQueryParamsRedacter( diff --git a/src/types.ts b/src/types.ts index 1f52496..f6f7b3f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ -import {pino} from 'pino'; +import type {pino} from 'pino'; +import type {LoggingLevel} from './lib/logging'; export interface AppConfig { appName?: string; @@ -20,6 +21,7 @@ export interface AppConfig { appSensitiveQueryParams?: string[]; appLoggingDestination?: pino.DestinationStream; + appLoggingLevel?: LoggingLevel; appTracingEnabled?: boolean; appTracingServiceName?: string;