Transferred from: http://zochen.iteye.com/blog/616151
Simple implementation of the use of the JDK class Java.util.logging.Logger to record the log. The main reason is to use configuration files to configure the output of the log in the log4j manner. There are many articles on the web about how to use Java.util.logging.Logger, but there is no complete way to configure the configuration file to achieve control log output data. The purpose of this article is to welcome the shooting of bricks.
On the yards ...
1. First, a logmanager is encapsulated. The main function of this class is the code in the static block, which is intended to read the properties file and initialize the log attributes.
Java code
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.util.logging.Logger;
- Public class Logmanager {
- //Initialize Logmanager
- Static {
- //Read config file (file stream can also be used here)
- ClassLoader cl = Logmanager. Class.getclassloader ();
- InputStream InputStream = null;
- if (cl! = null) {
- InputStream = Cl.getresourceasstream ("log.properties");
- } Else {
- InputStream = ClassLoader
- . Getsystemresourceasstream ("log.properties");
- }
- Java.util.logging.LogManager Logmanager = Java.util.logging.LogManager
- . Getlogmanager ();
- try {
- //re-initialize the log properties and reread the log configuration.
- Logmanager.readconfiguration (InputStream);
- } catch (SecurityException e) {
- System.err.println (e);
- } catch (IOException e) {
- System.err.println (e);
- }
- }
- /**
- * Get Log objects
- * @param clazz
- * @return
- */
- public static Logger GetLogger (Class clazz) {
- Logger Logger = Logger
- . GetLogger (Clazz.getname ());
- return logger;
- }
- }
2. In the place where logging is required, the Logmanager.getlogger () method is used to get the logger object, and then the logging method is called. Here, I log the error stack information using the log (level, String msg, throwable thrown).
Post the contents of my properties profile, where the handlers attribute must not be forgotten, and I spend a lot of time looking for reasons for missing this attribute.
Java code
- #Level的五个等级SEVERE (highest value), WARNING, INFO, CONFIG, FINE, Finer, FINEST (lowest). This is different from log4j.
- #为 Handler Specifies the default level (default is Level.info).
- Java.util.logging.consolehandler.level=info
- # Specifies the name of the Formatter class to use (default is Java.util.logging.SimpleFormatter).
- Java.util.logging.consolehandler.formatter=java.util.logging.simpleformatter
- # Specifies the default level for Handler (default is Level.all).
- Java.util.logging.filehandler.level=info
- # Specifies the name of the Formatter class to use (default is Java.util.logging.XMLFormatter).
- Java.util.logging.filehandler.formatter=java.util.logging.simpleformatter
- # Specifies the approximate maximum amount, in bytes, to write to any file. If the number is 0, there is no limit (the default is unrestricted).
- java.util.logging.filehandler.limit=1024000
- # Specifies how many output files participate in the loop (default is 1).
- java.util.logging.filehandler.count=1
- # Specify a pattern for the generated output file name. For details, see the following (the default is "%h/java%u.log").
- Java.util.logging.filehandler.pattern=c:/sslog%u.log
- # Specifies whether Filehandler should be appended to any existing file (false by default ).
- Java.util.logging.filehandler.append=True
- handlers= Java.util.logging.consolehandler,java.util.logging.filehandler
This configuration also has a flaw, when the log file accumulates to the specified size, it will be recreated, that is, the previous log information will be lost, but the size of the message will cause a single file too large. The following blog post should be able to solve the problem, which has not taken time to scrutiny.
Custom file processor and log output format for java.util.logging
Java.util.logging.Logger log output by means of a pass profile