Logger Handler Question

R

Rhino

Can anyone help a logging newbie with a question?

I am just trying the logger classes in 1.4.1 for the first time and I don't
see this answered in the Lgging overview or in the Logging API itself.

I've added some logging logic to one of my programs and created a single
FileHandler for the logger to use. I did not create a console handler. When
I run my program, the logging that I did in the program appears just fine in
the file specified for the FileHandler but it also appears on my console
where I don't want it. I thought perhaps the logger was logging to a console
handler by default but when I tried logger.getHandlers() there is only one
handler, the FileHandler. How are my log messages winding up in the console
and how can I get them to appear only in the Handlers that are added to my
logger, the FileHandler in this case?


Rhino
 
R

Rhino

I found the answer to my own question.

The logging.properties file, found in <jdk-path>/jre/lib/logging.properties
on my Windows XP system, has a property called 'handlers'. If that property
is set to 'java.util.logging.ConsoleHandler' as mine was, this seems to
force the logger to write everything to the console as well as to any
additional handlers that you add to the logger.

I changed my 'handlers' property as follows:
handlers=

and the logger stopped writing to the console and wrote only to the
FileHandler I defined within my program.

Another approach is the following:
handlers= java.util.logging.FileHandler

This should ensure that a default log is written to the default location
('%h/java%u.log') *AND* to whatever handlers you specify in your
application. Of course, if you specify a FileHandler in your application in
addition to the one in the logging.properties file, you will get TWO logs
written, one to the default location and one to the location you specified
in your application.

Rhino
 
T

Takuya Murata

Rhino said:
I found the answer to my own question.

Maybe a slightly simpler solution would be to remove a default handler
dynamically as follows:

Logger logger = Logger.getLogger ("");
if (logger.getHandlers ().length > 0)
logger.removeHandler (logger.getHandlers ()[0]);

It seems every LogRecord is passed to the root logger whatever handler
a logger is attached to. Very weird and annoying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,207
Latest member
Best crypto consultant

Latest Threads

Top