logging.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = [
  6. "getTestLogger",
  7. "initTestLogging",
  8. ];
  9. var {utils: Cu} = Components;
  10. Cu.import("resource://gre/modules/Log.jsm");
  11. this.initTestLogging = function initTestLogging(level) {
  12. function LogStats() {
  13. this.errorsLogged = 0;
  14. }
  15. LogStats.prototype = {
  16. format: function format(message) {
  17. if (message.level == Log.Level.Error) {
  18. this.errorsLogged += 1;
  19. }
  20. return message.time + "\t" + message.loggerName + "\t" + message.levelDesc + "\t" +
  21. this.formatText(message) + "\n";
  22. }
  23. };
  24. LogStats.prototype.__proto__ = new Log.BasicFormatter();
  25. let log = Log.repository.rootLogger;
  26. let logStats = new LogStats();
  27. let appender = new Log.DumpAppender(logStats);
  28. if (typeof(level) == "undefined") {
  29. level = "Debug";
  30. }
  31. getTestLogger().level = Log.Level[level];
  32. Log.repository.getLogger("Services").level = Log.Level[level];
  33. log.level = Log.Level.Trace;
  34. appender.level = Log.Level.Trace;
  35. // Overwrite any other appenders (e.g. from previous incarnations)
  36. log.ownAppenders = [appender];
  37. log.updateAppenders();
  38. return logStats;
  39. }
  40. this.getTestLogger = function getTestLogger(component) {
  41. return Log.repository.getLogger("Testing");
  42. }