Refactored logging system so that clients don't need to call factory functions to create specific instances of formatters and appenders.
authorAtul Varma <varmaa@toolness.com>
Thu, 29 May 2008 18:15:50 -0700
changeset 44489 1fad1f16ec3dcc44659930efa9357f32c76d49eb
parent 44488 1da426da17c0d0f0aa251e26a3d22804759bd8c9
child 44492 d11284c8ad1c3b30a4d7ad8e31b6efb2c563c7a9
push idunknown
push userunknown
push dateunknown
Refactored logging system so that clients don't need to call factory functions to create specific instances of formatters and appenders.
services/sync/modules/log4moz.js
services/sync/modules/service.js
--- a/services/sync/modules/log4moz.js
+++ b/services/sync/modules/log4moz.js
@@ -81,19 +81,23 @@ let Log4Moz = {
   },
 
   get Service() {
     delete Log4Moz.Service;
     Log4Moz.Service = new Log4MozService();
     return Log4Moz.Service;
   },
 
+  get Formatter() { return Formatter; },
+  get BasicFormatter() { return BasicFormatter; },
   get Appender() { return Appender; },
-  get Formatter() { return Formatter; },
-  get BasicFormatter() { return BasicFormatter; }
+  get DumpAppender() { return DumpAppender; },
+  get ConsoleAppender() { return ConsoleAppender; },
+  get FileAppender() { return FileAppender; },
+  get RotatingFileAppender() { return RotatingFileAppender; }
 };
 
 
 /*
  * LogMessage
  * Encapsulates a single log event's data
  */
 function LogMessage(loggerName, level, message){
@@ -411,16 +415,22 @@ FileAppender.prototype = {
 FileAppender.prototype.__proto__ = new Appender();
 
 /*
  * RotatingFileAppender
  * Similar to FileAppender, but rotates logs when they become too large
  */
 
 function RotatingFileAppender(file, formatter, maxSize, maxBackups) {
+  if (maxSize === undefined)
+    maxSize = ONE_MEGABYTE * 2;
+
+  if (maxBackups === undefined)
+    maxBackups = 0;
+
   this._name = "RotatingFileAppender";
   this._file = file; // nsIFile
   this._formatter = formatter;
   this._maxSize = maxSize;
   this._maxBackups = maxBackups;
 }
 RotatingFileAppender.prototype = {
   doAppend: function RFApp_doAppend(message) {
@@ -470,45 +480,10 @@ Log4MozService.prototype = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
 
   get rootLogger() {
     return this._repository.rootLogger;
   },
 
   getLogger: function LogSvc_getLogger(name) {
     return this._repository.getLogger(name);
-  },
-
-  newAppender: function LogSvc_newAppender(kind, formatter) {
-    switch (kind) {
-    case "dump":
-      return new DumpAppender(formatter);
-    case "console":
-      return new ConsoleAppender(formatter);
-    default:
-      dump("log4moz: unknown appender kind: " + kind);
-      return;
-    }
-  },
-
-  newFileAppender: function LogSvc_newAppender(kind, file, formatter) {
-    switch (kind) {
-    case "file":
-      return new FileAppender(file, formatter);
-    case "rotating":
-      // FIXME: hardcoded constants
-      return new RotatingFileAppender(file, formatter, ONE_MEGABYTE * 2, 0);
-    default:
-      dump("log4moz: unknown appender kind: " + kind);
-      return;
-    }
-  },
-
-  newFormatter: function LogSvc_newFormatter(kind) {
-    switch (kind) {
-    case "basic":
-      return new BasicFormatter();
-    default:
-      dump("log4moz: unknown formatter kind: " + kind);
-      return;
-    }
   }
 };
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -222,45 +222,45 @@ WeaveSvc.prototype = {
     }
   },
 
   _initLogs: function WeaveSync__initLogs() {
     this._log = Log4Moz.Service.getLogger("Service.Main");
     this._log.level =
       Log4Moz.Level[Utils.prefs.getCharPref("log.logger.service.main")];
 
-    let formatter = Log4Moz.Service.newFormatter("basic");
+    let formatter = new Log4Moz.BasicFormatter();
     let root = Log4Moz.Service.rootLogger;
     root.level = Log4Moz.Level[Utils.prefs.getCharPref("log.rootLogger")];
 
-    let capp = Log4Moz.Service.newAppender("console", formatter);
+    let capp = new Log4Moz.ConsoleAppender(formatter);
     capp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.console")];
     root.addAppender(capp);
 
-    let dapp = Log4Moz.Service.newAppender("dump", formatter);
+    let dapp = new Log4Moz.DumpAppender(formatter);
     dapp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.dump")];
     root.addAppender(dapp);
 
     let brief = this._dirSvc.get("ProfD", Ci.nsIFile);
     brief.QueryInterface(Ci.nsILocalFile);
     brief.append("weave");
     brief.append("logs");
     brief.append("brief-log.txt");
     if (!brief.exists())
       brief.create(brief.NORMAL_FILE_TYPE, PERMS_FILE);
 
     let verbose = brief.parent.clone();
     verbose.append("verbose-log.txt");
     if (!verbose.exists())
       verbose.create(verbose.NORMAL_FILE_TYPE, PERMS_FILE);
 
-    this._briefApp = Log4Moz.Service.newFileAppender("rotating", brief, formatter);
+    this._briefApp = new Log4Moz.RotatingFileAppender(brief, formatter);
     this._briefApp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.briefLog")];
     root.addAppender(this._briefApp);
-    this._debugApp = Log4Moz.Service.newFileAppender("rotating", verbose, formatter);
+    this._debugApp = new Log4Moz.RotatingFileAppender(verbose, formatter);
     this._debugApp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.debugLog")];
     root.addAppender(this._debugApp);
   },
 
   clearLogs: function WeaveSvc_clearLogs() {
     this._briefApp.clear();
     this._debugApp.clear();
   },