Skip to content

Commit

Permalink
Merge pull request #5 from jackwilsdon/add-message-format
Browse files Browse the repository at this point in the history
merged PR to fix issue #4
  • Loading branch information
felixmc committed Jun 5, 2015
2 parents a049dac + d3a8ad2 commit 19f6639
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ log.info( 'hello world!' );
log.warn( 'carefule there, world!' );

log.error( 'WHOA WHOA WHOA world?!' );

log.config({
messageFormatting: true
});

log.info("I see %d %s!", 3, "bananas");
```

The above code will render to:
Expand Down Expand Up @@ -164,3 +170,21 @@ For examples, the longest string of the above events is `warning`, with 7 charac
error.length = 5 ===> 7 - 5 = 2 ===> " "

If this confuses you, don't worry much about it, you don't have to use it. I simply included it to make outputing to the console prettier.

### Message Formatting

Message Formatting

Messages can be formatted using util.format by setting the config option messageFormatting to true. This defaults to false to ensure backward compatibility with versions prior to 0.3.1.

For example:

```javascript
log.info("I see", "3", "bananas!") // Outputs "I see 3 bananas!"

log.config({
messageFormatting: true
});

log.info("I see %d %s!", 3, "bananas"); // Outputs "I see 3 bananas!"
```
12 changes: 10 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ var log = require('./logger').config({ level: 0 });
log.debug('hello?');

log.info('hello world!');

log.warn('careful there, world');

log.error('WHOA WHOA WHOA world!');

log.info("I see", "3", "bananas!") // Outputs "I see 3 bananas!"

log.config({
messageFormatting: true
});

log.info("I see %d %s!", 3, "bananas"); // Outputs "I see 3 bananas!"
14 changes: 10 additions & 4 deletions logger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var dateFormat = require('dateformat'),
colors = require('colors');
colors = require('colors'),
util = require('util');

var events = { };

var options = {
level: 0,
format: "%timestamp% - %event%:%padding% %message%",
timestamp: "HH:MM:ss"
timestamp: "HH:MM:ss",
messageFormatting: false
};

function log_event( options ) {
Expand Down Expand Up @@ -36,8 +38,12 @@ log_event.prototype.__defineGetter__ ('padding', function() {
log_event.prototype.output = function(input) {
if(options.level <= this.level ) {
var message = '';
for(var i in input) {
message += " " + ( typeof input[i] === "object" ? JSON.stringify( input[i], null ) : input[i] );
if (options.messageFormatting) {
message = " " + util.format.apply(util, input);
} else {
for(var i in input) {
message += " " + ( typeof input[i] === "object" ? JSON.stringify( input[i], null ) : input[i] );
}
}
var format = this.format || options.format;
var output = format
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "custom-logger",
"description": "Simple yet highly customizable console logger for node.js. Supports templating output, custom log levels, custom log types, and colored output.",
"version": "0.3.0",
"version": "0.3.1",
"author": "Felix Milea-Ciobanu <[email protected]> (http://felixmc.com/)",
"repository": {
"type": "git",
Expand Down

0 comments on commit 19f6639

Please sign in to comment.