damn-it v0.3.1
A very simple library which keeps track of errors that occur during runtime
Install
Just like all node modules, you can run the following command to install it.
npm install damn-it
You can install it globally with -g
switch. However I recommand you to install it for each of your node applications locally.
Getting Started
The very first step of using damn-it
is to initialize it.
// instantiate and initialize it in one line
var damnIt = require("damn-it").initialize();
// or you can instantiate it first, then initialize it
var damnIt = require("damn-it");
damnIt.initialize();
I try to keep damn-it
as simple at possible. I strongly think that you shouldn't be worry about your application errors. Although errors are very important, however I believe that there are other critical issuse to think of.
sream() or silent() There is a very simple difference between these methods. Both of them save errors to the given backend. But the first one also log error in nodejs console, whereas, the later one just save error. Their usege are exactly the same.
try-catch
You can use damn-it
to handle your errors manually. If you have a try-catch statement, you can save its catch error with damn-it
.
try {
throw new Error("just a fake error!");
} catch(err) {
damnIt.scream(err);
}
express middleware If your node application is based on express, you can handle your errors too.
app.use(function( err, req, res, next ) {
damnIt.scream(err);
next(err);
});
pure nodejs application
As you know the main purpose of damn-it
is to save your application errors somewhere that you can see and review them easily. There is a method for error handling in nodejs which track all errors. Although it is not recommanded to use it, however for our purpose it is good.
process.on('uncaughtException', function( err ) {
damnIt.scream(err);
});
You can use it in express based applications too.
Options
initialize
method accept a wide range of options. There is also two helper methods namely, set
and get
that you can use to interact with damn-it
options.
backend (default="file") It handle where errors should be save.
- backend="mysql" : save errors in a mysql table
- backend="file" : save errors in a given file name
path (default="./errors.txt") Given file path to save errors.
type (default="json") It handle the way errors are being save.
- type="json" : save errors in JSON format
- type="text" : save errors in plain text
You can set or change damn-it
options in two different ways.
In intialize
method:
var damnIt = require("damnIt").initialize({
backend: "mysql",
file: "logs.txt",
type: "text"
});
Or with set
method:
var damnIt = require("damnIt").initialize();
damnIt.set("backend", "mysql");
damnIt.set("file", "logs.txt");
damnIt.set("type", "text");
Backends
damn-it
supports different type of backends. File
and Database
. You can switch between them anywhere in your application with set
method.
mysql backend
In order to use mysql
backend, you should provide an open connection to your database. Moreover, you need a mysql driver to create a connection to mysql database. I suggest node-mysql module which is powerful and easy to use.
I assume that you are using 'node-mysql'. First you should include it and create a connection.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '<host>',
database : '<database_name>',
user : '<database_username>',
password : '<database_password>'
});
Then you need to pass connection
to damn-it
module. You have to options as before. In the first place you can initialize damn-it
with an option argument which contains your database connection.
var damnIt = require("damn-it").initialize({
backend: "mysql",
db: connection
});
Pay attention to db
property. Its name should be db
.
Then every time you call scream()
or silent()
method, all errors will be saved into a table called errors
in your database.
file backend
There is nothing special about file
backend. However you should know that if you choose it, all errors will be saved into a file called whatever you give as file
property.
Test
You can test
this module with npm test
.