Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from zhangzifa/master
Browse files Browse the repository at this point in the history
add timestamp options
  • Loading branch information
dead-horse committed Dec 11, 2015
2 parents 9516b55 + e5a4919 commit 2ed5e02
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ logger.http('http request url: %s', 'https://github.com');
* **flushInterval**: all logs will cache in memory first, every `flushInterval` ms flush into files. default is `1s`
* **duration**: cut the logs every `duration` ms. default is `1h`
* **mkdir**: everytime before create a writeStream, will try to `mkdirp` first. useful when format is like `YYYY/MM/DD/[{category}.log]`, default to false
* **timestamp**: write timestamp with format YYYYMMDDHHmmssSSS before every line of logs, default is false

### Events

Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var copy = require('copy-to');
var util = require('util');
var ms = require('ms');
var os = require('os');
var YYYYMMDDHHmmssSSS = require('utility').YYYYMMDDHHmmssSSS;

var SEPERATOR = os.EOL + os.EOL;


/**
* Expose `Logger`
*/
Expand All @@ -33,7 +35,8 @@ var defaultOptions = {
stdout: false,
file: true,
errorFormatter: formatter,
seperator: SEPERATOR
seperator: SEPERATOR,
timestamp: false
}

function Logger(options) {
Expand Down Expand Up @@ -105,8 +108,12 @@ Logger.prototype._init = function() {
};

Logger.prototype._write = function (category, msg) {
var now = '';
if (this._options.timestamp) {
now = YYYYMMDDHHmmssSSS() + ' ';
}
// write to file
if (this._options.file && this._streams[category]) this._streams[category].write(msg);
if (this._options.file && this._streams[category]) this._streams[category].write(now + msg);

/* istanbul ignore next */
// write to stdout
Expand All @@ -117,8 +124,8 @@ Logger.prototype._write = function (category, msg) {
}

category === 'error'
? process.stderr.write(msg)
: process.stdout.write(msg);
? process.stderr.write(now + msg)
: process.stdout.write(now + msg);
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"should": "~4.0.4"
},
"dependencies": {
"utility": "~1.6.0",
"copy-to": "~1.0.1",
"error-formatter": "~1.0.3",
"iconv-lite": "~0.4.4",
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,20 @@ describe('mini-loger', function () {
should.not.exist(logger._streams.error);
});
});

describe('Logger.timestamp', function () {
it('should exist timestamp ', function () {
var logger = Logger({dir: logdir, timestamp: true, categories:['timestamp']});
logger.timestamp('logtext with timestamp');
logger.flush('timestamp');
var patt = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3} logtext with timestamp/g;
setTimeout(function () {
var content = fs.readFileSync(logger.getPath('timestamp'), 'utf-8');
var match = patt.exec(content);
shoudl.notEqual(match, null);
done();
}, 100);
});
});

});

0 comments on commit 2ed5e02

Please sign in to comment.