首页 > 代码库 > JAVA项目如何使用日志文件
JAVA项目如何使用日志文件
最近做了一个java桌面程序,界面使用SWT/jface画的,在做的过程中,我想使用log4j记录程序运行的日志信息,当然也包括错误日志。
下面我把使用log4j的使用方法记录在下面,也包括配置方法,供以后参考
1.配置方法:
导入jar包,slf4j-log-4j12.jar,slf4j-api-1.7.2.jar,log4j-1.2.17.jar
把log4j的配置文件log4j.xml拷贝到项目的src目录下
log4j.xml内容如下:${helloHome}是为虚拟机配置的本项目的路径
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="debug-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value=http://www.mamicode.com/"${helloHome}/logs/debug.log" />
<param name="MaxFileSize" value=http://www.mamicode.com/"1024KB"/>
<param name="MaxBackupIndex" value=http://www.mamicode.com/"5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value=http://www.mamicode.com/"%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value=http://www.mamicode.com/"debug" />
<param name="AcceptOnMatch" value=http://www.mamicode.com/"true" />
</filter>
</appender>
<appender name="info-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value=http://www.mamicode.com/"${helloHome}/info.log" />
<param name="MaxFileSize" value=http://www.mamicode.com/"1024KB"/>
<param name="MaxBackupIndex" value=http://www.mamicode.com/"5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value=http://www.mamicode.com/"%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value=http://www.mamicode.com/"info" />
<param name="LevelMin" value=http://www.mamicode.com/"info" />
<param name="AcceptOnMatch" value=http://www.mamicode.com/"true" />
</filter>
</appender>
<appender name="warn-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value=http://www.mamicode.com/"${helloHome}/warn.log" />
<param name="MaxFileSize" value=http://www.mamicode.com/"1024KB"/>
<param name="MaxBackupIndex" value=http://www.mamicode.com/"5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value=http://www.mamicode.com/"%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value=http://www.mamicode.com/"warn" />
<param name="LevelMin" value=http://www.mamicode.com/"warn" />
<param name="AcceptOnMatch" value=http://www.mamicode.com/"true" />
</filter>
</appender>
<appender name="error-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value=http://www.mamicode.com/"${helloHome}/logs/error.log" />
<param name="MaxFileSize" value=http://www.mamicode.com/"1024KB"/>
<param name="MaxBackupIndex" value=http://www.mamicode.com/"5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value=http://www.mamicode.com/"%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value=http://www.mamicode.com/"error" />
<param name="AcceptOnMatch" value=http://www.mamicode.com/"true" />
</filter>
</appender>
<!-- OF-506: Jetty INFO messages are generally not useful. Ignore them by default. -->
<logger name="org.eclipse.jetty">
<level value=http://www.mamicode.com/"warn" />
</logger>
<root>
<level value=http://www.mamicode.com/"info" />
<appender-ref ref="debug-out" />
<appender-ref ref="info-out" />
<appender-ref ref="warn-out" />
<appender-ref ref="error-out" />
</root>
</log4j:configuration>
到此配置完毕。
2.使用方法
public class HelloiServer {
private static final Logger Log = LoggerFactory.getLogger(HelloiServer.class);
public void aa() {
Log.error("erroreeeeeeeeeeeeeeee");
}
}
这样日志就会记录到上面log4j.xml文件所配置的"${helloHome}/logs/error.log"路径下了
JAVA项目如何使用日志文件