1.6.1 • Published 10 years ago
ezlogger v1.6.1
ezLogger
Installation
npm install ezLoggerInitialize
After initialize all project's console.log will change.
var ezLogger = require("ezlogger");
ezLogger(__dirname + "/log.txt");
console.log("That's it!");// ezLogger(FileName,Pattern);
// default pattern is
// "[date] [file]:[line] [message]"Note: Pattern is updated in 1.3.0 to function
ezLogger("./log.txt", function(file, line, message) { return "{3} {0}:{1} {2}".format(file, line, message, Date.simple()); });
log.txt file appeared.
15/06/2015 15:39:31 app.js:3 That's it!ezFormat
You can use console.log with ezFormat.
// initialize
var ezLogger = require("ezlogger");
ezLogger(__dirname + "/log.txt");var isConnected = true;
var myObject = {
Life: 1,
Tools: [
{
Name: "Jacket",
Weight: 3,
}
]
};
console.log("Server connection: {0:bool}", isConnected);
console.log("Returned object {0:json:4}", myObject);Dynamic File
Initialize ezlogger with function
var ezLogger = require("ezlogger");
ezLogger(function() {
// you can use Date.simple.more too. It will come with ezlogger
return __dirname + "/log." + Date.simple.more() + ".txt";
});
console.log("That's it!");log.15.06.2015.txt file appeared.
15/06/2015 15:39:31 app.js:5 That's it!Trace Level
If you want to create own logger function, you probably get wrong location because of trace level is 1 default. So use logTrace
function myLoggerError(message) {
console.log("[ERROR] {0}", message);
}
myLoggerError("test"); // prints 15/06/2015 15:39:31 test.js:2 [ERROR] test
// but it shouldn't be test.js:2 it should be test.js:5with logTrace
function myLoggerError(message) {
console.logTrace(2, "[ERROR] {0}", message);
}
myLoggerError("test"); // prints 15/06/2015 15:39:31 test.js:5 [ERROR] testadd ezFormat to own function
function myLoggerError(message) {
if (arguments.length > 1) {
message = String.prototype.format.apply(message, Array.prototype.slice.call(arguments, 1));
}
console.logTrace(2, "[ERROR] {0}", message);
}
myLoggerError("test! {0}", 1); // prints 15/06/2015 15:39:31 test.js:9 [ERROR] test! 11.6.0 Features
- console.error added
- console.errorTrace added
- handleUncaughtExceptions added.
var ezLogger = require("ezlogger")();
ezLogger.handleUncaughtExceptions();