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

ctx-logger

v2.0.1

Published

logger with multiple stores and auto context support

Downloads

121

Readme

logger with multiple stores and auto context support

Features

  • normal debug/info/warn/error operation
  • support time/timeEnd for profiling
  • multiple stores(console/file/db...)
  • auto flush on process exit(SIGINT)
  • log the context(through the web request...)

Usage

when you create a new logger object, you can set the following options:

  • stores: store's map object. the key is the store instance's name. Each store has the common options:
    • class: the store class, which can be the package name string, or the require return
    • level: the threshold level(DEBUG/INFO/WARN/ERROR/OFF). default is DEBUG
    • time: whether to record time/timeEnd operation. default is true
  • context: the context variable names. these variables are attached in the process.domain

if you do not set the stores option, a internal ConsoleStore will be used. it can also be retreived by require('ctx-logger').ConsoleStore.

var Logger = require('ctx-logger');

//use the internal ConsoleStore
var logger = new Logger();
...

//create a logger with a console store and a file store
var logger = new Logger({stores: {
  console: {class: Logger.ConsoleStore},
  file: {class: 'ctx-logger-file-store', dir: './log'}
}});
...

logger.time('label a');
logger.debug('a debug message');
logger.info('a info message');
logger.warn('a warn message');
logger.error('a error message');
logger.timeEnd('label a');

Used with context

In web http request, you may want to distinguish the differenct log messages produced by differenct users with the only one logger. Here, you can! see the code(with express):

//add the domain middleware
app.use(function(req, resp, next){
  var reqDomain = require('domain').create();
  reqDomain.on('error', function (err) {
    /*error handler*/
  });

  //set the context var to the user id, assume it's kiliwalk
  reqDomain.userId = req.session.userId;
  
  reqDomain.add(req);
  reqDomain.add(resp);
  reqDomain.run(next);
});


var logger = new Logger({stores: {
  console: {class: Logger.ConsoleStore},
}, context: 'userId'});

app.get('/test', function(req, resp){
  logger.debug('visit test page');
});

the output will be:

2015-09-15 11:11:11 DEBUG [kiliwalk] visit test page

Stores

Internal console store

you can choose whether enable colored output in console. just set the color option. default is true.

var logger = new Logger({stores: {
  console: {class: Logger.ConsoleStore, color: false},
}});

License :

Licensed under MIT

Copyright (c) 2015 kiliwalk