1 # log4js-node [![Build Status](https://secure.travis-ci.org/nomiddlename/log4js-node.png?branch=master)](http://travis-ci.org/nomiddlename/log4js-node)
3 [![NPM](https://nodei.co/npm/log4js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js/)
5 This is a conversion of the [log4js](https://github.com/stritti/log4js)
6 framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code and tidied up some of the javascript.
8 Out of the box it supports the following features:
10 * coloured console logging to stdout or stderr
11 * replacement of node's console.log functions (optional)
12 * file appender, with log rolling based on file size
17 * Logstash UDP appender
19 * multiprocess appender (useful when you've got worker processes)
20 * a logger for connect/express servers
21 * configurable log message layout/patterns
22 * different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)
24 NOTE: from log4js 0.5 onwards you'll need to explicitly enable replacement of node's console.log functions. Do this either by calling `log4js.replaceConsole()` or configuring with an object or json file like this:
44 var log4js = require('log4js');
45 var logger = log4js.getLogger();
46 logger.debug("Some debug messages");
48 By default, log4js outputs to stdout with the coloured layout (thanks to [masylum](http://github.com/masylum)), so for the above you would see:
50 [2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages
52 See example.js for a full example, but here's a snippet (also in fromreadme.js):
54 var log4js = require('log4js');
55 //console log is loaded by default, so you won't normally need to do this
56 //log4js.loadAppender('console');
57 log4js.loadAppender('file');
58 //log4js.addAppender(log4js.appenders.console());
59 log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');
61 var logger = log4js.getLogger('cheese');
62 logger.setLevel('ERROR');
64 logger.trace('Entering cheese testing');
65 logger.debug('Got cheese.');
66 logger.info('Cheese is Gouda.');
67 logger.warn('Cheese is quite smelly.');
68 logger.error('Cheese is too ripe!');
69 logger.fatal('Cheese was breeding ground for listeria.');
73 [2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe!
74 [2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria.
76 The first 5 lines of the code above could also be written as:
78 var log4js = require('log4js');
82 { type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
89 You can configure the appenders and log levels manually (as above), or provide a
90 configuration file (`log4js.configure('path/to/file.json')`), or a configuration object. The
91 configuration file location may also be specified via the environment variable
92 LOG4JS_CONFIG (`export LOG4JS_CONFIG=path/to/file.json`).
93 An example file can be found in `test/log4js.json`. An example config file with log rolling is in `test/with-log-rolling.json`.
94 You can configure log4js to check for configuration file changes at regular intervals, and if changed, reload. This allows changes to logging levels to occur without restarting the application.
96 To turn it on and specify a period:
99 log4js.configure('file.json', { reloadSecs: 300 });
101 For FileAppender you can also pass the path to the log directory as an option where all your log files would be stored.
104 log4js.configure('my_log4js_configuration.json', { cwd: '/absolute/path/to/log/dir' });
106 If you have already defined an absolute path for one of the FileAppenders in the configuration file, you could add a "absolute": true to the particular FileAppender to override the cwd option passed. Here is an example configuration file:
108 #### my_log4js_configuration.json ####
114 "filename": "relative/path/to/log_file.log",
117 "category": "relative-logger"
122 "filename": "/absolute/path/to/log_file.log",
125 "category": "absolute-logger"
130 Documentation for most of the core appenders can be found on the [wiki](https://github.com/nomiddlename/log4js-node/wiki/Appenders), otherwise take a look at the tests and the examples.
133 See the [wiki](https://github.com/nomiddlename/log4js-node/wiki). Improve the [wiki](https://github.com/nomiddlename/log4js-node/wiki), please.
135 There's also [an example application](https://github.com/nomiddlename/log4js-example).
138 Contributions welcome, but take a look at the [rules](https://github.com/nomiddlename/log4js-node/wiki/Contributing) first.
142 The original log4js was distributed under the Apache 2.0 License, and so is this. I've tried to
143 keep the original copyright and author credits in place, except in sections that I have rewritten