Skip to content

Commit

Permalink
Calling color_console instead of console from daily logger, so color …
Browse files Browse the repository at this point in the history
…console configuration can work.
  • Loading branch information
AmitThakkar committed Oct 27, 2015
1 parent ceaeeed commit d28da8d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
```

dailylog will output all types log to diff files every day like log4j
> dailylog will output all types log to diff files every day like log4j
Advanced Example
---------------
Expand Down
114 changes: 57 additions & 57 deletions lib/dailyfile.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
var fs = require('fs'),dateFormat = require('dateformat'), tinytim = require('tinytim'), utils = require('./utils');
var fs = require('fs'), dateFormat = require('dateformat'), tinytim = require('tinytim'), utils = require('./utils');
var path = require('path');

module.exports = function(conf) {
var _conf = {
root : '.',
logPathFormat : '{{root}}/{{prefix}}.{{date}}.log',
splitFormat : 'yyyymmdd'
};
_conf = utils.union(_conf, [conf]);
function LogFile(prefix, date) {
this.date = date;
this.prefix = prefix;
this.path = tinytim.tim(_conf.logPathFormat, {root:_conf.root, prefix:prefix, date:date});
(function mkdirSync_p(dir) {
if (fs.existsSync(dir)) return;
mkdirSync_p(path.dirname(dir));
fs.mkdirSync(dir);
})(_conf.root);
this.stream = fs.createWriteStream(this.path, {
flags: "a",
encoding: "utf8",
mode: 0666
});
}

LogFile.prototype.write = function(str) {
this.stream.write(str+"\n");
};

LogFile.prototype.destroy = function() {
if (this.stream) {
this.stream.end();
this.stream.destroySoon();
this.stream = null;
}
};

var _logMap = {};

function _push2File(str, title) {
var logFile = _logMap[title], now = dateFormat(new Date(), _conf.splitFormat)
if(logFile && logFile.date != now) {
logFile.destroy();
logFile = null;
}
if(!logFile)
logFile = _logMap[title] = new LogFile(title, now);
logFile.write(str);
}

return require('./console')({
transport : function(data) {
_push2File(data.output, data.title);
}
}, conf);
};
module.exports = function (conf) {
var _conf = {
root: '.',
logPathFormat: '{{root}}/{{prefix}}.{{date}}.log',
splitFormat: 'yyyymmdd'
};

_conf = utils.union(_conf, [conf]);

function LogFile(prefix, date) {
this.date = date;
this.prefix = prefix;
this.path = tinytim.tim(_conf.logPathFormat, {root: _conf.root, prefix: prefix, date: date});
(function mkdirSync_p(dir) {
if (fs.existsSync(dir)) return;
mkdirSync_p(path.dirname(dir));
fs.mkdirSync(dir);
})(_conf.root);
this.stream = fs.createWriteStream(this.path, {
flags: "a",
encoding: "utf8",
mode: 0666
});
}

LogFile.prototype.write = function (str) {
this.stream.write(str + "\n");
};

LogFile.prototype.destroy = function () {
if (this.stream) {
this.stream.end();
this.stream.destroySoon();
this.stream = null;
}
};

var _logMap = {};

function _push2File(str, title) {
var logFile = _logMap[title], now = dateFormat(new Date(), _conf.splitFormat);
if (logFile && logFile.date != now) {
logFile.destroy();
logFile = null;
}
if (!logFile)
logFile = _logMap[title] = new LogFile(title, now);
logFile.write(str);
}

return require('./color_console')({
transport: function (data) {
_push2File(data.output, data.title);
}
}, conf);
};

0 comments on commit d28da8d

Please sign in to comment.