Java Reference
In-Depth Information
Solution
Utilize SLF4J within your application, along with the Java Logging API, to implement
a logging solution. The following example first creates a logger object with the name
of recipeLogger . The example then proceeds to log an informational message, a
warning message, and an error message:
private void loadLoggingConfiguration() {
FileInputStream ins = null;
try {
ins = new FileInputStream(new
File("logging.properties"));
LogManager.getLogManager().readConfiguration(ins);
} catch (IOException e) {
e.printStackTrace();
}
}
private void start() {
loadLoggingConfiguration();
Logger logger
= LoggerFactory.getLogger("recipeLogger");
logger.info("Logging for the first Time!");
logger.warn("A warning to be had");
logger.error("This is an error!");
}
How It Works
The loadLogConfiguration() function opens a stream to the log-
ging.properties file and passes it to
java.util.logging.LogManager() . Doing so configures the
java.util.logging framework to use the settings specified in the log-
ging.properties file. Then, within the start method of the recipe, the code ac-
quires a logger object named recipeLogger . The example proceeds to log mes-
sages to through recipeLogger . More information on the actual logging parameters
can be found in Recipe 9-10.
Search WWH ::




Custom Search