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.
The custom logging properties file is loaded by specifying a
system property on the command line:
# 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
java -Djava.util.logging.config.file=mylogging.properties <class>
From this site i got some information
Can you use unix envrionment variables in your logging.property file? If yes, then how to use them?
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
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.
is there any way
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.
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.
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...
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.
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 ?