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

Satya - Wednesday, March 02, 2005 3:20:45 PM

What are the basics

You can find that out from this O'Reilly article

Satya - Wednesday, March 02, 2005 3:24:08 PM

JDK 1.4 logger is class based

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?

Satya - Wednesday, March 02, 2005 3:27:26 PM

Granularity of the logger

Severe
warning
info
config
fine
finer
finest

annonymous - Wednesday, March 02, 2005 3:28:08 PM

What is the displayed guy by default

The default is set to info

Satya - Wednesday, March 02, 2005 3:45:09 PM

Which logging should one use? Log4J or Suns

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

Satya - Wednesday, March 02, 2005 4:01:31 PM

Where can I find more about suns logging

Here is the jdk documentation on logging

Satya - Thursday, March 03, 2005 2:19:41 PM

How do I configure the logging

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

Satya - Thursday, March 03, 2005 2:23:04 PM

What does this file look like?

Here is a sample file from the jdk 142

Satya - Thursday, March 03, 2005 3:16:23 PM

Package description for java logging

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

Satya - Thursday, March 03, 2005 4:09:31 PM

How does webapps integrate with JDK 1.4 logging?

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

Satya - Thursday, March 03, 2005 6:44:16 PM

What is the difference between Logger.getLogger() and LogManager.getLogger()

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

Satya - Friday, March 04, 2005 9:03:13 AM

Is there anyway to load the handlers automatically from a properties file?

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?

Satya - Friday, March 04, 2005 9:16:42 AM

Another example properties file


.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

Satya - Friday, March 04, 2005 9:19:19 AM

What is the meaning of the above properties file?

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.

Satya - Friday, March 04, 2005 9:22:56 AM

It seems to be important to read the properties files before getting the loggers

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.