Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.
add("\n");
} public void flush() {} public void close() {} }); logger.warning("Logging Warning"); logger.info("Logging Info"); System.out.print(strHolder); monitor.expect(new String[] { "%% .* CustomHandler main", "WARNING: Logging Warning", "%% .* CustomHandler main", "INFO: Logging Info", "[WARNING:, CustomHandler:, main:, " + "<Logging Warning>, ", ", INFO:, CustomHandler:, main:, <Logging Info>, ", 988 Thinking in Java www.BruceEckel.com "]" }); } } ///:~ The console output comes from the root logger. When the ArrayList is printed you can see that only selected information has been captured into the object. Feedback Filters When you write the code to send a logging message to a Logger object, you often decide at the time you’re writing the code what level the logging message should be (the logging API certainly allows you to devise more complex systems wherein the level of the message can be determined dynamically, but this is less common in practice). The Logger object has a level that can be set so that it can decide what level of message to accept; all others will be ignored. This can be thought of as a basic filtering functionality, and it’s often all you need. Feedback Sometimes, however, you need more sophisticated filtering, so that you can decide whether to accept or reject a message based on something more than just the current level. To accomplish this you can write custom Filter objects. Filter is an interface that has a single method, boolean isLoggable(LogRecord record), which decides whether or not this particular LogRecord is interesting enough to report. Feedback Once you create a Filter, you register it with either a Logger or a Handler using the setFilter( ) method. For example, suppose you’d like to only log reports about Ducks:Feedback //: c15:SimpleFilter.java import com.bruceeckel.simpletest.*; import java.util.logging.*; public class SimpleFilter { private static Test monitor = new Test(); private static Logger logger = Logger.getLogger("SimpleFilter"); static class Duck {}; static class Wombat {}; static void sendLogMessages() { Chapter 15: Discovering problems 989 logger.log(Level.WARNING, "A duck in the house!", new Duck()); logger.log(Level.WARNING, "A Wombat at large!", new Wombat()); } public static void main(String[] args) { sendLogMessages(); logger.setFilter(new Filter() { public boolean isLoggable(LogRecord record) { Object[] params = record.getParameters(); if(params == null) return true; // No parameters if(record.getParameters()[0] instanceof Duck) return true; // Only log Ducks return false; } }); logger.info("After setting filter.."); sendLogMessages(); monitor.expect(new String[] { "%% .* SimpleFilter sendLogMessages", "WARNING: A duck in the house!", "%% .* SimpleFilter sendLogMessages", "WARNING: A Wombat at large!", "%% .* SimpleFilter main", "INFO: After setting filter..", "%% .* SimpleFilter sendLogMessages", "WARNING: A duck in the house!" }); } } ///:~ Before setting the Filter, messages about Ducks and Wombats are reported. The Filter is created as an anonymous inner class which looks at the LogRecord parameter to see if a Duck was passed as an extra argument to the log( ) method—if so, it returns true to indicate that the message should be processed.Feedback Notice that the signature of getParameters( ) says that it will return an Object[]. However, if no additional arguments have been passed to the log( ) method, getParameters( ) will return null (in violation of its signature—this is a bad programming practice). So instead of assuming that an array is returned (as promised) and checking to see if it is of zero 990 Thinking in Java www.BruceEckel.com length, we must check for null. If you don’t do this correctly, then the call to logger.info( ) will cause an exception to be thrown. Feedback Formatters A Formatter is a way to insert a formatting operation into a Handler’s processing steps. If you register a Formatter object with a Handler, then before the LogRecord is published by the Handler, it is first sent to the Formatter. After formatting, the LogRecord is returned to the Handler, which then publishes it. Feedback To write a custom Formatter, extend the Formatter class and override format(LogRecord record). Then, register the Formatter with the Handler using the setFormatter( ) call, as seen here: Feedback //: c15:SimpleFormatterExample.java import com.bruceeckel.simpletest.*; import java.util.logging.*; import java.util.*; public class SimpleFormatterExample { private static Test monitor = new Test(); private static Logger logger = Logger.getLogger("SimpleFormatterExample"); private static void logMessages() { logger.info("Line One"); logger.info("Line Two"); } public static void main(String[] args) { logger.setUseParentHandlers(false); Handler conHdlr = new ConsoleHandler(); conHdlr.setFormatter(new Formatter() { public String format(LogRecord record) { return record.getLevel() + " : " + record.getSourceClassName() + " -:- "
|
Wątki
|