Logging Notes

In reviewing all the discussion comparing Log4j vs Sun's logging API the only feature of Log4j that interests me is "Log4j includes log analysis GUIs: Chainsaw and LogFactor5" (LogFactor5 appears to be defunct). Everything else is a lot of "PR" with no specifics to back it up. Even the founder of Log4j states "the log4j and JSR47 APIs are very similar".

Thus we are left with the argument: "most" Java developers are familar with Log4j vs the need to add in Log4j as a 3rd party JAR to all projects.

Key Terminology:

Logger - same in both Sun and Log4j - an object to which messages can be logged.

LogManager (Sun) - not sure what the Log4j equivalent is. Manages a namespace in which all Loggers are stored. Manages control properties. The LogManager can be used to tell what properties file should be read.

Description / comments:
Handler Appender Handle where a log message goes (to a stream, file, console, socket, etc).
Formatter Layout Configures the format of the log messages.

Handler / Appenders


Formatter / Layout

By default Sun formats log messages in XML and includes date, time, class and method information. Log4j just dumps the message as plain text with no additional info.

Key Differences:

Predefined Log Levels
* Note: "TRACE" is not commonly documented in Log4j documentation. Trace was introduced in Log4j version 1.2.12.

Configuration order - Sun's logger is top down, Log4j is bottom up. Thus with Sun's approach you have to be careful of configuration order (setting a log level for foo.sub and then setting the log level for foo will result in the higher level (foo) setting overwriting the lower level (foo.sub) setting).

Log4j has inherited handlers.

Log4j configuration can be by both "property files" and "XML formatted configuration files", Sun only has "property files". (Both support in code setting of configuration.)

Log4j throws no exceptions. I'm really not sure if this is a bug or a feature. Log4j advocates try to make the point that if the logging code encounters a RunTimeException the application will crash and implies that can't happen in Log4j.

With logging turned on Log4j is roughly three times slower (this is a statement by the Log4j project founder). However Log4j is 4 to 100 times faster if localization is used but not needed (Huh?).

Since the JavaDocs for Log4j are so bad you really should buy the Log4j manual (PDF - $20).

Common Use Cases:

Sun Logging
Log4j Logging*
Trivial Case: log to console
Logger logger = Logger.getLogger("Console.Example");
logger.severe("Very severe, fatal problem");

Logger logger = Logger.getLogger("Console.Example");
logger.fatal("Very severe, fatal problem");

Simple Common Usage: log to a single file

try {
  Handler handler = new FileHandler("SunSingleLogFile.log");
  handler.setFormatter(new SimpleFormatter());
  logger = Logger.getLogger("package.name");
} catch (IOException e) {
  System.out.println("IOException constructing a FileHandler");

try {
FileAppender fileAppender = new FileAppender(new SimpleLayout(), "Log4J_logFile.txt");
} catch (IOException e) {
  System.out.println("Log4J configuration failed!");

Note: Log4j does not include date, package & method information automatically.

Common Usage: log to rotating files
int sizeLimit = 0; // 0 means no limit
int numRotate = 3; // 3 means rotate through three files
boolean append = false; // false is do not append
String fileNamePattern = "OurLogFile_" + dayStr + "_%g.log";
Handler handler =
new FileHandler(fileNamePattern, sizeLimit, numRotate, append);

Write a custom appender
Configure via configuration files
try {
} catch (IOException e) {
System.out.println("Read Log config error "+e);


* Note: Log4j fails for a trivial usage if the default properties file (log4j.properties or log4j.xml) is not found unless a configuration command is executed (such as BasicConfigurator.configure();).


To avoid the cost of constructing a message (when a log event will not actually be generated) use:

(Sun) logger.isLoggable(level)

(Log4j) logger.isEnabledFor(level) - Note: Log4j has convenience methods isDebugEnabled() and isInfoEnabled()

Log4j Website: http://logging.apache.org/log4j/1.2/index.html

Sun Logging: http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html

Log4j Notes

Sun Logging Notes