Permalink
Browse files

Making timestamps nicer

  • Loading branch information...
1 parent bb9e453 commit 8cbfa76a0af4dd47f412826fb3907f78f6c3bead @saikat committed Sep 13, 2010
Showing with 31 additions and 2 deletions.
  1. +31 −2 lib/socket-logger.js
View
@@ -85,8 +85,7 @@ function makeLogger() {
if (!Array.isArray(msg))
return;
- var currentTime = new Date(),
- timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds() + "." + currentTime.getMilliseconds();
+ timestamp = currentTimeToString();
msg.unshift(timestamp);
logger.buf.push(msg);
} catch (ex) {
@@ -99,5 +98,35 @@ function makeLogger() {
return logger;
}
+function currentTimeToString()
+{
+ var currentTime = new Date();
+ var year = currentTime.getFullYear();
+ var month = currentTime.getMonth() + 1 ;
+ if (month < 10)
+ month = '0' + month;
+ var day = currentTime.getDate();
+ if (day < 10)
+ day = '0' + day;
+ var hours = currentTime.getHours();
+ if (hours < 10)
+ hours = '0' + hours;
+ var minutes = currentTime.getMinutes();
+ if (minutes < 10)
+ minutes = '0' + minutes;
+ var seconds = currentTime.getSeconds();
+ if (seconds < 10)
+ seconds = '0' + seconds;
+
+ var ms = currentTime.getMilliseconds();
+ if (ms < 10)
+ ms = '00' + ms;
+ if (ms < 100)
+ ms = '0' + ms;
+ timestamp = year + "/" + month + "/" + day + ":" + hours
+ + ":" + minutes + ":" + seconds + "." + ms;
+ return timestamp;
+}
+
exports.defaultLogger = makeLogger();
exports.newLogger = makeLogger;

0 comments on commit 8cbfa76

Please sign in to comment.