Skip to content

Instantly share code, notes, and snippets.

@ralexandr
Created June 5, 2020 21:56
Show Gist options
  • Save ralexandr/89cafc3ba60d25d8c651eb2ad32bcaba to your computer and use it in GitHub Desktop.
Save ralexandr/89cafc3ba60d25d8c651eb2ad32bcaba to your computer and use it in GitHub Desktop.
Logger with custom formatting
import pino, { Logger } from 'pino';
function logMethod(args: any, method: any) {
if (args.length === 2) {
let interpolationValue;
switch (typeof args[1]) {
case 'object':
interpolationValue = ' %j';
break;
case 'string':
interpolationValue = ' %s';
break;
case 'number':
interpolationValue = ' %d';
break;
default:
}
args[0] = `${args[0]}${interpolationValue}`;
}
// @ts-ignore
method.apply(this, args);
}
export const logger = pino({
// Set to null to avoid adding pid, hostname and name properties to each log.
base: null,
prettyPrint: true,
// @ts-ignore
hooks: { logMethod },
});
export function getLogger() {
return logger;
}
// @ts-ignore
export { Logger };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment