[ Pobierz całość w formacie PDF ]
.addHandler(new FileHandler("LogToFile.xml"));logger.info("A message logged to the file");monitor.expect(new Object[] {"%%.* LogToFile main","INFO: A message logged to the file"});}} ///:~When you run this program, youll notice two things.First, even though were sending output to a file, youll still see console output.Thats because each message is converted to a LogRecord, which is first used by the local logger object, which passes it to its own handlers.At this point the LogRecord is passed to the parent object, which has its own handlers.This process continues until the root logger is reached.The root logger comes with a default ConsoleHandler, so the message appears on the screen as well as appearing in the log file (you can turn off this behavior by calling setUseParentHandlers(false)).The second thing youll notice is that the contents of the log file is in XML format, which will look something like this:<?xml version="1.0" standalone="no"?><!DOCTYPE log SYSTEM "logger.dtd"><log><record><date>2002-07-08T12:18:17</date><millis>1026152297750</millis><sequence>0</sequence><logger>LogToFile</logger><level>INFO</level><class>LogToFile</class><method>main</method><thread>10</thread><message>A message logged to the file</message></record></log>The default output format for a FileHandler is XML.If you want to change the format, you must attach a different Formatter object to the handler.Here, a SimpleFormatter is used for the file in order to output as plain text format://: X1:LogToFile2.javaimport com.bruceeckel.simpletest.*;import java.util.logging.*;public class LogToFile2 {static Test monitor = new Test();private static Logger logger =Logger.getLogger("LogToFile2");public static void main(String[] args)throws Exception {FileHandler logFile =new FileHandler("LogToFile2.txt");logFile.setFormatter(new SimpleFormatter());logger.addHandler(logFile);logger.info("A message logged to the file");monitor.expect(new Object[] {"%%.* LogToFile2 main","INFO: A message logged to the file"});}} ///:~The LogToFile2.txt file will look like this:Jul 8, 2002 12:35:17 PM LogToFile2 mainINFO: A message logged to the fileMultiple HandlersYou can register multiple handlers with each Logger object.When a logging request comes to the Logger, it notifies all the handlers that have been registered with it, as long as the logging level for the Logger is greater than or equal to that of the logging request.Each handler, in turn, has its own logging level; if the level of the LogRecord is greater than or equal to the level of the handler, then that handler publishes the record.Heres an example that adds a FileHandler and a ConsoleHandler to the Logger object://: X1:MultipleHandlers.javaimport com.bruceeckel.simpletest.*;import java.util.logging.*;public class MultipleHandlers {static Test monitor = new Test();private static Logger logger =Logger.getLogger("MultipleHandlers");public static void main(String[] args)throws Exception {FileHandler logFile =new FileHandler("MultipleHandlers.xml");logger.addHandler(logFile);logger.addHandler(new ConsoleHandler());logger.warning("Output to multiple handlers");monitor.expect(new Object[] {"%%.* MultipleHandlers main","WARNING: Output to multiple handlers","%%.* MultipleHandlers main","WARNING: Output to multiple handlers"});}} ///:~When you run the program, youll notice that the console output occurs twice thats because the root loggers default behavior is still enabled.If you want to turn this off, make a call to setUseParentHandlers(false)://: X1:MultipleHandlers2.javaimport com.bruceeckel.simpletest.*;import java.util.logging.*;public class MultipleHandlers2 {static Test monitor = new Test();private static Logger logger =Logger.getLogger("MultipleHandlers2");public static void main(String[] args)throws Exception {FileHandler logFile =new FileHandler("MultipleHandlers2.xml");logger.addHandler(logFile);logger.addHandler(new ConsoleHandler());logger.setUseParentHandlers(false);logger.warning("Output to multiple handlers");monitor.expect(new Object[] {"%%.* MultipleHandlers2 main","WARNING: Output to multiple handlers"});}} ///:~Now youll see only one console message
[ Pobierz całość w formacie PDF ]