Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changed parameters of logging methods

  • Loading branch information...
commit 4c664baccd4d2f2229851576b9ea951583ba6d69 1 parent d26e490
@igo authored
View
8 README.md
@@ -18,9 +18,7 @@ Use npm or download. Then add to your code:
*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.
+ logger.info(message, variable, message, variable...);
Examples
--------
@@ -31,7 +29,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});
+ logger.info('Array = ', [1, 2, 3, 4], ', Object = ', {one: 1, two: 2});
Output samples
@@ -70,6 +68,8 @@ Possible debug levels are `trace`, `debug`, `info`, `warn`, `error`.
Changes
-------
+0.4.0 - Logging methods parameters changed
+
0.3.1 - Fixed util dependencies
0.3.0 - Added parameters support to logging methods
View
4 examples/app.js
@@ -1,6 +1,4 @@
-require.paths.unshift('../lib');
-
-var logger = require('nlogger').logger(module);
+var logger = require('../lib/nlogger').logger(module);
var first = require('./my-modules/first');
var second = require('./my-modules/second');
var third = require('./my-modules/third');
View
9 examples/my-modules/first.js
@@ -1,14 +1,13 @@
-var logger = require('nlogger').logger(module);
+var logger = require('../../lib/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});
+logger.info('Message with ', 'some', ' parameters. Works also with objects ', { a: 1, b: 2 }, '. Cool!');
+logger.info('Array = ', [1, 2, 3, 4], ', Object = ', {one: 1, two: 2});
try {
throw new Error('Throwing sample error');
} catch(e) {
- logger.error('Something went wrong! Catched {}', e.message);
+ logger.error('Something went wrong! Catched ', e.message);
}
View
2  examples/my-modules/second.js
@@ -1,3 +1,3 @@
-var logger = require('nlogger').logger('my-custom-name');
+var logger = require('../../lib/nlogger').logger('my-custom-name');
logger.info('Message from second module with defined module name');
View
2  examples/my-modules/third.js
@@ -1,3 +1,3 @@
-var logger = require('nlogger').logger();
+var logger = require('../../lib/nlogger').logger();
logger.info('Ops! We forgot module name in this file');
View
30 lib/nlogger.js
@@ -23,7 +23,7 @@
THE SOFTWARE.
*/
-var sys = require('util'),
+var util = require('util'),
fs = require('fs');
@@ -76,25 +76,23 @@ function getClass(module) {
}
}
-function getMessage(msg, params) {
- var i;
- if (typeof msg == 'string') {
- // insert params
- msg = msg.split('{}');
- for (i = 0; i < params.length; i++) {
- msg.splice(2 * i + 1, 0, sys.inspect(params[i], false, 10));
+function getMessage(items) {
+ var msg = [], i;
+ for (i = 0; i < items.length; i++) {
+ if (typeof items[i] == 'string') {
+ msg.push(items[i]);
+ } else {
+ msg.push(util.inspect(items[i], false, 10));
}
- return msg.join('');
- } else {
- return sys.inspect(msg, false, 10);
}
+ return msg.join('');
}
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');
+ util.puts(getDate() + ' WARN nlogger - Config file not found. Using default configuration.');
config = {};
}
@@ -121,18 +119,14 @@ exports.logger = function(module) {
if (levelStr.length == 4) levelStr += ' ';
if (useColor) {
logger[level] = function(msg) {
- var params;
if (methods[level].priority >= priority) {
- params = Array.prototype.slice.call(arguments, 1);
- sys.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg, params) + '\x1B[0m');
+ util.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments) + '\x1B[0m');
}
};
} else {
logger[level] = function(msg) {
- var params;
if (methods[level].priority >= priority) {
- params = Array.prototype.slice.call(arguments, 1);
- sys.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(msg, params));
+ util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments));
}
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.