bug 467085 log4moz updateParents messed up, doesn't support multilevel parenting properly, r=thunder
authorSiddharth Agarwal <sid1337@gmail.com>
Tue, 02 Dec 2008 21:42:20 +0530
changeset 45077 7d03fe80909f793a59b4bdb5f0b7cede70b0f91e
parent 45076 66d3bdc803f093968127327655de29389d5bd69c
child 45078 53946805015941a79b8447a1f3545168103fa59b
push idunknown
push userunknown
push dateunknown
reviewersthunder
bugs467085
bug 467085 log4moz updateParents messed up, doesn't support multilevel parenting properly, r=thunder
services/sync/modules/log4moz.js
services/sync/tests/unit/test_log4moz.js
--- a/services/sync/modules/log4moz.js
+++ b/services/sync/modules/log4moz.js
@@ -278,34 +278,36 @@ LoggerRepository.prototype = {
   //  this._rootLogger = logger;
   //},
 
   _updateParents: function LogRep__updateParents(name) {
     let pieces = name.split('.');
     let cur, parent;
 
     // find the closest parent
-    for (let i = 0; i < pieces.length; i++) {
+    // don't test for the logger name itself, as there's a chance it's already
+    // there in this._loggers
+    for (let i = 0; i < pieces.length - 1; i++) {
       if (cur)
         cur += '.' + pieces[i];
       else
         cur = pieces[i];
       if (cur in this._loggers)
         parent = cur;
     }
 
-    // if they are the same it has no parent
-    if (parent == name)
+    // if we didn't assign a parent above, there is no parent
+    if (!parent)
       this._loggers[name].parent = this.rootLogger;
     else
       this._loggers[name].parent = this._loggers[parent];
 
     // trigger updates for any possible descendants of this logger
     for (let logger in this._loggers) {
-      if (logger != name && name.indexOf(logger) == 0)
+      if (logger != name && logger.indexOf(name) == 0)
         this._updateParents(logger);
     }
   },
 
   getLogger: function LogRep_getLogger(name) {
     if (!name)
       name = this.getLogger.caller.name;
     if (name in this._loggers)
--- a/services/sync/tests/unit/test_log4moz.js
+++ b/services/sync/tests/unit/test_log4moz.js
@@ -19,9 +19,27 @@ function run_test() {
   appender.level = Log4Moz.Level.Info;
   log.addAppender(appender);
   log.info("info test");
   log.debug("this should be logged but not appended.");
 
   do_check_eq(appender.messages.length, 1);
   do_check_true(appender.messages[0].indexOf("info test") > 0);
   do_check_true(appender.messages[0].indexOf("INFO") > 0);
+
+  // Test - check whether parenting is correct
+  let grandparentLog = Log4Moz.repository.getLogger("grandparent");
+  let childLog = Log4Moz.repository.getLogger("grandparent.parent.child");
+  do_check_eq(childLog.parent.name, "grandparent");
+
+  let parentLog = Log4Moz.repository.getLogger("grandparent.parent");
+  do_check_eq(childLog.parent.name, "grandparent.parent");
+
+  // Test - check that appends are exactly in scope
+  let gpAppender = new MockAppender(new Log4Moz.BasicFormatter());
+  gpAppender.level = Log4Moz.Level.Info;
+  grandparentLog.addAppender(gpAppender);
+  childLog.info("child info test");
+  log.info("this shouldn't show up in gpAppender");
+
+  do_check_eq(gpAppender.messages.length, 1);
+  do_check_true(gpAppender.messages[0].indexOf("child info test") > 0);
 }