Log.jsm

This module is still under development.
Until stabilized, expect frequent updates, including different interfaces and behavior in different versions of the platform.

Progress is tracked in Bugzilla using dependencies of bug 451283.

The Log.jsm JavaScript code module (formerly named log4moz.js) provides a log4j style API for logging log messages to various endpoints, such as the Browser Console or a file on disk. To use it, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/Log.jsm");

Basic usage

Components.utils.import("resource://gre/modules/Log.jsm");

// Get a logger, give it a name unique to this chunk of code.
// Use dots to create a hierarchy, this way you can later change
// the log level of sets of loggers under some common root
let log = Log.repository.getLogger("MyExtension.MyClass");
log.level = Log.Level.Debug;
// A console appender logs to the browser console.
log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
// A dump appender logs to stdout.
log.addAppender(new Log.DumpAppender(new Log.BasicFormatter()));

// Log some messages
log.error("Oh noes!! Something bad happened!");
log.debug("Details about bad thing only useful during debugging", {someInfo: "nothing"});
log.warn("Here is an error", new Error("ouch"));

Logger levels

FATAL Severe errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROR Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARN Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
INFO Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
CONFIG Information regarding important configuration options the system is using that affect how it runs.
DEBUG Detailed information on the flow through the system. Expect these to be written to logs only.
TRACE Most detailed information. Expect these to be written to logs only.

Class overview

Appender(); length: 1
Keys of prototype:
append();
doAppend();
level: 0
BasicFormatter(); length: 1
Keys of prototype:
format();
BoundedFileAppender(); length: 2
Keys of prototype:
doAppend();
reset();
ConsoleAppender(); length: 1
Keys of prototype:
doAppend();
DumpAppender(); length: 1
Keys of prototype:
doAppend();
FileAppender(); length: 2
Keys of prototype:
doAppend();
reset();
Formatter(); length: 0
Keys of prototype:
format();
LogMessage(); length: 4
Keys of prototype:
levelDesc
Logger(); length: 2
Keys of prototype:
addAppender();
level
logStructured();
name
parent
removeAppender();
updateAppenders();
And the methods mentioned below: Logger methods
LoggerRepository(); length: 0
Keys of prototype:
getLogger();
rootLogger
StorageStreamAppender(); length: 1
Keys of prototype:
doAppend();
getInputStream();
newOutputStream();
outputStream
reset();
StructuredFormatter(); length: 0
Keys of prototype:
format();

Method overview

enumerateInterfaces(); length: 0
enumerateProperties(); length: 2

Member fields

Variable Type Description
Level Object Contains the following fields:
Field Name Value
All 0
Config 30
Debug 20
Desc { 0: "ALL", 10: "TRACE", 20: "DEBUG", 30: "CONFIG", 40: "INFO", 50: "WARN", 60: "ERROR", 70: "FATAL" }
Error 30
Fatal 70
Info 40
Numbers { "ALL": 0, "TRACE": 10, "DEBUG": 20, "CONFIG": 30, "INFO": 40, "WARN": 50, "ERROR": 60, "FATAL": 70 }
Trace 10
Warn 50
repository LoggerRepository

Logger methods

void fatal(string text, [optional] Object params);
void error(string text, [optional] Object params);
void warn(string text, [optional] Object params);
void info(string text, [optional] Object params);
void config(string text, [optional] Object params);
void debug(string text, [optional] Object params);
void trace(string text, [optional] Object params);