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

winston-module-logger

v1.3.1

Published

A simple winston wrapper for generating logger instance per module and log tags

Downloads

21,066

Readme

winston-module-logger

A simple wrapper around winston logger that lets you logging by module and also using metadata for tags. This logger is mainly used (by me) for logging to Console transport. The log format is always JSON since the logger is used mainly in microservices architecture where a collector is collecting the logs to some central log management (e.g ELK, Stackdriver etc.).

Installation

npm install --save winston-module-logger

Usage

const moduleLogger = require('winston-module-logger');

// init the log logger and pass the log level (info by default).
// Should happen once in the application lifecycle.
moduleLogger.init('debug');

Simple string logging

// in some other module or scope you wish to have a specific logger, you can pass the module name
const log = require('winston-module-logger').getLogger('DBAccess');
log.debug('my message');

output on stdout:

{"moduleName":"DBAccess","level":"debug","message":"my message"}

Adding metadata tags

if you wish that all the messages will include some metadata in the logger instance you can 'getLogger' with metadata

const { getLogger } = require('winston-module-logger');
const log = getLogger('MyModule', {apiKey: apiKey});
log.info('REST api called for specific api key');

output on stdout:

{"moduleName":"MyModule","level":"info","message":"REST api called for specific api key", "apiKey":"12345"}

you can also add metadata tags on specific log calls:

log.warn('REST api called with wrong api key', { wrongApiKey: wrongApiKey });

output on stdout:

{"moduleName":"MyModule","level":"info","message":"REST api called for specific api key","apiKey":"12345","wrongApiKey":"555"}

Error logging

You can pass the exception object as the second parameter and it will log the stack trace

log.error('aaa', new Error('my error'), {apiKey: '12345'});
{"moduleName":"MyModule","stack":"Error: my error\n    at Object.<anonymous> (/Users/yehuda/git/rest-api/logger.js:8:18)\n    at Module._compile (module.js:660:30)\n    at Object.Module._extensions..js (module.js:671:10)\n    at Module.load (module.js:573:32)\n    at tryModuleLoad (module.js:513:12)\n    at Function.Module._load (module.js:505:3)\n    at Module.require (module.js:604:17)\n    at require (internal/module.js:11:18)\n    at Object.<anonymous> (/Users/yehuda/git/rest-api/app.js:6:13)\n    at Module._compile (module.js:657:14)","apiKey":"12345","level":"error","message":"aaa"}

Adding error and warning hooks

You can add a dummy 'middleware' to the error and warn logs. This is mainly for the purpose of adding some functionality on error and warnings such as incrementing a prometheus gauge or something like this. For performance reasons you can add only one middleware to the error and warn log functions. The logger assumes there is no dependency in the middleware function and does not 'await' for it.

You can add the middleware both in the module logger level (the logger obtained by the getLogger method) or globally for all the module loggers in the system.

To add a middleware on the module logger use the addLogMiddleware method of the module logger:

log.addLogMiddleware('error', () => { /*... do something */ });

To remove the middleware:

log.clearLogMiddleware('error');

To add a global middleware to all the module loggers (whether they already exist or will be created later):

const moduleLogger = require('winston-module-logger');
moduleLogger.addGlobalLogMiddleware('error', () => {/* ... do something */});

To clear the global middleware:

moduleLogger.clearGlobalLogMiddleware('error'); 

To clear all global middlewares:

moduleLogger.clearAllGlobalMiddlewares();

Contributions

You are welcome to open PRs