Manage this page

0. Feedback

1. Display

Links

1. An introduction to the java logging api from O'Reilly

2. java forums from sun

142 api

142 api Cheat sheat

You can find that out from this O'Reilly article

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

See how you have to get a logger and keep it static for each class.

The question that begs to be asked is will the "getLogger" function does a hashtable look up or creates a new one every time?

Severe
warning
info
config
fine
finer
finest

The default is set to info

Apparently the Apache commons logging is an interface that can work with both. Go figure one more choice now.

Here is the jdk documentation on logging

Using a config file jre/lib/logging.properties.

Here is a sample file from the jdk 142

From the jdk api for the logging pacakage. This talks about the intent of the logging. I was hoping to see more, but that is not there

All of the logging is controlled by a global object called LogManager. This being a static variable (is it?), there is one LogManager for each webapp. So each LogManager can have its own configuration.

Using its readconfiguration method it can read its own configuration separate from other log managers.

How about the containers log manager such as tomcat? I am hoping it has its own. I am hoping when a webapp asks for a log manager what you receive is not from the tomcats logmanager

LogManager's getLogger() will return a logger only if it exists. I think the Logger.getLogger() will create a new one if it doesn't exist.

Initially if you are seeing a null Logger, just check which call you made

Consider instantiating a logger


Logger testLogger = Logger.getLogger("test");

At that time the test logger does not have any handlers attached to it. You can attach them programmatically.

But can you attach them via a properties load file? Example:


test.handlers=handler1,handler2 etc.

I have seen some home grown solutions do this. Is this something you have to write on your own or such a facility exists in the jdk 1.4 logging?


.level = ALL
test.level=ALL

java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

#This doesn't seem to work with out 
#extra help from the source code.
test.handlers=java.util.logging.ConsoleHandler

There is a logger called "test". Its level is set to log all messages of any severity. This logger uses a handler called console handler. Based on previous notes you may have to register this yourself programmatically.

Once registered, you can set the formatter and the level for that console handler.

Example


initLogManager();
		
Logger m_jdkLogger = Logger.getLogger("test");
ConsoleHandler conHandler = new ConsoleHandler();
m_jdkLogger.addHandler(conHandler);
		
m_jdkLogger.info("Hello");

See how the logmanager is initialized first before getting the logger. Here is the initialization code


InputStream is = new FileInputStream("somefile");
LogManager lm = LogManager.getLogManager();
lm.readConfiguration(is);
is.close();

This doesn't seem to work if I do the read after I get the logger.