Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added parameters support

  • Loading branch information...
commit 6dc47079c7a013ba9a1a8309ef581875cca8429e 1 parent 15a0444
Igor Urminček authored
View
28 README.md
@@ -1,7 +1,12 @@
nlogger
===========
-nlogger (formerly node-logger) is a Node.js logging library that prints messages with module name, current line number and can print messages in color and can be configured from file.
+nlogger is a Node.js logging library that can
+
+* print messages with module name and current line number so you know from where it was called
+* print messages in color
+* print parameters in message
+* be configured from file
Usage
@@ -10,7 +15,12 @@ Use npm or download. Then add to your code:
var logger = require('nlogger').logger(module);
-*module* is object defined automatically by nodejs. Don't bother with it, just always type *module*.
+*module* is object defined automatically by Node.js. If you don't want automatic module names, replace it with your desired string name.
+
+ logger.info(message);
+ logger.info(message, parameter...);
+
+Strings `{}` in message will be replaced by appropriate parameter. See examples.
Examples
--------
@@ -21,6 +31,7 @@ Examples
logger.warn('Warning message');
logger.error('Error message');
logger.trace('Trace message');
+ logger.info('Array = {}, Object = {}', [1, 2, 3, 4], {one: 1, two: 2});
Output samples
@@ -31,6 +42,7 @@ Output samples
2010-10-02 20:39:03.589 WARN main:7 - Warning message
2010-10-02 20:39:03.590 ERROR main:8 - Error message
2010-10-02 20:39:03.590 TRACE main:9 - Trace message
+ 2010-10-02 20:39:03.590 INFO main:10 - Array = [ 1, 2, 3, 4 ], Object = { one: 1, two: 2 }
2010-10-02 20:59:12.496 INFO my-modules/first:3 - Message from first module from line #3
2010-10-02 20:59:12.514 INFO my-modules/second:10 - Message from second module from line #10
@@ -40,7 +52,7 @@ Output samples
Configuration
-------------
-nlogger tries load configuration file nlogger.json which looks like:
+nlogger can load optional configuration file nlogger.json which looks like:
{
"color": "auto",
@@ -50,14 +62,16 @@ nlogger tries load configuration file nlogger.json which looks like:
}
}
-* color - print message in color? [true, false, "auto"]
-* level.* - default debug level
-* level.yourModuleName - debug level for specified module
+* `color` - print message in color? [true, false, "auto"]
+* `level.*` - default debug level
+* `level.yourModuleName` - debug level for specified module
-Possible debug levels are trace, debug, info, warn, error.
+Possible debug levels are `trace`, `debug`, `info`, `warn`, `error`.
Changes
-------
+0.3.0 - Added parameters support to logging methods
+
0.2.0 - Added configuration file support
0.1.0 - First npm release
View
1  examples/app.js
@@ -4,7 +4,6 @@ var logger = require('nlogger').logger(module);
var first = require('./my-modules/first');
var second = require('./my-modules/second');
var third = require('./my-modules/third');
-var fourth = require('./my-modules/fourth');
setTimeout(function() {
logger.debug('Timed log');
View
11 examples/my-modules/first.js
@@ -1,3 +1,14 @@
var logger = require('nlogger').logger(module);
logger.info('Message from first module from line #3');
+logger.debug({one: 1, two: 2});
+
+logger.info('Message with {} parameters. Works also with objects {}. Cool!', 'some', { a: 1, b: 2 });
+logger.info('One = {}, two = {}, 3 = {}, four = {}', 1, 2, 'three', 4);
+logger.info('Array = {}, Object = {}', [1, 2, 3, 4], {one: 1, two: 2});
+
+try {
+ throw new Error('Throwing sample error');
+} catch(e) {
+ logger.error('Something went wrong! Catched {}', e.message);
+}
View
3  examples/my-modules/fourth.js
@@ -1,3 +0,0 @@
-var logger = require('nlogger').logger();
-
-logger.info('Message from fourth module from line #3');
View
11 examples/my-modules/second.js
@@ -1,10 +1,3 @@
-var logger = require('nlogger').logger(module);
+var logger = require('nlogger').logger('my-custom-name');
-// some dummy empty lines
-
-
-
-
-
-
-logger.info('Message from second module from line #10');
+logger.info('Message from second module with defined module name');
View
4 examples/my-modules/third.js
@@ -1,3 +1,3 @@
-var logger = require('nlogger').logger('fake-module-name');
+var logger = require('nlogger').logger();
-logger.info('Message from third module from line #3');
+logger.info('Ops! We forgot module name in this file');
View
20 lib/nlogger.js
@@ -79,9 +79,15 @@ function getClass(module) {
}
}
-function getMessage(msg) {
+function getMessage(msg, params) {
+ var i;
if (typeof msg == 'string') {
- return msg;
+ // insert params
+ msg = msg.split('{}');
+ for (i = 0; i < params.length; i++) {
+ msg.splice(2 * i + 1, 0, sys.inspect(params[i], false, 10));
+ }
+ return msg.join('');
} else {
return sys.inspect(msg, false, 10);
}
@@ -91,7 +97,7 @@ try {
var file = fs.readFileSync('./nlogger.json', 'binary'),
config = JSON.parse(file);
} catch(e) {
- sys.puts(getDate() + ' WARN nlogger Error loading nlogger config file. Using defaults');
+ sys.puts(getDate() + ' WARN nlogger - Error loading nlogger config file. Using defaults');
config = {};
}
@@ -118,14 +124,18 @@ exports.logger = function(module) {
if (levelStr.length == 4) levelStr += ' ';
if (useColor) {
logger[level] = function(msg) {
+ var params;
if (methods[level].priority >= priority) {
- sys.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg) + '\x1B[0m');
+ params = Array.prototype.slice.call(arguments, 1);
+ sys.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg, params) + '\x1B[0m');
}
};
} else {
logger[level] = function(msg) {
+ var params;
if (methods[level].priority >= priority) {
- sys.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg));
+ params = Array.prototype.slice.call(arguments, 1);
+ sys.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg, params));
}
};
}
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "nlogger",
"description": "Logging lib that prints module name and line number",
- "version": "0.2.0",
+ "version": "0.3.0",
"homepage": "https://github.com/igo/nlogger",
"keywords": ["log", "logging", "logger"],
"author": "Igor Urminček",
Please sign in to comment.
Something went wrong with that request. Please try again.