最近做了一个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="${helloHome}/logs/debug.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="debug" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="info-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${helloHome}/info.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="info" />
<param name="LevelMin" value="info" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="warn-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${helloHome}/warn.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="warn" />
<param name="LevelMin" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="error-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${helloHome}/logs/error.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss} %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="error" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- OF-506: Jetty INFO messages are generally not useful. Ignore them by default. -->
<logger name="org.eclipse.jetty">
<level value="warn" />
</logger>
<root>
<level value="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"路径下了