Created
June 5, 2020 21:56
-
-
Save ralexandr/89cafc3ba60d25d8c651eb2ad32bcaba to your computer and use it in GitHub Desktop.
Logger with custom formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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