Scrib
A logger utlity for Node.js using adapters.
Scrib itself is just an event emitter with some fancy methods. The real wok is done by its adapters. When you initialize Scrib, you load one or more adapters for it. When you tell Scrib to log something, it's going to tell every adapter about your log and they'll handle it in their way e.g. send an e-mail or write to a log file.
For Scrib everything is a message. Every error, warning or log is a message. Each message has following properties:
message
: A short message describing itselfdata = {}
: Optional data (e.g. API response)priority = 0
: The message's importance as a number equal or greater than 0id = null
: An ID to specify the message. Useful for errorscategory = null
: A category to group messages (e.g. Database)time = Date.now()
: The timestamp when we logged
Example
npm install scrib
var Scrib = logger = "local": file: "./log.txt" { // If during Scrib's startup an error appeared we should stop the process ife throw e; logger; });
This example loads the local-adapter for Scrib. After Scrib finished loading it's going to call the callback.
Adapters
scrib-local
: Write logs into log files
When you want to write one yourself, have a look at this.
API
new Scrib(adapters, callback)
Initalizes a new logger. adapters
is a object where each key is an adapter's name and the value is an config object which is going to be passed into the adapter.
Scrib first tries to require scrib-{ADAPTER}
and then the file directly (see the code).
callback
is a callback where the first argument is an error.
// Will require scrib-local "local": {} // Wll require ./adapter.js "./adapter.js": {} {});
Scrib.put(message, data, priority, id, category)
The simplest way to log something:
logger;
Scrib.register(id, message, priority, category)
Scrib.log(id, data)
If we don't want to call Scrib.put
each time using priority, category and message, we can simply register a message and then log it using Scrib.log
and the ID:
logger;logger;
Scrib.catch(error, data)
This transforms an Error
object into a message and then logs it:
try throw "Ups"; catche logger;}
If e
is an error thrown by libuv, Scrib will trun it into a message: message is e.description
, id is e.code
, data.errno is e.errno
.
Additional data from e
is merged into data (see code).
Events
Scrib inherits from EventEmitter
and has its functions.
log
: Emitted for each logid:ID
: Emitted for each log if it's ID isID
, e.g.id:API_TIMEOUT
category:CATEGORY
: Emitted for each log if it's category iscategory
, e.g.category:API
Testing
git clone git://github.com/Acconut/scrib.git
cd scrib
npm install
npm test
Licensed under the MIT License.