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

logzio-node-debug

v3.1.1

Published

A nodejs debug wrapper for logz.io

Downloads

39

Readme

logzio-node-debug

npm version

A nodejs debug wrapper for logz.io

This library is ES6 thus suitable for Node development and not for browser.

This lib quite opinionated in a way. In case somebody needs to change it. We can parametrized:

  • all namespaces will be sent to Logz.io in the message
  • log levels are {level: debug} or {level: error}

Installation

npm install logzio-node-debug --save

Sample usage

Initialize the lib with Logz.io options:

const logzOptions = { // Params doc: https://github.com/logzio/logzio-nodejs#options
  token: `${logzio_token}`, 
  type: `${the_app_you_wanna_track}`     
}

require('logzio-node-debug').init(logzOptions)

Create loggers, providing a namespace, for every file where you want to use logging (just like u're used to with debug):

const debug = require('logzio-node-debug').debug('your-project:server')
const error = require('logzio-node-debug').debug('your-project:server:error')

debug('I am debug')
error('I am error')

MDC - Mapped Diagnostic Context

Inspired by Logback's implementation of SLF4J MDC API

MDC allows you to store extra data which will be automagically appended to every log entry as long as you stay in the current context. In Logz.io, the MDC keys will be shown as Fields and can be used as filter parameters.

Compatiblity issue

We have moved to cls-hooked instead of continuation-local-storage so that the lib is compatible with async/await. This causes the MDC to work with NodeJS versions 8+. If any of you need MDC to work with lower versions, create a PR with a switch to cls-hooked and continuation-local-storage. Thank you. ;)

MDC API

MDC.createContext(next)

Creates MDC context. Has to be called before calling put, otherwise put will have no effect.

It is possible to create nested contexts. In that case, all MDC data from parent context will be copied to child context, therefore all modifications to MDC in the child context will be done independently from the parent context.

The next parameter is context callback. MDC data will be valid during the whole duration of the callback. This makes it ideal to call this in a middleware function (see request-id.middleware.js for example).

const MDC = require('logzio-node-debug').MDC

MDC.createContext(doStuff)

Or, if you want to create a context with one or more values already initialized:

const MDC = require('logzio-node-debug').MDC

MDC.createContext(() => {
  MDC.put('foo', 'bar')
  doStuff()
})

Or, as an Express middleware function:

const MDC = require('logzio-node-debug').MDC

function mdcMiddleware(req, res, next) {
  MDC.createContext(() => {
    MDC.put('foo', 'bar')
    next()
  })
}
MDC.getAll()

Returns the whole MDC object.

MDC.get(key)

Returns the value of the key in MDC.

MDC.put(key, value)

Sets the value of key in MDC.

MDC.remove(key)

Sets the key to undefined.

MDC.clear()

Removes all data from MDC.

One example to explain them all
MDC.createContext(() => {
  MDC.put('foo', '1')
  doStuff()
})

function doStuff() {
  MDC.put('bar', 2)
  MDC.get('foo') // => '1'
  MDC.getAll() // => {foo: '1', bar: 2}
  MDC.remove('foo')
  MDC.getAll() // => {bar: 2}
  MDC.clear()
  MDC.getAll() // => {}
}

Request ids in logs

A common use-case for MDC is adding a requestId to all incoming requests. If you have some concurrent request (which you for sure have in prod ;-) then you can filter logs by requestId. To enable this functionality, you can use pre-implemented express middleware requestIdMiddleware. Just add it to your app middlewares, probably somewhere in app.js:

const requestIdMiddleware = require('logzio-node-debug').requestIdMiddleware
app.use(requestIdMiddleware)

In Logz.io/Kibana use field requestId to filter your logs.

Tests

While testing you will probably need no Logz.io at all. In that case, just don't initialize the lib with require('logzio-node-debug').init.

Log level

2 log levels will be appended to your message - {level: debug} or {level: error}.

That's all folks

Enjoy the colorful console logs and logz.io side by side.