0.1.7 • Published 11 years ago

erlog v0.1.7

Weekly downloads
4
License
-
Repository
github
Last release
11 years ago

Erlog - Reporting in for duty!

A simple logging middleware for node.js, just install with npm and require where you need it

$ npm install erlog
var erlog = require('erlog')

Functions

Erlog has a few simple functions that make logging and troubleshooting a little easier:

Simple log message

erlog(message)

Call this function with any string as an argument and it'll format it with the current date and time.

Example
erlog("Hello World")

Inspect object

erlog.inspect(object, options)

This will log the contents of any object you pass to it, as well as its children. The options argument is optional, and may include the following options

options.title = "Descriptor String" // give erlog an identifier to tie to the object
options.showHidden = true // will log non-enumerable properties if true, defaults to false
options.depth = 3 // number of times to recurse into object, when null (default) erlog will log the entire object's tree
options.colors = false // if true, erlog will attempt to format object with color, defaults to true
Example
var myObject = {
    param1: 123904939
  , func: function() {
      return this.param1
    }
  , foo: "bar"
  , child: {
        fizz: 0
      , buzz: 1
      , fizzbuzz: [
            { name: "jen" }
          , { name: "bob" }
        ]
    }
}

erlog.inspect(myObject, { 
    title: "An Object"
  , showHidden: false
  , depth: 1
  , colors: true 
})

//or, to keep things tidy
erlog.inspect(myObject)

Error Logging

erlog.error(error, options)

This was developed with callback errors in mind, which are a common practice in node.js applications. It conveniently returns 0 if there was no error encountered, and returns a nonzero value if errors were passed. The options object accepts the following properties, but is optional:

options.message = "When I was trying to do thing" // a context to give the log entry
options.success = "I was able to do thing" // a message to log if no error was encountered
Example

For example, when searching through a database with mongoose:

User.find(id, function(err, user) {
  if (erlog.error(err, { message: "I was looking for a user", success: "and I found them!" })) {
    // there was an error
    next(err) 
  } else {
    // good to go! And erlog logs the success message, if there is one!
    req.user = user;
  }
}) 

And if you like things a tad more tidy

User.find(id, function(err, user) {
  if (erlog.error(err)) {
    //there was an error
    next(err)
  } else {
    // good to go! nothing is logged by erlog, this time.
    req.user = user;
  } 
})

License

The MIT License (MIT)

Copyright (c) 2013 Michael Keating

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.1.7

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago