/ *
A simple logging helper that uses the ` pino ` logging system . Pino is actually
not very good but it works well enough . My main complaint is it seems Pino ' s
speed comes from doing _zero _ string interpolation on its own . You have to
craft all of the strings yourself , which is probably the majority of the processing
time in logging , so Pino is effectively "cheating" by claiming it ' s fast but
not doing anything . If you ever want to beat Pino just don ' t log anything too .
Fastest log is no logs at all .
* /
import pino from 'pino' ;
import pinoPretty from 'pino-pretty' ;
import path from 'path' ;
const log _level = process . env . PROD === undefined ? "debug" : "info" ;
/ *
Base log connector using ` pino ` logging . You can use this but
it ' s probably better to use the ` create ` method below so you can
specify the file this log came from .
* /
export const log = pino ( {
level : log _level ,
prettyPrint : {
levelFirst : true ,
colorize : true ,
singleLine : true ,
ignore : "module" ,
messageFormat : "{levelLabel}[{pid}] {module}: {msg}"
} ,
prettifier : pinoPretty
} ) ;
/ *
This will create a ` pino ` logger with the name of the file in the log messages . Incredibly
useful when you ' re trying to find out where something happened . To make this work you need
to do the following :
` ` ` javascript
const log = logging . create ( import . meta . url ) ;
` ` `
The ` import.meta.url ` is how you get a the current file in ESM style modules . In older
` node ` you ' d use so the called "magic" variable ` __file ` . I guess if you put ` __ ` in a
variable name it ' s considered magic , but if you attach ` meta.url ` to a keyword like ` import `
and then also make that keyword a function magically then it ' s definitely not magic and
suprior to a simple variable everyone knows .
* /
export const create = ( import _url ) => {
const pd = path . parse ( import _url ) ;
const log _line = ` ${ path . basename ( pd . dir ) } / ${ pd . base } ` ;
return log . child ( { module : log _line } ) ;
}
export default { create , log } ;