@lickle/log
v0.0.0-alpha.7
Published
A tiny structured logging utility that includes, customizable transports and metadata.
Downloads
350
Readme
A little logger
A tiny structured logging utility that includes, customizable transports, metadata.
Install
Install the @lickle/log
library using your preferred package manager:
npm install @lickle/log
Usage
Default Logger
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: '...' } }
Adding Metadata
You can add metadata to the logger using the log.meta
method. Metadata fields are merged by default. To replace existing metadata, pass true
as the second argument log.meta({ ... }, true)
.
import log from '@lickle/log'
log.meta({ requestId: '123' })
log.info`start`
// Output: { level: 'info', time: '...', msg: 'start', meta: { requestId: '123' } }
Configuration
Customizing the Transport
Customize the transport function for the top-level logger as shown below:
import log from '@lickle/log'
log.configure({
transport: (lg) => console[lg.level](`[${lg.time}] ${lg.msg}\n${JSON.stringify(lg.meta)}`),
})
Creating a New Logger Instance
You can create a new logger instance with custom configurations:
import { create } from '@lickle/log'
const logger = create({
meta: {},
transport: (lg) => console[lg.level](`[${lg.time}] ${lg.msg}\n${JSON.stringify(lg.meta)}`),
})
License
This project is licensed under the MIT License.
MIT © Dan Beaven