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

log4node

v0.1.6

Published

Logger for node cluster, compatible with logrotate

Downloads

615

Readme

Overview

Build Status

This module is designed to be used with Node Cluster in production:

  • one log file for all workers
  • compatible with logrotate: an USR2 signal reopen the log file

This module is compatible with node 0.8.x. For the node 0.6.x, please use version 0.0.1 of this module.

This module is inspired from this module.

How to use it

Installation

npm install log4node

Usage

Default logger:

var log = require('log4node');

log.error("this is a log");

Will output to console.

Note: you can reconfigure default logger by calling

log.reconfigure({level: 'info', file: 'toto.log'});

Will now write into toto.log

Your custom logger:

var log4node = require('log4node');
    log = new log4node.Log4Node({level: 'warning', file: 'test.log'});

log.error("this is a log");
log.debug("this is a debug log");

Note : you can use the syntax accepted by utils.format.

Log level

Log level can be adjusted for each logger:

log.setLogLevel('info');

Log level for default logger is 'info'.

Available log levels are:

  • emergency
  • alert
  • critical
  • error
  • warning
  • notice
  • info
  • debug

Prefix

Prefix of log lines can be changed:

log.setPrefix("%d - %p ");

You can use following field in prefix:

  • %d: current date
  • %p: current process id
  • %l: log level

Default prefix is: [%d] %l

You can also use a function to specify prefix :

log.setPrefix(function(level) {
  return 'toto ' + (new Date()).toString() + ' ';
});

Cluster mode

Workers processes will send logs to the cluster master for writing to file.

Setup is fully transparent for developper.

A full example can be found here.

Reopen log file

Just send USR2 signal to node process, or, in cluster mode, to master node process:

kill -USR2 pid

Example of logrotate file:

/var/log/node.log {
  rotate 5
  weekly
  postrotate
    kill -USR2 `cat process.pid`
  endscript
}

Create a specialized logger

This feature is provided to specialize a logger for a sub-component. You can create a new logger with its own level and prefix for a sub-component. The logs will be send to the same files with a prefix.

log = new log4node.Log4Node({log_level: 'warning', file: 'test.log'});
sublogger1 = log.clone({prefix:'SUBMODULE - ', level:'error');

or with the default logger

sublogger1 = log4node.clone(prefix:'SUBMODULE - ', level:'error');

Mutliple instanciation

If you have a module A which depends of log4node, and a module B which also depends of log4node, you have to use only one instance, for example by giving the log4node instance of A to B.

Write callback

If you want to plug a custom transport, just specify a write_callback

log = new log4node.Log4Node({
  write_callback: function(line) {
  // do something with the formatted line of log.
  }
});

Changelog

Version 0.1.6

  • Add write callback

Version 0.1.5

  • In cluster mode, workers open the log files in append mode instead of sending logs to the master. You can revert to the old way (always send log to master).

License

Copyright 2012 Bertrand Paquet

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.