Configuring Logger Default Values with a Properties File

The default values for loggers and handlers can be set using a properties file. This example demonstrates a sample logging properties file.

For more information about logging properties, see the lib/logging.properties file in the JRE directory.

# Specify the handlers to create in the root logger
# (all loggers are children of the root logger)
# The following creates two handlers
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

# Set the default logging level for the root logger
.level = ALL

# Set the default logging level for new ConsoleHandler instances
java.util.logging.ConsoleHandler.level = INFO

# Set the default logging level for new FileHandler instances
java.util.logging.FileHandler.level = ALL

# Set the default formatter for new ConsoleHandler instances
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# Set the default logging level for the logger named com.mycompany
com.mycompany.level = ALL
The custom logging properties file is loaded by specifying a system property on the command line:
java -Djava.util.logging.config.file=mylogging.properties <class>

Comments

21 May 2010 - 4:51am by Kiran (not verified)

From this site i got some information

10 Jun 2010 - 2:43pm by gw (not verified)

Can you use unix envrionment variables in your logging.property file? If yes, then how to use them?

9 Mar 2011 - 2:41am by xmedeko (not verified)

Nope, you cannot use environment variables in the logging.properties (or any other Java properties) file. If you need to use them, make some "preprocessor" in a unix shell and run it before you start Java. I mean something like sed s/xxx/yyy/g logging.properties.template > logging.properties

8 May 2011 - 10:07pm by sweetfa (not verified)

You can use environment variables in your properties file if you are using maven as your build tool. Just add the property ${env.YOURVARIABLE} and ensure resource filtering is enabled in your pom.xml.

8 Sep 2011 - 10:52pm by Anonymous (not verified)

is there any way

25 Oct 2011 - 12:22pm by LK (not verified)

As for FileHandler, I can never find any log file generated this way (after program executes logging code). My pattern line is specified as
java.util.logging.FileHandler.pattern = %h/java%u.log
Where should I find java.log on my computer (Windows 7)? I searched whole HD, nothing.

25 Oct 2011 - 5:15pm by Anonymous (not verified)

Probably you did not had it configured correctly, I would try to hardcode a full path, get it to work and then use a variable.

27 Oct 2011 - 3:30pm by LK (not verified)

I tried this:

java.util.logging.FileHandler.level = ALL
java.util.logging.FileHandler.pattern = C:\Temp\java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

and I even changed the pattern line to
java.util.logging.FileHandler.pattern = C:\\Temp\\java%u.log

Logging to file doesn't seem to work. There is no file java.log created in C:\Temp directory. At the same time, console works...

25 Feb 2012 - 6:42pm by Sparky (not verified)

LK: To find your log files, you need to know that %h stands for the value in your user.home variable. [To get that, execute: System.out.println("%h: " + System.getProperty("user.home"));] If you are in Windows XP, that will be C:\Documents and Settings\[your Windows Login ID]. The file name within that directory will be java0.log, java1.log, java2.log or so forth. I'm not sure if Windows 7 uses the same value for %h but just look for whatever the println() statement tells you.

4 May 2012 - 11:29am by Anonymous (not verified)

I tried in windows7, when you use %h/java%u.log, it creates under your user.home directory. That is: c:\users\\java0.log
I am trying to change this path to c:\temp\myLog.log, where temp folder already exists in c drive. but its not working. Has anybody did change to the path and found it created in new path ?

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.