Log entries can be sent to these destinations, as either simple text or as XML:
Level
class defines seven levels of logging enlightenment:
FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE
ALL
and OFF
are defined values as wellLevel
s in code, which may be modified as desired:
CONFIG
to log configuration parametersINFO
to log high-level "heartbeat"
informationSEVERE
FINE
, with FINER
and FINEST
used occasionally, according to taste.LogManager.readConfiguration
.Logger
(usually attached to the package containing the above line of code)Handler
(attached to an application)logging request of some level is made to logger attached to current
package
if the request level is too low for that package's logger {
discard it
}
otherwise {
cycle through all handlers {
if the request level is too low
for that handler {
discard it
}
otherwise {
log the request
}
}
}
Here's an example of a logging configuration file:
# Properties file which configures the operation of the JDK
# logging facility.
# The system will look for this config file, first using
# a System property specified at startup:
#
# >java -Djava.util.logging.config.file=myLoggingConfigFilePath
#
# If this property is not specified, then the config file is
# retrieved from its default location at:
#
# JDK_HOME/jre/lib/logging.properties
# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# (? LogManager docs say no comma here, but JDK example has comma.)
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
# Loggers and Handlers may override this level
.level=INFO
# Loggers
# ------------------------------------------
# Loggers are usually attached to packages.
# Here, the level for each package is specified.
# The global level is used by default, so levels
# specified here simply act as an override.
myapp.ui.level=ALL
myapp.business.level=CONFIG
myapp.data.level=SEVERE
# Handlers
# -----------------------------------------
# --- ConsoleHandler ---
# Override of global logging level
java.util.logging.ConsoleHandler.level=SEVERE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# --- FileHandler ---
# Override of global logging level
java.util.logging.FileHandler.level=ALL
# Naming style for the output file:
# (The output file is placed in the directory
# defined by the "user.home" System property.)
java.util.logging.FileHandler.pattern=%h/java%u.log
# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=50000
# Number of output files to cycle through, by appending an
# integer to the base file name:
java.util.logging.FileHandler.count=1
# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
An application should likely centralize the naming policy for Logger
s,
since switching naming styles becomes a simple edit in one method, instead
of a large number of edits spread throughout the application. Reasons to
alter the naming policy might include:
Logger
name can help distinguish one application's entries from othersHere's an example of using the logging API:
package myapp.business; import java.util.logging.*; /** * Demonstrate Java's logging facilities, in conjunction * with a logging config file. */ public final class SimpleLogger { public static void main(String... args) { SimpleLogger thing = new SimpleLogger(); thing.doSomething(); } public void doSomething() { //Log messages, one for each level //The actual logging output depends on the configured //level for this package. Calls to "inapplicable" //messages are inexpensive. logger.finest("this is finest"); logger.finer("this is finer"); logger.fine("this is fine"); logger.config("this is config"); logger.info("this is info"); logger.warning("this is a warning"); logger.severe("this is severe"); //In the above style, the name of the class and //method which has generated a message is placed //in the output on a best-efforts basis only. //To ensure that this information is always //included, use the following "precise log" //style instead : logger.logp(Level.INFO, this.getClass().toString(), "doSomething", "blah"); //For the very common task of logging exceptions, there is a //method which takes a Throwable : Throwable ex = new IllegalArgumentException("Some exception text"); logger.log(Level.SEVERE, "Some message", ex); //There are convenience methods for exiting and //entering a method, which are at Level.FINER : logger.exiting(this.getClass().toString(), "doSomething"); //Display user.home directory, if desired. //(This is the directory where the log files are generated.) //System.out.println("user.home dir: " + System.getProperty("user.home") ); } // PRIVATE //This style has no hard-coded literals, and requires the logger //to be non-static. private final Logger logger=Logger.getLogger(this.getClass().getPackage().getName()); //This style lets the logger be static, but hard-codes a class literal. //private static final Logger logger = // Logger.getLogger(SimpleLogger.class.getPackage().getName()) //; //This style uses a hard-coded literal and should likely be avoided: //private static final Logger logger = Logger.getLogger("myapp.business"); }
>java -Djava.util.logging.config.file=C:\\Temp\\logging.properties
-cp . myapp.business.SimpleLogger
ConsoleHandler
is configured to show only SEVERE
messages
:
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
SEVERE: this is severe
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
SEVERE: Some message
java.lang.IllegalArgumentException: Some exception text
at myapp.business.SimpleLogger.doSomething(SimpleLogger.java:39)
at myapp.business.SimpleLogger.main(SimpleLogger.java:13)
While the FileHandler
shows ALL
that is sent to it:
Jan 8, 2003 10:43:46 AM myapp.business.SimpleLogger doSomething
CONFIG: this is config
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
INFO: this is info
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
WARNING: this is a warning
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
SEVERE: this is severe
Jan 8, 2003 10:43:47 AM class myapp.business.SimpleLogger doSomething
INFO: blah
Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething
SEVERE: Some message
java.lang.IllegalArgumentException: Some exception text
at myapp.business.SimpleLogger.doSomething(SimpleLogger.java:39)
at myapp.business.SimpleLogger.main(SimpleLogger.java:13)
Example Run 2, showing the ConsoleHandler
output, using the
logging.properties
file which ships with the JDK, and which logs only INFO
level
and above:
>java -cp . myapp.business.SimpleLogger
Jan 8, 2003 10:52:31 AM myapp.business.SimpleLogger doSomething
INFO: this is info
Jan 8, 2003 10:52:31 AM myapp.business.SimpleLogger doSomething
WARNING: this is a warning
Jan 8, 2003 10:52:31 AM myapp.business.SimpleLogger doSomething
SEVERE: this is severe
Jan 8, 2003 10:52:31 AM class myapp.business.SimpleLogger doSomething
INFO: blah
Jan 8, 2003 10:52:31 AM myapp.business.SimpleLogger doSomething
SEVERE: Some message
java.lang.IllegalArgumentException: Some exception text
at myapp.business.SimpleLogger.doSomething(SimpleLogger.java:39)
at myapp.business.SimpleLogger.main(SimpleLogger.java:13)