Created
May 14, 2012 18:25
-
-
Save shawn-simon/2695511 to your computer and use it in GitHub Desktop.
Chris' basic node logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 1999-2012 TeaLeaf Technology, Inc. | |
* All rights reserved. | |
* | |
*/ | |
var fs = require('fs'); | |
function SimpleLogger(logName, logDir, rollDays, consoleOutput) { | |
if(logDir == undefined || logDir == '') | |
logDir = './'; | |
else if(logDir.substr(logDir.length-1) != '/') | |
logDir += '/'; | |
this.logName = logName == 'undefined' ? 'Unknown' : logName; | |
this.logDir = logDir; | |
this.rollDays = rollDays; | |
this.consoleOutput = consoleOutput; | |
this.logWriters = []; | |
initRollTimer(this); | |
} | |
SimpleLogger.prototype.trace = function (source, message) { | |
writeLog(this, 'TRACE', source, message); | |
} | |
SimpleLogger.prototype.info = function(source, message) { | |
writeLog(this, 'INFO', source, message); | |
} | |
SimpleLogger.prototype.error = function(source, message) { | |
writeLog(this, 'ERROR', source, message); | |
} | |
SimpleLogger.prototype.log = function(type, source, message) { | |
writeLog(this, type, source, message); | |
} | |
function writeLog (log, type, source, message) { | |
var now = new Date(); | |
var todaysLog = logFilename(log, now); | |
var logWriter; | |
if(log.logWriters[todaysLog] == undefined) { | |
log.logWriters = []; | |
logWriter = log.logWriters[todaysLog] = fs.createWriteStream(todaysLog, {'flags': 'a'}); | |
} | |
else | |
logWriter = log.logWriters[todaysLog]; | |
var logMsg = ' ' + type + ' ' + source + '\t' + message; | |
if(log.consoleOutput) | |
console.log(formatDate(now, false, '', true, ':') + logMsg); | |
logWriter.write(formatDate(now, true, '-', true, ':') + logMsg + '\n'); | |
} | |
function padNumber(input) { | |
return input < 10 ? '0' + input : input; | |
} | |
function logFilename(log, input) { | |
return log.logDir + log.logName | |
+ '_' + formatDate(input, true, '', false, '') + '.log'; | |
} | |
//[CF] Using a custom date format function to eliminate the need for an additional module | |
function formatDate(input, includeDate, dateSeparator, includeTime, timeSeparator) { | |
var retString = ''; | |
if(input == undefined) return retString; | |
if(includeDate) { | |
retString = input.getFullYear() + dateSeparator + padNumber(input.getMonth()+1) | |
+ dateSeparator + padNumber(input.getDate()); | |
} | |
if(includeTime) { | |
if(includeDate) | |
retString += 'T'; | |
retString += padNumber(input.getHours()) + timeSeparator + padNumber(input.getMinutes()) + timeSeparator | |
+ padNumber(input.getSeconds()); | |
} | |
return retString; | |
} | |
//Roll logs at 1am every day | |
function initRollTimer(log) { | |
var hourToRoll = 1; | |
var now = new Date(); | |
var rollTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), | |
hourToRoll, 0, 0, 0); | |
rollTime.setDate(rollTime.getDate()+1); | |
var timerDelay = rollTime.getTime() - now.getTime(); | |
setTimeout(rollLogs, timerDelay); | |
} | |
//[CF] Current roll logic only deletes a single day, it will not go back any further | |
function rollLogs(log) { | |
var curDate = new Date(); | |
curDate.setDate(curDate.getDate()-log.rollDays); | |
var fileToDelete = logFilename(log, curDate); | |
fs.unlink(fileToDelete); | |
initRollTimer(log); | |
} | |
module.exports = SimpleLogger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment