By default, the JDK's logmanager will read the configuration in the "lib/logging.properties" file in the JRE directory.
Custom configurations are often required in development, so you can modify the default configuration in two ways:
1, through the Java command line to modify the way
In addition, Logmanager can also allow users to control the configuration of the log based on two system properties: "Java.util.logging.config.class" "Java.util.logging.config.file"
In which, class this property takes precedence, and if set, the file attribute is ignored.
There are two ways to set properties in Java: When the Preferences API starts, the command-line arguments
Refer to the JDK documentation for the preferences API.
Command-line arguments refer to the-d<name>=<value> method of specifying attributes at startup, where we can use the
Java-djava.util.logging.config.file= "Abc.properties"
Specifies that the "abc.properties" file be used as a configuration file
2, custom subclass log class, in the code re-read configuration information
Package com.proj.log;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.logging.LogManager;
Import Java.util.logging.Logger;
/** * Log {@link Java.util.logging.Logger} get/public class logging {private static Logger Logger = null; Private Logging () {} public static Logger GetLogger () {if (null = = Logger) {InputStream is
= Logging.class.getClass (). getResourceAsStream ("/logger.properties");
try {Logmanager.getlogmanager (). Readconfiguration (IS);
catch (Exception e) {logging.warning ("Input properties file is error.\n" + e.tostring ());
}finally{try {is.close ();
catch (IOException e) {logging.warning ("Close FileInputStream a case.\n" + e.tostring ());
} logger = Logger.getlogger ("logger");
return logger; private static Logger logging = Logger.getlogger (Logging.class.getName ()); }The default is to add files to the SRC directory logger.properties:
handlers = Java.util.logging.consolehandler,java.util.logging.filehandler
Java.util.logging.ConsoleHandler.formatter = Java.util.logging.SimpleFormatter
Java.util.logging.ConsoleHandler.level = INFO
Java.util.logging.FileHandler.pattern = C:/my.log%g.log
Java.util.logging.FileHandler.formatter = Java.util.logging.SimpleFormatter
Java.util.logging.FileHandler.limit = 104857600
java.util.logging.FileHandler.count = 3
Java.util.logging.FileHandler.append = True
java.util.logging.FileHandler.level = INFO
logger.level = FINEST
Test Cases:
public class Loggertest extends testcase{
@Test public
void Testlogger () throws Exception {Logger
Logger = Lo Gging.getlogger ();
Logger.finest ("finest");
Logger.finer ("finer");
Logger.fine ("fine");
Logger.info ("info");
Logger.config ("config");
Logger.warning ("warning");
Logger.severe ("severe");
}
Modify Date format:
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.io.StringWriter;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.logging.FileHandler;
Import Java.util.logging.Formatter;
Import Java.util.logging.Level;
Import Java.util.logging.LogRecord;
Import Java.util.logging.Logger; public class Logtest {public static void main (string[] args) throws IOException {Logger log = Logger.getlogger (logte
St.class.getName ());
Log.setlevel (Level.info);
Filehandler Filehandler = new Filehandler ("E:/testlog%g.log", true);
Filehandler.setlevel (Level.severe);
Filehandler.setformatter (New Formatter () {public String format (LogRecord record) {
SimpleDateFormat sd = new SimpleDateFormat ("[Yyyy-mm-dd HH:mm:ss]");
String d = Sd.format (new Date ());
Return D + record.getlevel () + ":" + record.getmessage () + "n";
}
});
Log.addhandler (Filehandler); try {THRow new Exception ("ldddddddd");
catch (Exception e) {stringwriter SW = new StringWriter ();
E.printstacktrace (new PrintWriter (SW));
Log.severe (Sw.tostring ());
} log.info ("AAA");
}
}Format: 2014-10-29 17:39:11