자바로 로그에 대한 좋은 설명과 예제가 있었음
http://www.javapractices.com/Topic143.cjp
| Logging messages |
| Topic : |
Java's logging facility (see Sun's overview and API ) has two parts : a configuration file, and an API for using logging services. It has impressive flexibility. Log entries can be sent to these destinations, as either simple text or as XML :
logging request of some level is made to logger attached to current package Here is an example of a logging configuration file : # Properties file which configures the operation of the JDK # The system will look for this config file, first using # Global logging properties. # Default global logging level. # Loggers # Handlers # --- ConsoleHandler --- # --- FileHandler --- # Naming style for the output file: # Limiting size of output file in bytes: # Number of output files to cycle through, by appending an # Style of output (Simple or XML): An application should likely centralize the naming policy for Loggers, 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 :
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 argv[]) {
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.
fLogger.finest("this is finest");
fLogger.finer("this is finer");
fLogger.fine("this is fine");
fLogger.config("this is config");
fLogger.info("this is info");
fLogger.warning("this is a warning");
fLogger.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 :
fLogger.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");
fLogger.log(Level.SEVERE, "Some message", ex);
//There are convenience methods for exiting and
//entering a method, which are at Level.FINER :
fLogger.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 logger will inherit the config of its parent, and add
//any further config as an override. A simple style is to use
//all config of the parent except, perhaps, for logging level.
//This style uses a hard-coded literal and should likely be avoided:
//private static final Logger fLogger = Logger.getLogger("myapp.business");
//This style has no hard-coded literals, but forces the logger
//to be non-static.
//private final Logger fLogger=Logger.getLogger(this.getClass().getPackage().getName());
//This style uses a static field, but hard-codes a class literal.
//This is probably acceptable.
private static final Logger fLogger =
Logger.getLogger(SimpleLogger.class.getPackage().getName());
}
Example Run 1 , using the config file defined above : >java -Djava.util.logging.config.file=C:\Temp\logging.properties ConsoleHandler is configured to show only SEVERE messages : Jan 8, 2003 10:43:47 AM myapp.business.SimpleLogger doSomething While the FileHandler shows ALL that is sent to it : Jan 8, 2003 10:43:46 AM myapp.business.SimpleLogger doSomething 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 |
| Would you use this technique? |
| Add your comment to this Topic : |
| Comment : |
© 2006 John O'Hanley (Canada) | Source Code | Contact | Sponsor This Site | License | RSS |
| Over 53,000 unique IPs (and 106,000 sessions) last month. |
| - In Memoriam : Bill Dirani - |

| 
