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 =
Functions
Erlog has a few simple functions that make logging and troubleshooting a little easier:
Simple log message
Call this function with any string as an argument and it'll format it with the current date and time.
Example
>> Sun, October 27th 2013, 4:38:44 pm: Hello World
Inspect object
erlog
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 objectoptionstitle = "Descriptor String" // will log non-enumerable properties if true, defaults to falseoptionsshowHidden = true // number of times to recurse into object, defaults to null // when null erlog will log the entire object's treeoptionsdepth = 3 // if true, erlog will format object with color, defaults to trueoptionscolors = false
Example
var myObject = param1: 123904939 { return thisparam1 } foo: "bar" child: fizz: 0 buzz: 1 fizzbuzz: name: "jen" name: "bob" erlog
>> 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
>> 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
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:
optionsmessage = "When I was trying to do thing" // a context to give the log entryoptionssuccess = "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
>> 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
>> 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.