Permalink
Browse files

Added sprintf-like %s support

  • Loading branch information...
1 parent 781361c commit cc80e06b8351053e3a9dd38193d0c6a5d2aa98aa @tj committed May 23, 2011
Showing with 28 additions and 11 deletions.
  1. +4 −0 Readme.md
  2. +8 −0 examples/file.log
  3. +2 −1 examples/stdout.js
  4. +14 −10 lib/log.js
View
@@ -30,6 +30,10 @@ Instead of the log level constants, you may also supply a string:
var Log = require('log')
, log = new Log('warning');
+ We can also use `%s` much like `console.log()` to pass arguments:
+
+ log.error('oh no, failed to send mail to %s.', user.email);
+
## Reader
To stream a log, simply pass a readable stream instead of a writable:
View
@@ -6,3 +6,11 @@
[Sun, 26 Sep 2010 01:26:14 GMT] CRITICAL a critical message
[Sun, 26 Sep 2010 01:26:14 GMT] ALERT a alert message
[Sun, 26 Sep 2010 01:26:14 GMT] EMERGENCY a emergency message
+[Thu, 03 Feb 2011 04:54:39 GMT] DEBUG a debug message
+[Thu, 03 Feb 2011 04:54:39 GMT] INFO a info message
+[Thu, 03 Feb 2011 04:54:39 GMT] NOTICE a notice message
+[Thu, 03 Feb 2011 04:54:39 GMT] WARNING a warning message
+[Thu, 03 Feb 2011 04:54:39 GMT] ERROR a error message
+[Thu, 03 Feb 2011 04:54:39 GMT] CRITICAL a critical message
+[Thu, 03 Feb 2011 04:54:39 GMT] ALERT a alert message
+[Thu, 03 Feb 2011 04:54:39 GMT] EMERGENCY a emergency message
View
@@ -13,4 +13,5 @@ log.warning('a warning message');
log.error('a error message');
log.critical('a critical message');
log.alert('a alert message');
-log.emergency('a emergency message');
+log.emergency('a emergency %s', 'message');
+log.emergency('a really %s emergency %s', 'bad', 'message');
View
@@ -139,12 +139,16 @@ Log.prototype = {
* Log output message.
*
* @param {String} levelStr
- * @param {String} msg
+ * @param {Array} args
* @api private
*/
- log: function(levelStr, msg) {
+ log: function(levelStr, args) {
if (exports[levelStr] <= this.level) {
+ var i = 1;
+ var msg = args[0].replace(/%s/g, function(){
+ return args[i++];
+ });
this.stream.write(
'[' + new Date().toUTCString() + ']'
+ ' ' + levelStr
@@ -162,7 +166,7 @@ Log.prototype = {
*/
emergency: function(msg){
- this.log('EMERGENCY', msg);
+ this.log('EMERGENCY', arguments);
},
/**
@@ -173,7 +177,7 @@ Log.prototype = {
*/
alert: function(msg){
- this.log('ALERT', msg);
+ this.log('ALERT', arguments);
},
/**
@@ -184,7 +188,7 @@ Log.prototype = {
*/
critical: function(msg){
- this.log('CRITICAL', msg);
+ this.log('CRITICAL', arguments);
},
/**
@@ -195,7 +199,7 @@ Log.prototype = {
*/
error: function(msg){
- this.log('ERROR', msg);
+ this.log('ERROR', arguments);
},
/**
@@ -206,7 +210,7 @@ Log.prototype = {
*/
warning: function(msg){
- this.log('WARNING', msg);
+ this.log('WARNING', arguments);
},
/**
@@ -217,7 +221,7 @@ Log.prototype = {
*/
notice: function(msg){
- this.log('NOTICE', msg);
+ this.log('NOTICE', arguments);
},
/**
@@ -228,7 +232,7 @@ Log.prototype = {
*/
info: function(msg){
- this.log('INFO', msg);
+ this.log('INFO', arguments);
},
/**
@@ -239,7 +243,7 @@ Log.prototype = {
*/
debug: function(msg){
- this.log('DEBUG', msg);
+ this.log('DEBUG', arguments);
}
};

0 comments on commit cc80e06

Please sign in to comment.