npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ekino/logger

v2.1.1

Published

A Lightweight logger that combines debug namespacing capabilities with winston levels and multioutput

Downloads

330

Readme

@ekino/logger

NPM version Travis CI Coverage Status styled with prettier

A Lightweight logger that combines debug namespacing capabilities with winston levels and multioutput

Installation

Using npm:

npm install @ekino/logger

Or yarn:

yarn add @ekino/logger

Usage

By default, the logger output warn and error levels for all namespaces. You can set LOG_LEVEL environment to override the default behavior. By default, it writes logs to stdout in JSON format

The logger api allows you to set log level for all namespaces. For advanced usage, you define it even per namespace.

A log instance is bounded to a namespace. To use it, instantiate a logger with a namespace and call a log function.

This logger define 5 log levels: error, warn, info, debug, trace. When you set a level, all levels above it are enabled too. Log level can be set by calling setLevel function.

For example, enabling info will enable info, warn and error but not debug or trace. The "special" log level none means no log and can only be used to set a namespace level.

{ trace: 0, debug: 1, info: 2, warn: 3, error: 4 }

The basic log function signature is:

my_log.the_level(message, data) // With data an object holding informations usefull for debug purpose

Example

const { setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setNamespaces('root:*')
setLevel('debug')

const logger = createLogger('root:testing')
logger.debug('sample message', {
    foo: 'bar',
})

output:

Example

Using context ID

One of the main complexity working with node is ability to follow all logs attached to one call or one function. This is not mandatory, but based on our experience, we recommend as a best practice to add a unique identifier that will be passed all along functions calls. When you log something, you can provide this id as a first parameter and logger will log it. If not provided, it's auto generated.

The signature of the function with contextId is:

my_log.the_level(contextId, message, data)

Example app.js

const { setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setNamespaces('root:*')
setLevel('debug')

const logger = createLogger('root:testing')
logger.debug('ctxId', 'log with predefined context ID', {
    foo: 'bar',
})

output:

Example

Using namespaces

Logger relies on namespaces. When you want to log something, you should define a namespace that is bound to it. When you debug, this gives you the flexibility to enable only the namespaces you need to output. As a good practice, we recommend setting a namespace by folder / file. For example for a file in modules/login/dao you could define 'modules:login:dao'. Warning, "=" can't be part of the namespace as it's a reserved symbol.

You can also define a level per namespace. If no level is defined, the default global level is used. To disable logs of a namespace, you can specify a level none A namespace ':*' means eveything after ':' will be enabled. Namespaces are parsed as regexp.

To define namespace level, you should suffix namespace with "=the_level" For example let's say you need to enable all info logs but for debug purpose you need to lower the level of the namespace database to debug. You could then use:

const { setLevel, setNamespaces } = require('@ekino/logger')

setLevel('info')
setNamespaces('*,database*=debug,database:redis*=none')

Using Logging Namespaces

const { setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setNamespaces('namespace:*, namespace:mute=none')
setLevel('debug')

const loggerA = createLogger('namespace:subNamespace')
const loggerB = createLogger('namespace:mute')

loggerA.debug('Will be logged')
loggerB.info('Will not be logged')
const { setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setNamespaces('*, wrongNamespace=none')
setLevel('debug')

const loggerA = createLogger('namespace:subNamespace')
const loggerB = createLogger('wrongNamespace')

loggerA.debug('Will be logged')
loggerB.info('Will not be logged')

Outputs

Logger allow you to provide your own output adapter to customize how and where to write logs. It's bundle by default with pretty adapter and json that both write to stdout. By default, json adapter is enabled. You can use multiple adapters at the same time

JSON

const { setNamespaces, setLevel, setOutput, outputs, createLogger } = require('@ekino/logger')

setNamespaces('namespace:*')
setLevel('debug')
setOutput(outputs.json)

const logger = createLogger('namespace:subNamespace')
logger.debug('ctxId', 'Will be logged', {
    someData: 'someValue',
    someData2: 'someValue',
})

output:

Example

Pretty

Pretty will output a yaml like content.

const { setNamespaces, setLevel, setOutput, outputs, createLogger } = require('@ekino/logger')

setNamespaces('namespace:*')
setLevel('debug')
setOutput(outputs.pretty)

const logger = createLogger('namespace:subNamespace')
logger.debug('ctxId', 'Will be logged', {
    someData: 'someValue',
    someData2: 'someValue',
})

output:

Example

Output function

An output, is a function that will receive log data and should transform and store it

Log data follow the format:

{
    time: Date,
    level: string,
    namespace: string,
    contextId: string,
    meta: { any data defined in global context },
    message: string,
    data: object
}
const { setNamespaces, setLevel, setOutput, outputs, outputUtils, createLogger } = require('@ekino/logger')

setNamespaces('namespace:*')
setLevel('debug')

const consoleAdapter = (log) => {
    console.log(outputUtils.stringify(log))
}

// This will output in stdout with the pretty output
// and in the same will log through native console.log() function (usually to stdout too)
setOutput([outputs.pretty, consoleAdapter])

const logger = createLogger('namespace:subNamespace')
logger.debug('ctxId', 'Will be logged', {
    someData: 'someValue',
    someData2: 'someValue',
})

JSON Stringify utility

To ease the creation of an output adapter, we provide a utility to stringify a json object that support circular reference and add stack to output for errors.

const { outputUtils } = require('@ekino/logger')

const consoleAdapter = (log) => {
    console.log(outputUtils.stringify(log))
}

Log data

Most of the time, a log message is not enough to guess context. You can append arbitrary data to your logs. If you're using some kind of log collector, you'll then be able to extract those values and inject them in elasticsearch for example.

const { setOutput, setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setOutput('pretty')
setNamespaces('namespace:*')
setLevel('info')

const logger = createLogger('namespace:subNamespace')
logger.warn('message', { someData: 'someValue' })

output:

Example

Force Log

You can force to write the log even the logLevel isn't enabled.

const { setOutput, setNamespaces, setLevel, createLogger } = require('@ekino/logger')

setOutput('pretty')
setNamespaces('namespace:*')
setLevel('info')

const log = logger.createLogger('namespace', true)
const num = 1
log.debug('Will be logged', { someData: 'someValue' }, num > 0)

Adding global metadata

Sometimes, you need to identify to which version or which application the logs refers to. To do so, we provide a function to set informations that will be added to the each log at a top level key.

const { setOutput, setNamespaces, setLevel, setGlobalContext, createLogger } = require('@ekino/logger')

setOutput('pretty')
setNamespaces('*')
setLevel('info')
setGlobalContext({ version: '2.0.0', env: 'dev' })

const logger = createLogger('namespace')
logger.warn('message', { someData: 'someValue' })

output:

Example

TypeScript

This package provides its own definition, so it can be easily used with TypeScript.