Skip to content

Commit

Permalink
Output log with local timestamp correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Nov 5, 2012
1 parent cceb2ac commit e3f2578
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
32 changes: 27 additions & 5 deletions lib/logger.js
Expand Up @@ -34,9 +34,31 @@ function logQuery(selectQuery, elapsedTime) {
}
exports.query = logQuery;

function setLogFilePath(context, path) {
var logger = getLogger(context);
logger.add(winston.transports.File, { filename: path });
logger.add(winston.transports.File, { filename: path, handleExceptions: true });
function getLocalISOTimestamp() {
var date = new Date();
var offsetMinutes = date.getTimezoneOffset();
var timezoneOffset = ('0' + (Math.abs(offsetMinutes) / 60)).slice(-2) + ':' +
('0' + (Math.abs(offsetMinutes) % 60)).slice(-2);
if (offsetMinutes > 0) {
timezoneOffset = '-' + timezoneOffset;
} else {
timezoneOffset = '+' + timezoneOffset;
}
return date.getFullYear() + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + (date.getDate())).slice(-2) + 'T' +
('0' + (date.getHours())).slice(-2) + ':' +
('0' + (date.getMinutes())).slice(-2) + ':' +
('0' + (date.getSeconds())).slice(-2) + '.' +
date.getMilliseconds() +
timezoneOffset;
}

function addFileTransport(context, filename, options) {
options = options || {};
options.filename = filename;
options.json = false;
options.timestamp = getLocalISOTimestamp;
getLogger(context).add(winston.transports.File, options);
}
exports.setLogFilePath = setLogFilePath;
exports.addFileTransport = addFileTransport;
6 changes: 4 additions & 2 deletions lib/server.js
Expand Up @@ -33,10 +33,12 @@ function prepareLoggers(application, config) {
application.use(express.logger({ stream: accessLogStream }));
}
if (config.queryLogPath) {
logger.setLogFilePath('query', CLI.resolve(config.queryLogPath));
logger.addFileTransport('query', CLI.resolve(config.queryLogPath));
}
if (config.errorLogPath) {
logger.setLogFilePath('error', CLI.resolve(config.errorLogPath));
logger.addFileTransport('error',
filename: CLI.resolve(config.errorLogPath),
{ handleExceptions: true });
}
}

Expand Down

0 comments on commit e3f2578

Please sign in to comment.