log-2000
v0.1.7
Published
Definitely the best logging utility...
Downloads
6
Maintainers
Readme
log-2000
log-2000 is the result of an attempt at creating a highly customizable logger with an interface that is easy to extend.
Installation
npm install --save log-2000
Usage
var Log = require('log-2000')
var log = Log()
log.info('Hello.')
Will print
{ level: 'info',
date: Fri Jan 29 2016 09:25:10 GMT+0100 (W. Europe Standard Time),
message: 'Hello.' }
Examples
Examples can be found here. Play with them on tonicdev.com/npm/log-2000 or locally by doing:
git clone https://github.com/mickvangelderen/log-2000.git
cd log-2000/examples
npm install
node basic
Customizing log-2000
With log-2000, you can customize log levels, what is being logged and where it is being logged.
The default configuration of log-2000 can be viewed in log-factory
.
Customizing levels
The levels can be customized like this:
var Log = require('log-2000')
var log = Log({
levels: [ 'my', 'own', 'levels' ]
})
log.my('hello!')
Customizing writers
These are the writers log-2000 provides:
var Log = require('log-2000')
var consoleWriter = require('log-2000/lib/console-writer')
var log = Log({
writers: [ consoleWriter ]
})
Customizing transformers
These are the transformers log-2000 provides:
var Log = require('log-2000')
var attachDate = require('log-2000/lib/attach-date')
var log = Log({
transformers: [ attachDate ]
})
Extending log-2000
Nowadays, extensibility is important because there are so many talented and ingenious people out there and node is being used for an increasing variety of tasks.
Creating writers
A writer is simply a function that accepts a level
and data
parameter. The function myWriter
in the following code for example is a writer.
function myWriter(level, data) {
console.log(level, data)
}
For more interesting writers I recommend using the factory pattern. Check the default console writer for an example.
It would be nice if all writer factories accepted a transformers
option that transforms incoming data like this:
data = transformers.reduce((d, f) => f(level, d), data)
This ensures that a user can transform the data exactly how they want for each writer. For example one might want to log human readable messages to the console and JSON lines to a file.
Creating transformers
A transformer is simply a function that accepts a level
and data
parameter and returns a new data
object. The following code for example is a transformer.
function myTransformer(level, data) {
return Object.assign({ level: level }, data)
}