Skip to content

Commit

Permalink
feat(logging): add option to control logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed Aug 28, 2023
1 parent 2c01f39 commit 939b12c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
},
Expand Down
2 changes: 2 additions & 0 deletions src/nodekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {pino} from 'pino';
import type {pino} from 'pino';
import type {LoggingLevel} from './lib/logging';

export interface AppConfig {
appName?: string;
Expand All @@ -20,6 +21,7 @@ export interface AppConfig {
appSensitiveQueryParams?: string[];

appLoggingDestination?: pino.DestinationStream;
appLoggingLevel?: LoggingLevel;

appTracingEnabled?: boolean;
appTracingServiceName?: string;
Expand Down

0 comments on commit 939b12c

Please sign in to comment.