[Groonga-commit] groonga/gcs [master] Forgot to add logger

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Nov 1 18:38:21 JST 2012


YUKI Hiroshi	2012-11-01 18:38:21 +0900 (Thu, 01 Nov 2012)

  New Revision: e394f772c73526b4feb3d9e7cf45ebd86bb29338
  https://github.com/groonga/gcs/commit/e394f772c73526b4feb3d9e7cf45ebd86bb29338

  Log:
    Forgot to add logger

  Added files:
    lib/logger.js

  Added: lib/logger.js (+64 -0) 100644
===================================================================
--- /dev/null
+++ lib/logger.js    2012-11-01 18:38:21 +0900 (2da50a7)
@@ -0,0 +1,64 @@
+var fs = require('fs');
+var path = require('path');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+
+function Logger(filePath) {
+  this.init(filePath);
+}
+Logger.prototype = {
+  init: function(filePath) {
+    filePath = CLI.resolve(filePath);
+    var flags = path.existsSync(filePath) ? 'a' : 'w';
+    this.stream = fs.createWriteStream(filePath, { flags: flags });
+  },
+  getTimestamp: function() {
+    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;
+  },
+  log: function(message) {
+    var timestamp = '[' + this.getTimestamp + '] ';
+    var indent = timestamp.replace(/./g, ' ');
+
+    message = message.split('\n');
+    this.stream.write(timestamp + message[0] + '\n');
+
+    if (message.length > 1) {
+      message.slice(1).forEach(function(line) {
+        this.stream.write(indent + message + '\n');
+      }, this);
+    }
+  }
+};
+
+function ErrorLogger(filePath) {
+  this.logger = new Logger(filePath);
+}
+ErrorLogger.prototype = {
+  log: function(error) {
+    var message = error;
+    if (typeof error == 'object') {
+      message = error.toString() + '\n' + error.stack;
+    }
+    this.logger.log(message);
+  }
+};
+
+exports.Logger = Logger;
+exports.ErrorLogger = ErrorLogger;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index