arsenic-logger

Simple, easy to read log statements with stack trace in Node.js. Now supports the ArsenicLogger cloud logging service.

node-logger

Simple, easy to read log statements with stack trace in Node.js. There are a few other great loggers out there for Node.js, the inspiration to create our own was mainly driven by the need for both a stack trace and a colorized and easy to read output.

npm install arsenic-logger
Logger = require('arsenic-logger');

There are 5 levels of logging

log, debug, info, warn, error, fatal

Here is an example of how to use the logger.

 
Logger = require('arsenic-logger');
 
Logger.setLevel('debug');
 
Logger.debug("debug test");
Logger.info("info test");
Logger.warn("info test");
Logger.error("errortest");
 
var someObject = new Object();
someObject.name = 'testing';
someObject.data = [5,6,7,8,9];
someObject.date = new Date();
 
Logger.debug("This is an object... ", someObject);
 
function somefunc(){
    Logger.debug("testing inside a function");  
}
 
var someclass = {
    test:function(){
        Logger.debug("Testing inside a class");
    }
}
 
somefunc();
someclass.test();
 
Logger.info(someObject);
 
// A fatal call, that will call process.exit 
Logger.fatal("fatal test");
 
// Feed uncaught exceptions to the Logger 
Logger.catchExceptions();
    
function badFunc(){
    throw "This is an exception!";  
}
 
badFunc();
 
Logger.debug(variableThatDoesntExist);
 

The method, setTimestamp turns time stamps on for the logger

You can turn on timestamps using the following;

// Turn timestamps on 
Logger.echoTimestamps(true);
 
// Example output 
// [Tue Oct 9:40:24 2014] testing inside a function  {from line 35 of test.js ... 
 
// Turn time stamps off 
Logger.echoTimestamps(false);

You can set the timestamp format using the following command. Internally the Logger uses the moment library and so supports any format supported by moment, you can see the supported formats here.

As a convenience, this also turns timestamps on, so no need to call echoTimestaps(true).

// Set timestamp format 
Logger.setTimestampFormat('ddd, hA');
 
// Example output  
//[Tue, 9AM] [debug] testing inside a function  {from line 36 of test.js ... 

The Logger supports any localed support by moment, for example 'fr'.

Logger.setLocale('fr');
 
// Example output 
// [mar. oct. 10:01:24 2014] [debug] testing inside a function  {from line 37 of test.js ... 

The method setFilter can be used to give fine control over what logs are sent to the console. This supports function names, filenames and tags.

Logger.setFilter({functions:"somefunc"})
Logger.setFilter({files:"test.js"})
Logger.setFilter({tags:"my-tag"})

These can be combined, such as;

// Echo ONLY logs that match the filename and tag 
Logger.setFilter({files:"test.js", tags: "my-tag"})

Also, you can pass an array or a single string into any of these options. In that case, the effect will be an OR, e.g.

// Echo logs that have the tag my-tag or my-other-tag 
Logger.setFilter({tags: ["my-tag", "my-other-tag"]})

You can add a custom tag and other advanced options by using the 'advanced' version of the logging commands;

    Logger.debugX('tags', 'args');
    Logger.warnX('tags', 'args');
    Logger.infoX('tags', 'args');
    Logger.errorX('tags', 'args');
    Logger.fatalX('tags', 'args');

Where tags includes custom tags. The other args are the same as the basic version of the Logging commands.

tags can be either a string, or an array of strings. e.g. 'TestTag' or ['This','Or','That'] where it will match on any of the tags in the array.

Here is an example;

var someObject = new Object();
someObject.name = 'fred';
someObject.data = [5,6,7,8,9];
someObject.date = new Date();
 
Logger.debugX("TestTag", "This is an object, but using a custom tag... ", someObject);

You can now filter output based on log level and also these advanced values using setFilter, for example;

Logger.setLevel('debug');
Logger.setFilter({tags:"TestTag"});
 
var someObject = new Object();
someObject.name = 'fred';
someObject.data = [5,6,7,8,9];
someObject.date = new Date();
 
var anotherObject = new Object();
anotherObject.name = 'bob';
anotherObject.data = [2,7,8];
 
Logger.debug("This is an object... ", someObject, anotherObject);
 
Logger.debugX("TestTag", "This is an object, but using a custom tag... ", someObject);

Here, because the level was set to debug and a the filter was set to the tag "TestTag" the only output from this would be from the 2nd debug statement.

This can be particulalry useful for large projects where you want to only see the logs from a specific section of code at a time.

The Logger now supports Loggly. It makes use of node-loggly under-the-hood. To use this, call;

Logger.useLoggly('token', 'subdomain','username','password');

Then all calls will be sent to your loggly account!

The Logger supports the cloud logging service offered by ArsenicSoup. To use this service first create an account at ArsenicLogger (http://logger.arsenicsoup.com).

Once you have an account, you will be given a API key. With this, you can now setup this Logger class to send log reports from your server to the ArsenicLogger service.

Then simply setup the Logger like so;

Logger.useArsenicLogger('YOUR-API-KEY');

You can also specify a custom tag to assign to all subsequent logging calls to help with searching and categorizing on the ArsenicLogger service, e.g.;

Logger.useArsenicLogger('YOUR-API-KEY', 'MY-TAG');

The Logger can echo current memory and cpu usage information to the command line using the following commands, which can be combined. Note: the arsenic logger service automatically is sent this information.

Logger.echoMemoryUsage()

This will add the amount of memory used by the heap at the time an entry was logged. For example, the follow result shows the process was consuming 3.16GB of heap space when the Logger.error("errortest") method was called.

[3.16GB  error] errortest  {from line 14 of test.js....}
Logger.echoCPUUsage()

In a similar fashion, the current 15 minute average CPU usage is sent to the console, for example;

[1.43%  error] errortest  {from line 14 of test.js,......}

Requires the excellent callsite module (https://github.com/visionmedia/callsite)

npm install callsite

And also the Path module (http://nodejs.org/api/path.html)

npm install path

For a more full featured logger, check out tracer.

If you like this, and use it, please consider donating to help support future development.

Donate Bitcoins

Feel free to contact me at mike@arsenicsoup.com if you want to help or have suggestions.

Thanks!

(The MIT License)

Copyright (C) 2012 by Ad Astra Systems, LLC;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.