的级别。
2.修改logging.properties
默认的外部配置文件 是JRE中lib/logging.properties文件。你可以打开这个文件,修改以下两行为:
.level=ALL //... java.util.logging.ConsoleHandler.level = ALL |
这种方式会影响jre下所有用户。
为了不影响到所有的用户,我们还可以通过LogManager的readConfiguration(InputStream ins)读取指定的配制文件。
【例1.3】:LogManager管理日志
package lwf.log.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class LogTest {
static String strClassName = LogTest.class.getName();
static Logger logger = Logger.getLogger(strClassName);
static LogManager logManager = LogManager.getLogManager();
static {
InputStream inputStream = null;
try {
//读取配制文件
inputStream = LogTest.class.getClassLoader().getResourceAsStream("log.properties");
logManager.readConfiguration(inputStream);
//添加Logger
logManager.addLogger(logger);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static double division(int value1, int value2) {
double result = 0;
try {
result = value1 / value2;
} catch(ArithmeticException e) {
logger.severe("[severe]除数不能为0.");
logger.warning("[warning]除数不能为0.");
logger.info("[info]除数不能为0.");
logger.config("[config]除数不能为0.");
logger.fine("[fine]除数不能为0.");
logger.finer("[finer]除数不能为0.");
logger.finest("[finest]除数不能为0.");
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println(division(5, 0));
}
}
log.properties:
# "handlers" specifies a comma separated list of log Handler #handlers= java.util.logging.ConsoleHandler handlers= java.util.logging.FileHandler # Default logging level. .level= CONFIG # default file output is in "E:\Test" directory. java.util.logging.FileHandler.pattern = E:/Test/Log%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter # Limit the message that are printed on the console to CONFIG and above. java.util.logging.ConsoleHandler.level = CONFIG java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter # Facility specific properties.Provides extra control for each logger. # For example, set the com.xyz.foo logger to only log SEVERE messages: com.xyz.foo.level = SEVERE |
这样,用户就可以自己定义配制文件了。在E:\Test下可以看到输出的日志文件Log0.log
java.util.logging包中类的关系图如下:

参考文章:
http://blog.csdn.net/dl88250/article/details/1843813
2.log4j
1.项目串导入log4j的jar包
如Eclipse下项目名右键,Build Path\Add Libraries,添加一组用户自己的jar包。项目结构如下:

2.修改log4j的配制文件,设置日志输出的级别、格式等
log4j的log有5个级别:FATAL(严重的 )、ERROR(错误 )、WARN(警告)、INFO(信息)、DEBUG(调试 )。
3.在项目代码中适当添加日志。
【例2.1】
log4j.properties:
#set log level: show debug, info, error log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender which outputs to System.out. #log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1=org.apache.log4j.FileAppender # A1 uses PatternLayout. log4j.appender.A1.l |