logwrangler is a module logging system that you can easily extend to add various logging locations and functions.
Install
npm install --save logwrangler
Use
Logging to stdout
var logwrangler = require('../index');
var logger = logwrangler.create();
logger.log({
level: logger.levels.INFO,
ns: 'web server',
message: 'some log message',
data: {}
});
Options
level <string>
Specifies the level of the log. Levels can be found in logwrangler
object or in the logger
instance object.
Available levels include:
ns <string>
Specifies a particular namespace for your log. Good for grouping logs by location in your codebase or by component.
node <string>
Name of the node, machine, or specific process currently running.
ident <string>
A more local identifier for the particular log. This could be something like a user ID to make it easy to trace the actions of a user.
message <string>
A human readable message to describe what is happening
data <object>
Contextual data related to the this particular log message. If the value of data
is an Error()
, the error will be parsed to grab it's message and stack trace and data
will be rewritten to have an error
property that will contain the contents of the Error
object.
data, no error
{
data: {
someKey: 'someValue'
}
}
data, with error
{
data: new Error('Some error message')
}
// will become:
{
data: {
error: {
message: 'Some error message',
stack: <stack trace>
}
}
}
data, error and context
{
data: {
error: new Error('Some error message'),
someKey: 'someValue'
}
}
// will become
{
data: {
error: {
message: 'Some error message',
stack: <stack trace>
},
someKey: 'someValue'
}
}
Custom logging handler
Creating a custom logger is as easy as providing an anonymous function to logger.use
.
Parameters
options
Options that you passed in during initialization
{
options: {
level: <level to use>
}
}
data
The data to be logged
{
data: {
level: <log level>,
ns: <namespace - string>,
ident: <custom identifier - string>,
node: <node, machine, process, identifier - string>
message: <message - string>,
data: <custom data - object>
}
}
Usage
var logger = fancyLog.create({
level: fancyLog.levels.DEBUG, // default level to log
});
logger.use(function(options, data){
// do something with your data
});