首页 > 代码库 > JAVA使用log4j

JAVA使用log4j

1.引入jar包

  log4j-1.2.13.jar

2.src下建立配置文件:log4j.properties

技术分享

#不+All,只写后一种LOGlog4j.rootLogger =ALL,systemOut,logRollingFile #输出到控制台 log4j.appender.systemOut = org.apache.log4j.ConsoleAppender log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout #"%-5p":消息类型(DEBUG?INFO?),左对其,5字符#"%l":那个文件那个方法哪一行输出log#"%n":换行#"%m":log消息log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ss.SSS}][%l]%n%m%n#阈值(log级别:DEBUG,INFO,ERROR,WARN)log4j.appender.systemOut.Threshold = DEBUG#设定文件大小输出到文件 log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout log4j.appender.logRollingFile.layout.ConversionPattern = %m%nlog4j.appender.logRollingFile.Threshold = DEBUG log4j.appender.logRollingFile.File = C:/log/AhLog.txt log4j.appender.logRollingFile.MaxFileSize = 1MB log4j.appender.logRollingFile.MaxBackupIndex = 10
参考:
  http://blog.csdn.net/dr_guo/article/details/50718063  《log4j.properties配置详解与实例

3.编写Java测试文件:

import org.apache.log4j.Logger;public class TestLog4j {    static Logger log = Logger.getLogger(TestLog4j.class.getName());    public static void main(String[] args) {        log.debug("AH DEBUG");        log.info("AH INFO");        log.error("AH ERROR");        log.warn("AH WARN");    }}

 

JAVA使用log4j