log4js-node

A port of log4js to node.js

View the Project on GitHub

API

configuration - log4js.configure(object || string)

There is one entry point for configuring log4js. A string argument is treated as a filename to load configuration from. Config files should be JSON, and contain a configuration object (see format below). You can also pass a configuration object directly to configure.

Configuration should take place immediately after requiring log4js for the first time in your application. If you do not call configure, log4js will use LOG4JS_CONFIG (if defined) or the default config. The default config defines one appender, which would log to stdout with the coloured layout, but also defines the default log level to be OFF - which means no logs will be output.

If you are using cluster, then include the call to configure in the worker processes as well as the master. That way the worker processes will pick up the right levels for your categories, and any custom levels you may have defined. Appenders will only be defined on the master process, so there is no danger of multiple processes attempting to write to the same appender. No special configuration is needed to use log4js with clusters, unlike previous versions.

Configuration objects must define at least one appender, and a default category. Log4js will throw an exception if the configuration is invalid.

configure method call returns the configured log4js object.

Configuration Object

Properties:

Loggers - log4js.getLogger([category])

This function takes a single optional string argument to denote the category to be used for log events on this logger. If no category is specified, the events will be routed to the appender for the default category. The function returns a Logger object which has its level set to the level specified for that category in the config and implements the following functions:

Shutdown - log4js.shutdown(cb)

shutdown accepts a callback that will be called when log4js has closed all appenders and finished writing log events. Use this when your programme exits to make sure all your logs are written to files, sockets are closed, etc.

Custom Layouts - log4js.addLayout(type, fn)

This function is used to add user-defined layout functions. See layouts for more details and an example.