Java - Logging

java

http://commons.apache.org/logging/commons-logging-1.1/guide.html#Configuration
http://logback.qos.ch/manual/introduction.html
http://hanoo.org/index.php?article=how-to-generate-logs-in-java

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/LogManager.html
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/LogRecord.html

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/package-summary.html
http://docs.oracle.com/javase/6/docs/api/java/util/logging/package-frame.html

http://docs.oracle.com/javase/1.4.2/docs/guide/util/logging/overview.html
http://www.vogella.com/articles/Logging/article.html - done reading
http://onjava.com/pub/a/onjava/2002/06/19/log.html - done reading
http://java.sys-con.com/node/48541

Log4j
SLF4J
JUL
JCL
Logback
http://www.gurock.com/smartinspect/smartinspect-log4j.l.html SmartInspect

What does JUL abbreviate for?

Java Util Logging (namely java.util.logging). Java contains the Java Logging API. This logging API allows you to configure which message types are written. Individual classes can use this logger to write messages to the configured log files.

The java.util.logging package provides the logging capabilities via the Logger class.

How can we create a logger?

To create a logger in your Java code, you can use the following snippet:

import java.util.logging.Logger;

// assumes the current class is called logger
private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());

The Logger you create is actually a hierarchy of Loggers, and a . (dot) in the hierarchy indicates a level in the hierarchy. So if you get a Logger for the com.example key, this Logger is a child of the com Logger and the com Logger is child of the Logger for the empty String. You can configure the main logger and this affects all its children.

How can we configure the root logger?

Not sure. You can configure the main logger and this affects all its children.

What are the various log levels?

  1. SEVERE (highest)
  2. WARNING
  3. INFO
  4. CONFIG
  5. FINE
  6. FINER
  7. FINEST
  8. OFF
  9. ALL

How can we log a message?

import java.util.logging.Logger;

private static Logger theLogger = Logger.getLogger(HelloWorld.class.getName());
theLogger.finest("Hello logging!");

The above code log the message with the severity of FINEST.

What is a log handler?

Each logger can have access to several handlers. The handler receives the log message from the logger and exports it to a certain target. You have several standard handlers:

  1. ConsoleHandler: Write the log message to console
  2. FileHandler: Writes the log message to file

Log levels INFO and higher will be automatically written to the console.

Each Handler has its own Level, in additon to the one for the Logger. So even if we turn up the log level of our Logger, when the logger hands the record to its handler(s), the handler(s) would ignore a Level.FINE record. To get the handler(s) to actually output the record, we need to turn up their level(s), like this:

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogManager;

private static Logger theLogger = Logger.getLogger(HelloWorld.class.getName());

// The root logger's handlers default to INFO. We have to
// crank them up. We could crank up only some of them
// if we wanted, but we will turn them all up.
Handler[] handlers = Logger.getLogger( "" ).getHandlers();
for ( int index = 0; index < handlers.length; index++ ) {
  handlers[index].setLevel( Level.FINE );
}

What is a formatter?

Each handler's output can be configured with a formatter. Available formatters:

  • SimpleFormatter: Generate all messages as text
  • XMLFormatter: Generates XML output for the log messages

How can we build our own formatter?

What is a log manager?

The log manager is responsible for creating and managing the logger and the maintenance of the configuration. We could set the logging level for a package, or even a set of packages, by calling the LogManager.setLevel(String name, Level level) method. So, for example, we could set the logging level of all loggers to Level.FINE by making this call:

LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.FINE);

The LogManager will allow us to control things at more than just the class level. We could set the logging level for a package, or even a set of packages, by calling the LogManager.setLevel(String name, Level level) method. So, for example, we could set the logging level of all loggers for this article to Level.FINE by making this call:

LogManager.getLogManager().setLevel("logging", Level.FINE);

This would make every Logger with the package prefix of "logging" have a level of Level.FINE, unless that particular Logger already had a Level explicitly set.

Any Logger for a class or package below the one being set that has had its Level explicitly set will keep that Level, as will any Logger whose Level is set subsequent to setting the package's Level. To get a Logger to pick up the setting for the package, you can set its Level to null.

Why is it a best practice to use the fully qualified name of each class as the name of your logger?

It is common practice to use the fully qualified name of each class whose activity is being logged as a message category, because this allows developers to fine-tune log settings for each class.

Using the fully qualified class name of your class as the name of your Logger is the approach recommended by the Logging API documentation.

How can we use a custom formatter in our code?

How can we write our own custom logger?

What interfaces and classes are part of JUL?

  • ConsoleHandler
  • FileHander
  • Filter
  • Formatter
  • Handler
  • Level
  • Logger
  • LoggingPermission
  • LogManager
  • LogRecord
  • MemoryHandler
  • SimpleFormatter
  • SocketHandler
  • StreamHandler
  • XMLFormatter

What is Logging Hierarchy?

Once a particular Logger decides to log a LogRecord, the Logger not only passes it to any Handler(s) attached to it, but it also forwards the record to its parent Logger. The parent Logger does not perform a level or Filter check, but instead forwards it to any attached Handler(s) and forwards it to its parent Logger. Eventually, the record reaches the root Logger. The root Logger has a ConsoleHandler that produces the output. Note that this forwarding behavior be controlled via the setUseParentHandlers(boolean) method in the Logger class.

What is a Filter?

If you are using logging and find that you want use more than the log Level of the record when deciding whether to log it, you can look into using Filters. They can be attached to both Loggers and Handlers.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License