A tiny structured logging utility that includes, customizable transports, metadata.
Install the @lickle/log
library using your preferred package manager:
npm install @lickle/log
The default logger uses a console-based structured logger transport. Here's how to use it:
import log from '@lickle/log'
log.info`initialised`
// Output: { level: 'info', time: '...', msg: 'initialised' }
log.info({ userId: '...' })`user authenticated`
// Output: { level: 'info', time: '...', msg: 'user authenticated', meta: { userId: '...' } }
log.info('started tast')
// Output: { level: 'info', time: '...', msg: 'started tast' }
log.error(new Error('task failed'))
// Output: { level: 'error', time: '...', msg: 'task failed', meta: { stack: '...' } }
The logger's metadata is stored in the meta property. You can update it directly, and any additional metadata passed to a log call will be merged with this global metadata.
import log from '@lickle/log'
log.meta['requestId'] = '123'
log.info`start`
// Output: { level: 'info', time: '...', msg: 'start', meta: { requestId: '123' } }
Customize the transport function for the logger:
import log from '@lickle/log'
log.transport = (log) => {
if (log.level === 'error') sendToServer({ ...log, time: Date.now() })
console.log(`[${log.level.toUpperCase()}] ${log.msg}`, log.meta)
}
You can create a new logger instance with custom configurations:
import { create } from '@lickle/log'
const log = create({
meta: { service: 'auth' },
transport: (lg) => console[lg.level](`[${lg.time}] ${lg.msg}\n${JSON.stringify(lg.meta)}`),
})
log.info`foo`
This project is licensed under the MIT License.
MIT © Dan Beaven