A logger
via GitHub
yarn add git+https://github.com/jamiller619/logger.git
I'm pretty sure this won't work in yarn berry, FYI.
import logger from 'logger'
const log = logger('something.namespace')
log.info('message', 'another one?', {
even: 'an object works',
})
log.error(
'Mundane detail',
new Error('I must have put a decimal in the wrong place. I always do that!')
)
// At some point, need to call init. All previous logs have been queued so nothing is lost.
logger.init('/path/to/my.log').then(() => {
// ...blah
})
// or
await logger.init('/path/to/my.log')
Which produces:
2023.05.21 10:25:38 PM INFO [something.namespace] (34504): message another one? {
even: "an object works"
} +0ms
2023.05.21 10:25:38 PM ERROR [something.namespace] (34504): Mundane detail {
name: "Error",
message: "I must have put a decimal in the wrong place. I always do that!",
stack: ""
} +0ms
The init
function accepts two parameters, the path to the
log file, and an options object. Any options passed into
init
are merged with defaults and passed directly to winston.
The first argument to all log calls is the message
parameter, which must be a string. All other parameters can
be anything, and will hopefully be serialized correctly
for displaying in a console
and/or file. The only exception to this is log.error
,
which accepts two parameters where the second parameter is
an Error
object.