Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DO NOT PUT "use strict" HERE OR THE LOGGER WILL NOT BE ABLE TO LOG ERROR
- /* ************************************************************************
- Version: 0.3
- License: LGPLv3
- Authors: Deisss
- Date: 2012-04-20
- Date of last modification: 2013-06-07
- Description: Define a shared stdout/stderror shared redirection
- ************************************************************************ */
- // Module loaded
- var fs = require("fs"),
- util = require("util");
- // Variables, do not forget / at then end !
- var windows_error_log_root = __dirname + "/../",
- unix_error_log_root = "/var/log/node/",
- max_line_number = 10000,
- stdout = null,
- stderr = null,
- stdoutPath = "",
- stderrPath = "",
- maxct = max_line_number;
- /*
- --------------------------------------
- TIME FUNCTION
- --------------------------------------
- */
- /**
- * Print a date with "0" before if it's less than 10
- *
- * @param variable {Integer} A number (from date)
- * @return {String} The parsed date
- */
- function beautifulDate(variable) {
- var prefix = (variable < 10) ? "0" : "";
- return prefix + variable;
- };
- /**
- * Print in a nice way the output date
- *
- * @return {String} The formatted date
- */
- function outputDate() {
- var d = new Date();
- return ("" + d.getFullYear() + "-" + beautifulDate(d.getMonth()+1) + "-" + beautifulDate(d.getDate()) + " " +
- beautifulDate(d.getHours()) + "." + beautifulDate(d.getMinutes()) + "." + beautifulDate(d.getSeconds()));
- };
- /**
- * Return a formatted date string
- *
- * @return {String} The formatted date
- */
- var printDate = function() {
- var d = new Date();
- var tmp = outputDate();
- return (tmp.replace(".", ":") + " (UTC : " + (d.getTimezoneOffset()/60) + "h) => ");
- };
- /*
- --------------------------------------
- PATH FUNCTION
- --------------------------------------
- */
- /**
- * Get the main OS path, create it if needed
- */
- function getOSRootPath() {
- var path = "";
- if(process.platform === "win32") {
- path = windows_error_log_root;
- } else {
- path = unix_error_log_root;
- }
- //Test or create the directory, we do it synchronously to never loose any log message
- try {
- if(!fs.statSync(windows_error_log_root).isDirectory()) {
- fs.mkdirSync(windows_error_log_root, 0600);
- }
- } catch(e) {
- fs.mkdirSync(windows_error_log_root, 0600);
- }
- return path;
- };
- /*
- --------------------------------------
- STREAM FUNCTION
- --------------------------------------
- */
- /**
- * Start log stream system
- */
- function createStream() {
- var path = getOSRootPath();
- stdoutPath = path + "access.log";
- stderrPath = path + "error.log";
- stdout = fs.createWriteStream(stdoutPath, { flags: "a" });
- stderr = fs.createWriteStream(stderrPath, { flags: "a" });
- };
- /**
- * Close log stream system
- */
- function closeStream() {
- stdout.end();
- stderr.end();
- };
- /**
- * Close stream if needed
- */
- function checkAndCloseStream() {
- maxct--;
- if(maxct <= 0) {
- closeStream();
- //Now we move log files to something usable
- var d = outputDate();
- fs.renameSync(stdoutPath, getOSRootPath() + "access-" + d + ".log");
- fs.renameSync(stderrPath, getOSRootPath() + "error-" + d + ".log");
- //Now we create again stream
- createStream();
- maxct = max_line_number;
- }
- };
- /**
- * Console logger object
- */
- module.exports = new function() {
- /**
- * Print a log (any number of arguments allowed)
- */
- this.log = function() {
- console.log.apply(this, arguments);
- if(stdout != null && stdout.writable===true) {
- stdout.write(printDate() + util.format.apply(this, arguments) + "\n");
- checkAndCloseStream();
- }
- };
- /**
- * Print dir (any number of arguments allowed)
- */
- this.dir = function(object) {
- console.dir.apply(this, arguments);
- if(stdout != null && stdout.writable===true) {
- stdout.write(printDate() + util.inspect(object) + "\n");
- checkAndCloseStream();
- }
- };
- /**
- * Print an error (any number of arguments allowed)
- */
- this.error = function() {
- console.error.apply(this, arguments);
- if(stderr != null && stderr.writable===true) {
- stderr.write(printDate() + util.format.apply(this, arguments) + "\n");
- checkAndCloseStream();
- }
- };
- // Alias
- this.err = this.error;
- //Catching all system error
- process.on("uncaughtException", function(err) {
- this.error("Uncaught exception : ");
- this.error(err);
- }.bind(this));
- //We create the stream
- createStream();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement