Skip to content

Commit

Permalink
fix: Don't create logger on each log call, so that frameworks that ch…
Browse files Browse the repository at this point in the history
…eck for imports after teardown (jest) don't throw errors deep inside pino
  • Loading branch information
TimothyJones committed Sep 6, 2021
1 parent 3d2d19b commit c5cf077
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ const pkg = require('../../package.json');

const logContext = `pact-core@${pkg.version}`;
let currentLogLevel: LogLevel = 'info';
let logger = createLogger(currentLogLevel);

export const setLogLevel = (level: LogLevel = 'info'): void => {
currentLogLevel = level;
logger = createLogger(currentLogLevel);
};

export const verboseIsImplied = (): boolean =>
currentLogLevel === 'trace' || currentLogLevel === 'debug';

const addContext = (context: string, message: string) =>
`${context}: ${message}`;

export default {
pactCrash: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).error(pactCrashMessage(message)),
logger.error(addContext(context, pactCrashMessage(message))),
error: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).error(message),
logger.error(addContext(context, message)),
warn: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).warn(message),
logger.warn(addContext(context, message)),
info: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).info(message),
logger.info(addContext(context, message)),
debug: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).debug(message),
logger.debug(addContext(context, message)),
trace: (message: string, context: string = logContext): void =>
createLogger(currentLogLevel, context).trace(message),
logger.trace(addContext(context, message)),
};
3 changes: 1 addition & 2 deletions src/logger/pino.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import pino = require('pino');
import { LogLevel } from './types';

export const createLogger = (level: LogLevel, context: string): pino.Logger =>
export const createLogger = (level: LogLevel): pino.Logger =>
pino({
level: level.toLowerCase(),
prettyPrint: {
messageFormat: `${context}: {msg}`,
translateTime: true,
},
});

0 comments on commit c5cf077

Please sign in to comment.