Skip to content

Commit

Permalink
Prepare for 0.2.0
Browse files Browse the repository at this point in the history
Fix moment().add deprecated issue
Change to write directly instead of emit an event. See #4
Add Android-like logging function signature
Include the stacktrace for trace-level logging
Update test cases
Update README
  • Loading branch information
wood committed Nov 20, 2014
1 parent b2466fc commit dd8596c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"quotmark": "double",
"noempty": true,
"noarg": true,
"trailing": false
"trailing": false,
"sub":true
}
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
}];

var logA = new ln("a", appenders);
logA.error("ln");
logA.e("ln");
logA.error(new Error("ln"));
logA.error("ln", new Error("ln"), { a: true }); //you can call it with numbers of argument
logA.e("ln", new Error("ln"), { a: true }); //you can call it with numbers of argument
//only the last json and string will used
#####Output:
{"n":"a","h":"woods-mac-mini","p":340,"v":0,"t":1402197924414,"l":50,"m":"ln"}
Expand Down Expand Up @@ -88,27 +88,20 @@
* 2.3GHz i5
* 8GB RAM
* 128GB SSD
* OS X 10.9.3
* Node.js 0.10.29
* OS X 10.10.1
* Node.js 0.10.33

#####Testing result

#####Thanks Ryan for making the benchmark script async. See [this](https://github.com/wood1986/ln/pull/3)

async `run.sh 100000`|ln (0.1.4)|bunyan (0.23.1)|log4js (0.6.14)|winston (0.7.3)
--:|:--:|:--:|:--:|:--:
real|**3.96s**|5.38s|6.59s|257.76s
user|**3.56s**|4.90s|6.08s|257.25s
sys|**1.37s**|1.53s|1.56s|7.30s
maximum resident set size|**32.7M**|32.3M|53.7M|158.9M


sync `run.sh 100000`|ln (0.1.3)|ln (0.1.2)|bunyan (0.23.1)|log4js (0.6.14)|winston (0.7.3)
async `run.sh 100000`|ln (0.2.0)|ln (0.1.5)|bunyan (1.2.3)|log4js (0.6.20)|winston (0.8.3)
--:|:--:|:--:|:--:|:--:|:--:
real|**0.90s**|**3.56s**|10.66s|11.76s|436.04s
user|**0.82s**|**3.47s**|10.29s|11.39s|407.30s
sys|**0.10s**|**0.13s**|2.09s|2.10s|22.76s
maximum resident set size|**79.2M**|**77.6M**|79.1M|85.2M|433.6M
real|**4.02s**|4.16s|5.60s|6.51s|6.35s
user|**3.66s**|3.79s|5.19s|6.10s|5.94s
sys |**1.35s**|1.35s|1.47s|1.44s|1.44s
maximum resident set size|**29.8M**|30.6M|52.4M|54.0M|33.6M|


####4. How can I verify your test?

Expand Down
60 changes: 34 additions & 26 deletions lib/ln.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var os = require("os");
var fs = require("fs");
var util = require("util");
var moment = require("moment");
var events = require("events");

var VERSION = 0;
var PIPE_BUFF = 4096;
Expand Down Expand Up @@ -44,13 +43,7 @@ function ln(name, appenders) {
if (appender.hasOwnProperty("level") && LEVEL.hasOwnProperty(appender.level.toUpperCase())) {
appender.level = LEVEL[appender.level.toUpperCase()];
} else {
throw new TypeError("each appender must have \"level\" attribute");
}

if (!appender.emitter) {
appender.emitter = new events.EventEmitter();
} else if (!(appender.emitter instanceof events.EventEmitter)) {
throw new TypeError("the emitter is not in events.EventEmitter type");
throw new TypeError("each appender must have \"level\" attribute with valid value");
}

if (!appender.formatter) {
Expand All @@ -61,17 +54,17 @@ function ln(name, appenders) {

switch (appender.type) {
case "console":
appender.emitter.on("log", function(appender, timestamp, string) {
process.stdout.write(string);
});
appender.write = function(timestamp, string) {
console.log(string);
};
break;
case "file":
appender.queue = {};
appender.isFlushed = true;
var units = { "ms": 111, "s": 1, "m": 1, "h": 12, "d": 1, "w": 1, "M": 3, "y": 1 };
var keys = Object.keys(units);
var key = null;
while ((key = keys.shift()) !== undefined && moment().format(appender.path) === moment().add(key, units[key]).format(appender.path));
while ((key = keys.shift()) !== undefined && moment().format(appender.path) === moment().add(units[key], key).format(appender.path));
if (keys.length === 0) {
appender.formattedPath = moment().format(appender.path);
} else {
Expand All @@ -80,12 +73,13 @@ function ln(name, appenders) {
appender.formattedPath = "";
}

var log = function(appender, timestamp, string) {
var log = function(timestamp, string) {
var appender = this;
var queue = appender.queue;
if (arguments.length > 1) {
if (arguments.length > 0) {
if (appender.hasOwnProperty("period") && timestamp >= appender.next) {
appender.formattedPath = (appender.isUTC ? moment(timestamp).utc() : moment(timestamp)).format(appender.path);
appender.next = moment(timestamp).startOf(appender.period).add(appender.period, 1).valueOf();
appender.next = moment(timestamp).startOf(appender.period).add(1, appender.period).valueOf();
}
var path = appender.formattedPath;
queue[path] = queue[path] || [];
Expand Down Expand Up @@ -118,7 +112,7 @@ function ln(name, appenders) {
});
appender.stream.on("drain", function() {
appender.isFlushed = true;
appender.emitter.emit("log", appender);
appender.write();
});
}
for (i = 0, n = queue[key].length; appender.isFlushed && i < n; i++) {
Expand All @@ -131,7 +125,7 @@ function ln(name, appenders) {
}
}
};
appender.emitter.on("log", log);
appender.write = log;
break;
default:
break;
Expand Down Expand Up @@ -174,13 +168,26 @@ function log(level) {
} else if (typeof arg === "object") {
this.fields.j = arg;
} else if (typeof arg !== "undefined") {
this.fields.m = arg;
if (level === LEVEL["TRACE"]) {
var _prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = function (error, stack) {
var string = "Trace : " + arg;
for (var j = 1, m = stack.length; j < m; j++) {
string += "\n at " + stack[j].toString();
}
Error.prepareStackTrace = _prepareStackTrace;
return string;
};
this.fields.m = new Error().stack;
} else {
this.fields.m = arg;
}
}
}

for (i = 0, n = appenders.length; i < n; i++) {
var appender = appenders[i];
appender.emitter.emit("log", appender, timestamp, appender.formatter(this.fields) + "\n");
appender.write(timestamp, appender.formatter(this.fields) + "\n");
}

delete this.fields.m;
Expand All @@ -192,12 +199,13 @@ module.exports = ln;
module.exports.LEVEL = LEVEL;
module.exports.PIPE_BUFF = PIPE_BUFF;

ln.prototype.trace = log(10);
ln.prototype.debug = log(20);
ln.prototype.info = log(30);
ln.prototype.warn = log(40);
ln.prototype.error = log(50);
ln.prototype.fatal = log(60);
ln.prototype.trace = ln.prototype.t = log(10);
ln.prototype.debug = ln.prototype.d = log(20);
ln.prototype.info = ln.prototype.i = log(30);
ln.prototype.warn = ln.prototype.w = log(40);
ln.prototype.error = ln.prototype.e = log(50);
ln.prototype.fatal = ln.prototype.f = log(60);

ln.prototype.clone = function(name) {
return new ln(name, this);
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ln",
"version": "0.1.5",
"version": "0.2.0",
"description": "The SUPER BEST JSON logging library for Node.js",
"main": "./lib/ln.js",
"directories": {
Expand Down
Loading

0 comments on commit dd8596c

Please sign in to comment.