Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

Log Errors

Build Status

  • logs errors based on environment
    • development gets full color-coded errors
    • production error logs use syslog error headings so they will be picked up by logwatch et al
    • production only logs serious errors (level 3 and below) to reduce noise
  • request attrs logged if passed request object
  • response returned if passed response object

Quickstart

varlogErrors=require('log-errors'),logProd=logErrors.production,logDev=logErrors.development;try{thrownewerror("funky");}catch(e){logDev(e);}

This will output in colored text:

Error name: Error
Error object:
{ Error: funky,
message: 'funky',
type: undefined,
stack: Getter/Setter,
arguments: undefined }
Stack trace:
'Error: funky at Object.<anonymous> (/home/andy/lib/modules/npm/log-errors/lib/development.js:16:12) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)'

Using in Express.js

It will also log request url + query info.

varlogErrors=require('log-errors'),logProd=logErrors.production,logDev=logErrors.development;// add this at very bottom (below all route handlers)// it is designed to catch the errors passed by next(err) callsapp.configure('production',function(){app.use(logProd);});app.configure('development',function(){app.use(logDev);// equates to:// app.use(function(err, req, res, next) {// logDev(err, req, res, next);// });});

Example Output:

Error name: Error
Request:
url: someurl
query: ?some=random&query=params
Error object:
{ Error: funky,
message: 'funky',
type: undefined,
stack: Getter/Setter,
arguments: undefined }
Stack trace:
'Error: funky at Object.<anonymous> (/home/andy/lib/modules/npm/log-errors/lib/development.js:16:12) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)'

Docs

Info printed to STDOUT

  1. error name: (err.name)
  2. error message (err.message)
  3. error logLevel (err.logLevel)
  4. error responseCode (err.resCode)
  5. request url (req.url)
  6. request query (req.query)

HTTP Response

If passed a response object then the logger will return this with the response code specified in the error (err.resCode). It defaults to sending a 500 server error with the body {error: 'Error'}.

varlogErrors=require('log-errors'),logDev=logErrors.development;// will call res.send(403, {"error":"random"})logDev({resCode:403,name:"random"},req,res);

Log Levels

  • Must be one of the 8 unix log levels used in the Visionmedia Logging Module.
  • Defaults to 'info' level if not passed one of the 8 listed.

Base Log Class

  • Production and development loggers inherit from this.
  • If app is an eventEmitter then the 'seriousError' evt will be prodcast when using the productionLogger.
varLogClass=function(env,app){this.env=env;this.app=app;};

Development Logger

  • Always prints full error in colored text.
varlogDev=require('log-errors').development;logDev(newError('development error msg'));

Production Logger

  • Will only print error info if error.LogLevel is 3 or below, ie ['error', 'critical', 'alert', 'emergency']
varlogProd=require('log-errors').production,err=newError('some message about the error');err.logLevel='critical'logProd(err);

'seriousError' Evt

  • If error.LogLevel is 3 or below and this.app is an eventEmitter then the 'seriousError' evt will be prodcast when using the productionLogger.
  • It will NOT be broadcast if the error.doNotKill is truthy
if(!err.doNotKill){appErrorHandler&&this.app.emit('seriousError');}

Using in Conjunction with Custom Errors npm Module

  • If your errors inherit from the custom errors class then the extra error attrs (logLevel, name, message etc) are already added.
  • However, the logger should work fine with the built in base error class too.
varvalErr=require('custom-errors').general.ValidationError,logErrors=require('log-errors'),logProd=logErrors.production,logDev=logErrors.development;try{thrownewvalErr("funky");}catch(e){logDev(e);}

Outputs:

Error name: Validation
Error object:
{ logLevel: 'warn',
message: 'funky',
name: 'Validation',
resCode: 400,
[stack]: [Getter/Setter] }
Stack trace:
'Validation: funky at Object.<anonymous> (/home/andy/lib/modules/npm/log-errors/lib/development.js:18:12) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)'

About

Node.js error logger, can be used as standalone or with express.js

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages