fell
A logging library that works in Node.js and the browser.
The aims of fell are as follows:
- Very low cost when logging at a level not in use.
- Friendly to unit testing.
- Allows you to log at different levels from within different pieces of code.
- Quick and easy to get started with.
- Works in both Node.js and the browser.
Getting It
In Node.js:
npm install --save fell
then:
var fell = ;
For the browser, download the latest releases of fell and emitr, and refer to them from your script tags:
The Default Logger
The default configuration has it outputting to the console (if one is available), so you can start using it immediately:
fell;fell;
Specific Loggers
You can get more finely grained control if you log to specified loggers within your modules or classes.
{ thislog = fell;} MyClassprototype { thislog;}; var myObj = ;myObj;
Configuration
To take advantage of this control, you can configure particular loggers to log at particular levels.
fell;
You can set up your logging by calling configure at the start of your program. It takes up to three arguments. The first argument is the default log level that will be done for all loggers that don't have more specific configuration.
The second argument is a map containing logger names to the levels that they should log at. This is interpreted hierarchically, so in the above example the logger mymodule.MyClass
will log at level info
, since it matches the mymodule
configuration. The logger mymodule.some.hierarchy
will log at level fatal
, as will any loggers with names that start mymodule.some.hierarchy.
.
The third argument is an array of destinations that log events should be routed too. If you don't pass anything (as in the above example), this will default to an array containing only a logger that outputs to the console object in environments that support this.
Calling fell.configure
clears the state of the logger, so the levels, configuration and log destinations are all reset.
If you want to modify the logging while in use you can use methods specifically for that:
// Changes the log level for things not configured specifically.fell; // Changes the log level for mymodule.MyClass and things below it.fell; // Adds a new destination that stores the most recent 10 log events.var store = 10;fell; // Removes the previously added destination.fell;
Testing
Care must be taken when testing for log messages in order to avoid writing fragile tests. To help with this, we recommend the use of a mocking library like Mochito.
Here's an example of some logging code that you might want to test:
var fell = ; { thislog = fell; thislog;} MyObjectLOG_MESSAGES = INITIALISING: 'Initialising MyObject, version {0}, with parameter {1}.' MyObjectversion = '1.2.3'; moduleexports = MyObject;
and the corresponding test code to verify it:
var MyObject = ;var fell = ;var mochito = ; ;