Logging Levels


You may want to log messages for special purposes, such as for timing how long a critical method takes to execute or for introducing "trace" messages that allow you as a developer to follow the flow of execution in the system. You may not want these messages to normally appear in the logs when the application is run in production. Instead, you may need the ability to "turn on" these kinds of messages from time to time without having to recode, recompile, and redistribute the application.

As mentioned earlier, the Java logging API supports seven logging levels: severe, warning, info, config, fine, finer, and finest. You should restrict normal production logging to severe, warning, and info levels. Reserve the other levels for special, ephemeral logging needs.

Suppose you need to log the execution time of the Student method getGpa. You are concerned that it may not perform well under heavy load. Introduce logging messages at the beginning and end of getGpa, using the fine Logger method.

 double getGpa() {    Student.logger.fine("begin getGpa " + System.currentTimeMillis());    if (grades.isEmpty())       return 0.0;    double total = 0.0;    for (Grade grade: grades)       total += gradingStrategy.getGradePointsFor(grade);    double result = total / grades.size();    Student.logger.fine("end getGpa " + System.currentTimeMillis());    return result; } 

Recompile and rerun your tests.[12] You won't see these new logging messages either on your console or within a log file.

[12] Here I've chosen to take a lazy logging routeno testingbecause these are temporary log messages.

In order to view the logging messages, you must configure the handler to accept messages at the lower logging level. You typically do this within logging.properties. Edit the logging.properties file and make the modifications noted in bold:

 ... .level= FINE ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ # default file output is in user's home directory. java.util.logging.FileHandler.pattern = %h/java%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 java.util.logging.FileHandler.level = FINE java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter # Limit the message that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = INFO ... 

You designate both the global level (.level) and the FileHandler level (java.util.logging.FileHandler.level) to accept any message logged at a FINE or higher level. The logging facility first ensures that the request level is as high as the global level; if not, it does not send the message to the handler. If the logger sends the message to the handler, the handler similarly determines whether the message should be logged.

In other words, the global should either be the same level or lower than any handler levels. If the reverse is true, no messages will be logged by a handler. For example, if you were to set the global level to INFO and the File Handler to FINE, the FileHandler would never log any messages.

Rerun the tests one more time and ensure that the timing log messages appear in the log file but not on the console.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net