@yingluan/consolo
v1.0.0
Published
A lightweight logger with optional tag prefix.
Downloads
98
Readme
@yingluan/consolo
A lightweight logger with optional tag prefix.
Installation
npm install @yingluan/consoloUsage
Basic
import { consolo } from '@yingluan/consolo'
consolo.info('This is an info message')
// This is an info messageWith tag
import { consolo } from '@yingluan/consolo'
consolo.withTag('@yingluan/consolo').info('This is an info message with tag')
// [@yingluan/consolo] This is an info message with tagMultiple tags
import { consolo } from '@yingluan/consolo'
consolo.withTag('@yingluan').withTag('consolo').info('This is an info message with multiple tags')
// [@yingluan][consolo] This is an info message with multiple tagsCustom instance
Use createConsolo to create an instance with custom options. It is recommended to pass process.env.NODE_ENV === 'development' so that bundlers can statically analyze and tree-shake logging code in production builds.
import { createConsolo } from '@yingluan/consolo'
const consolo = createConsolo({isDev: process.env.NODE_ENV === 'development'})
consolo.withTag('@yingluan/consolo').info('This is an info message with a custom instance')
// [@yingluan/consolo] This is an info message with a custom instanceType-safe tags
type Scope = 'background' | 'popup' | 'content'
const consolo = createConsolo<Scope>({ isDev: process.env.NODE_ENV === 'development' })
consolo.withTag('background').info('started') // ✅
consolo.withTag('other').info('started') // ❌ type errorAPI
consolo
Shared instance for app-wide usage. isDev defaults to true, so logging is enabled by default.
createConsolo<T>(options={})
Creates a new Consolo instance.
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| isDev | boolean | true | When true, logs are printed to the console. |
.withTag(tag)
Returns a new Consolo instance with the tag appended to the prefix. Can be chained.
.info(...args)
Logs to console.info. No-op in production.
.error(...args)
Logs to console.error. No-op in production.
