8000 GitHub - Pingid/lickle-log: A tiny structured logging utility that includes, customizable transports, metadata.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

A tiny structured logging utility that includes, customizable transports, metadata.

License

Notifications You must be signed in to change notification settings

Pingid/lickle-log

Repository files navigation

A little logger

A tiny structured logging utility that includes, customizable transports, metadata.

Build Status Build Size Version Downloads

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

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' } }

Customizing the Transport

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)
}

Creating a New Logger

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`

License

This project is licensed under the MIT License.

MIT © Dan Beaven

About

A tiny structured logging utility that includes, customizable transports, metadata.

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0