测试一下JDK提供的日志。
1 日志属性配置文件 缺省日志配置文件:
D:\Program Files (x86)\Java\jdk1.7.0_05\jre\lib\logging.properties
2 配置文件中特殊字符说明
•"/" 本地路径名分隔符
•"%t" 系统临时目录
•"%h" "user.home" 系统属性的值 (win系统会在c:/users/username目录下)
•"%g" 区分循环日志的生成号
•"%u" 解决冲突的惟一号码
•"%%" 转换为单个百分数符号"%"
3 日志配置文件在源代码中说明 java.util.logging.LogManager.java
String fname = System.getProperty("java.util.logging.config.file");
if (fname == null) {
fname = System.getProperty("java.home");
if (fname == null) {
throw new Error("Can't find java.home ??");
}
File f = new File(fname, "lib");
f = new File(f, "logging.properties");
fname = f.getCanonicalPath();
}
InputStream in = new FileInputStream(fname);
BufferedInputStream bin = new BufferedInputStream(in);
try {
readConfiguration(bin);
4 日志配置内容示例
# 输出到文件与控制台
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=
# java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.pattern = LOG.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
cuigh.log.jdk17 = SERVER
5 JAVA示例代码
package cuigh.log.jdk17;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Jdk17Log {
/**
* 功能:演示JDK提供的日志
* 说明:1 日志属性配置文件 缺省日志配置文件:D:\Program Files (x86)\Java\jdk1.7.0_05\jre\lib\logging.properties
* 2 配置文件中特殊字符说明
* •"/" 本地路径名分隔符
•"%t" 系统临时目录
•"%h" "user.home" 系统属性的值 (win系统会在c:/users/username目录下)
•"%g" 区分循环日志的生成号
•"%u" 解决冲突的惟一号码
•"%%" 转换为单个百分数符号"%"
3 日志配置文件在源代码中说明 java.util.logging.LogManager.java
String fname = System.getProperty("java.util.logging.config.file");
if (fname == null) {
fname = System.getProperty("java.home");
if (fname == null) {
throw new Error("Can't find java.home ??");
}
File f = new File(fname, "lib");
f = new File(f, "logging.properties");
fname = f.getCanonicalPath();
}
InputStream in = new FileInputStream(fname);
BufferedInputStream bin = new BufferedInputStream(in);
try {
readConfiguration(bin);
4 日志配置内容示例
*/
public static void main(String[] args) throws SecurityException, IOException {
// TODO Auto-generated method stub
Logger log = Logger.getLogger("Jdk17Log");
log.setLevel(Level.FINE);
Logger log1 = Logger.getLogger("Jdk17Log");
log.setLevel(Level.FINEST);
System.out.println(log==log1); //true
Logger log2 = Logger.getLogger("Jdk17Log.blog");
log2.setLevel(Level.WARNING);
log.info("aaa");
log1.info("bbb");
log2.info("fine");
String fname = System.getProperty("java.util.logging.config.file");
fname = System.getProperty("java.home");
File f = new File(fname, "lib");
f = new File(f, "logging.properties");
fname = f.getCanonicalPath();
System.out.println("java.util.logging.config.file = " + fname);
}
}