erlog

0.1.7 • Public • Published

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")
>> Sun, October 27th 2013, 4:38:44 pm: 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

// give erlog an identifier to tie to the object
options.title = "Descriptor String"
 
// will log non-enumerable properties if true, defaults to false
options.showHidden = true
 
// number of times to recurse into object, defaults to null 
// when null erlog will log the entire object's tree
options.depth = 3 
 
 // if true, erlog will format object with color, defaults to true
options.colors = false
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: "El Fred"
  , showHidden: false
  , depth: 1
  , colors: true 
})
>> Sun, October 27th 2013, 4:38:44pm: Inspection of El Fred
>> { param1: 123904939,
>>   func:
>>    { [Function]
>>      [length]: 0,
>>      [name]: '',
>>      [arguments]: null,
>>      [caller]: null,
>>      [prototype]: [Object] },
>>   foo: 'bar',
>>   child:
>>    { fizz: 0,
>>      buzz: 1,
>>      fizzbuzz: [Object] } }

Or, to keep things tidy and simple:

erlog.inspect(myObject)
>> Sun, October 27th 2013, 4:38:44pm: Inspection of Object
>> { param1: 123904939,
>>   func:
>>    { [Function]
>>      [length]: 0,
>>      [name]: '',
>>      [arguments]: null,
>>      [caller]: null,
>>      [prototype]: { [constructor]: [Circular] } },
>>   foo: 'bar',
>>   child:
>>    { fizz: 0,
>>      buzz: 1,
>>      fizzbuzz: 
>>       [ { name: 'jen' },
>>         { name: 'bob' },
>>         [length]: 2 ] } }

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 through the users", success: "and there weren't any problems" })) {
    // there was an error
    next(err) 
  } else {
    // good to go! And erlog logs the success message, if there is one!
    req.user = user;
  }
}) 
>> Sun, October 27th 2013, 4:38:44pm: I was looking through the users
>> Sun, October 27th 2013, 4:38:44pm:: and there weren't any problems

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;
  } 
})
>> Sun, October 27th 2013, 4:38:44pm: Upon checking for an error
>> Sun, October 27th 2013, 4:38:44pm:: Successful

Planned features

  • allow for substitution along the lines of erlog('this: %a and that %b', a, b)
  • module.export options to change the formatting style of output

License

The MIT License (MIT)

Copyright © 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.

Readme

Keywords

none

Package Sidebar

Install

npm i erlog

Weekly Downloads

1

Version

0.1.7

License

MIT

Last publish

Collaborators

  • humanoidanalog