Permalink
Browse files

Custom logger function

  • Loading branch information...
1 parent a330031 commit 028b13aa5019fadc95ba70c31178d747ebd52811 @corpix committed Jul 19, 2012
Showing with 20 additions and 16 deletions.
  1. +13 −13 index.js
  2. +1 −1 package.json
  3. +6 −2 readme.md
View
26 index.js
@@ -1,8 +1,14 @@
var fs = require('fs')
, path = require('path')
, basename = path.basename
+ , defaultLogger
, ex = {};
+defaultLogger = function(err){
+ console.error(new Date().toLocaleString(), '>>', err);
+ console.log(err.stack);
+}
+
ex.define = function(opts){
var fn = function(){
this.message = opts.message;
@@ -22,12 +28,10 @@ ex.define = function(opts){
}
ex.bind = function(app, opts){
- if(!opts)
- opts = {};
+ if(!opts) opts = {};
+ opts.logger || (opts.logger = defaultLogger);
- if(!app.set('view engine')){
- opts.plain = true;
- }
+ if(!app.set('view engine')) opts.plain = true;
if(opts.lastRoute == undefined || opts.lastRoute == true) {
app.use(function(req, res, next){
@@ -37,8 +41,7 @@ ex.bind = function(app, opts){
app.use(function(err, req, res, next){
if(!err.name || err.name == 'Error' || !ex.hasOwnProperty(err.name)){
- console.error(new Date().toLocaleString(), '>>', err);
- console.log(err.stack);
+ opts.logger(err);
if(req.xhr || opts.plain)
return res.send({ error: 'Internal error' }, 500);
@@ -52,8 +55,7 @@ ex.bind = function(app, opts){
});
}
- if(req.xhr)
- return res.send({ error: err.message }, err.status);
+ if(req.xhr) return res.send({ error: err.message }, err.status);
if(opts.plain){
res.send(err.message, err.status);
@@ -79,12 +81,10 @@ ex.bind = function(app, opts){
var httpErrors = __dirname + '/http';
fs.readdir(httpErrors, function(err, files){
- if(err)
- throw new Error(err);
+ if(err) throw new Error(err);
files.forEach(function(file){
- if(file.charAt(0) == '.')
- return;
+ if(file.charAt(0) == '.') return;
var opts = require(httpErrors + '/' + file);
opts.name = basename(file, '.json');
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Corpix <me@corpix.ru> (http://corpix.ru)",
"name": "express-errors",
"description": "Error handler",
- "version": "0.2.1",
+ "version": "0.2.2",
"repository": {
"type": "git",
"url": "git://github.com/corpix/express-errors.git"
View
8 readme.md
@@ -16,6 +16,7 @@ Provides easy access to common http errors for express e.g.
You need create `views/errors` directory and add 401,403,404 error views.
```javascript
+ var logger;
app.get('/400', function(req, res, next){
next(errors.BadRequest); // You will get "bad request" error
});
@@ -26,14 +27,18 @@ You need create `views/errors` directory and add 401,403,404 error views.
var errors = require('express-errors');
- errors.bind(app, { layout: false }); // Important! Call it after all routes ready
+ logger = function(err){ // Custom logger function
+ console.error(err);
+ }
+ errors.bind(app, { layout: false, logger: logger }); // IMPORTANT! Call it after all routes ready
```
#### Options
Currently this options are available:
* `lastRoute` - if false `express-errors` will not maintain last app.use route(NotFound error)
* `plain` - if app.settings['view engine'] is undefined OR this option is presented res.send instead of res.render will be used
+ * `logger` - custom logger function
#### Defining an error
Yeah, you can. Use .define method:
@@ -48,5 +53,4 @@ Yeah, you can. Use .define method:
#### TODO
- * Bundled views
* More options

0 comments on commit 028b13a

Please sign in to comment.