Skip to content

Commit cc80e06

Browse files
committed
Added sprintf-like %s support
1 parent 781361c commit cc80e06

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Instead of the log level constants, you may also supply a string:
3030
var Log = require('log')
3131
, log = new Log('warning');
3232

33+
We can also use `%s` much like `console.log()` to pass arguments:
34+
35+
log.error('oh no, failed to send mail to %s.', user.email);
36+
3337
## Reader
3438

3539
To stream a log, simply pass a readable stream instead of a writable:

examples/file.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@
66
[Sun, 26 Sep 2010 01:26:14 GMT] CRITICAL a critical message
77
[Sun, 26 Sep 2010 01:26:14 GMT] ALERT a alert message
88
[Sun, 26 Sep 2010 01:26:14 GMT] EMERGENCY a emergency message
9+
[Thu, 03 Feb 2011 04:54:39 GMT] DEBUG a debug message
10+
[Thu, 03 Feb 2011 04:54:39 GMT] INFO a info message
11+
[Thu, 03 Feb 2011 04:54:39 GMT] NOTICE a notice message
12+
[Thu, 03 Feb 2011 04:54:39 GMT] WARNING a warning message
13+
[Thu, 03 Feb 2011 04:54:39 GMT] ERROR a error message
14+
[Thu, 03 Feb 2011 04:54:39 GMT] CRITICAL a critical message
15+
[Thu, 03 Feb 2011 04:54:39 GMT] ALERT a alert message
16+
[Thu, 03 Feb 2011 04:54:39 GMT] EMERGENCY a emergency message

examples/stdout.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ log.warning('a warning message');
1313
log.error('a error message');
1414
log.critical('a critical message');
1515
log.alert('a alert message');
16-
log.emergency('a emergency message');
16+
log.emergency('a emergency %s', 'message');
17+
log.emergency('a really %s emergency %s', 'bad', 'message');

lib/log.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ Log.prototype = {
139139
* Log output message.
140140
*
141141
* @param {String} levelStr
142-
* @param {String} msg
142+
* @param {Array} args
143143
* @api private
144144
*/
145145

146-
log: function(levelStr, msg) {
146+
log: function(levelStr, args) {
147147
if (exports[levelStr] <= this.level) {
148+
var i = 1;
149+
var msg = args[0].replace(/%s/g, function(){
150+
return args[i++];
151+
});
148152
this.stream.write(
149153
'[' + new Date().toUTCString() + ']'
150154
+ ' ' + levelStr
@@ -162,7 +166,7 @@ Log.prototype = {
162166
*/
163167

164168
emergency: function(msg){
165-
this.log('EMERGENCY', msg);
169+
this.log('EMERGENCY', arguments);
166170
},
167171

168172
/**
@@ -173,7 +177,7 @@ Log.prototype = {
173177
*/
174178

175179
alert: function(msg){
176-
this.log('ALERT', msg);
180+
this.log('ALERT', arguments);
177181
},
178182

179183
/**
@@ -184,7 +188,7 @@ Log.prototype = {
184188
*/
185189

186190
critical: function(msg){
187-
this.log('CRITICAL', msg);
191+
this.log('CRITICAL', arguments);
188192
},
189193

190194
/**
@@ -195,7 +199,7 @@ Log.prototype = {
195199
*/
196200

197201
error: function(msg){
198-
this.log('ERROR', msg);
202+
this.log('ERROR', arguments);
199203
},
200204

201205
/**
@@ -206,7 +210,7 @@ Log.prototype = {
206210
*/
207211

208212
warning: function(msg){
209-
this.log('WARNING', msg);
213+
this.log('WARNING', arguments);
210214
},
211215

212216
/**
@@ -217,7 +221,7 @@ Log.prototype = {
217221
*/
218222

219223
notice: function(msg){
220-
this.log('NOTICE', msg);
224+
this.log('NOTICE', arguments);
221225
},
222226

223227
/**
@@ -228,7 +232,7 @@ Log.prototype = {
228232
*/
229233

230234
info: function(msg){
231-
this.log('INFO', msg);
235+
this.log('INFO', arguments);
232236
},
233237

234238
/**
@@ -239,7 +243,7 @@ Log.prototype = {
239243
*/
240244

241245
debug: function(msg){
242-
this.log('DEBUG', msg);
246+
this.log('DEBUG', arguments);
243247
}
244248
};
245249

0 commit comments

Comments
 (0)