本文主要是介绍Log4j用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日志记录,具体请参考Log4j文档指南。
Log4j下载
在apache网站,可以免费下载到Log4j最新版本的软件包
Apache log4j (推荐)
Apache log4j 2
Log4j的包下载完成后,解压,将其中打包好的的log4j-1.x.x.jar导入你的工程LIB中。
Log4j配置
Log4J的配置文件(Configuration File)就是用来设置记录器的级别、存放器和布局的,它可接key=value格式的设置或xml格式的设置信息。通过配置,可以创建出Log4J的运行环境。
1. 配置文件
Log4J配置文件的基本格式如下:














其中 [level] 是日志输出级别,共有5级:






Appender 为日志输出目的地,Log4j提供的appender有以下几种:





Layout:日志输出格式,Log4j提供的layout有以下几种:




打印参数: Log4J采用类似C语言中的printf函数的打印格式格式化日志信息,如下:









2. 在代码中初始化Logger:
1)在程序中调用BasicConfigurator.configure()方法:给根记录器增加一个ConsoleAppender,输出格式通过PatternLayout设为"%-4r [%t] %-5p %c %x - %m%n",还有根记录器的默认级别是Level.DEBUG.
2)配置放在文件里,通过命令行参数传递文件名字,通过PropertyConfigurator.configure(args[x])解析并配置;
3)配置放在文件里,通过环境变量传递文件名等信息,利用log4j默认的初始化过程解析并配置;
4)配置放在文件里,通过应用服务器配置传递文件名等信息,利用一个特殊的servlet来完成配置。
Log4j示例
新建Java工程,代码
- package com.homer;
- import org.apache.log4j.BasicConfigurator;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- import org.apache.log4j.xml.DOMConfigurator;
- public class HMain {
- static Logger log4j = Logger.getLogger(HMain.class.getClass());
- public HMain(){
- System.out.println("hello, I am HMain");
- printLog();
- }
- private void printLog(){
- BasicConfigurator.configure();
- PropertyConfigurator.configure("/home/homer/workspace/myLog4j/WebContent/WEB-INF/log4j.properties");
- DOMConfigurator.configure("");
- log4j.debug("log4j debug");
- log4j.info("log4j info");
- log4j.warn("log4j warn");
- log4j.error("log4j error");
- log4j.fatal("log4j fatal");
- }
- }
Log4j 配置文件 (log4j.properties)
- ### set log levels ###
- log4j.rootLogger = debug , stdout , D , E
- ### 输出到控制台 ###
- log4j.appender.stdout = org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target = System.out
- log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
- ### 输出到日志文件 ###
- log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.D.File = logs/log.log
- log4j.appender.D.Append = true
- log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
- log4j.appender.D.layout = org.apache.log4j.PatternLayout
- log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
- ### 保存异常信息到单独文件 ###
- log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.E.File = /logs/error.log ## 异常日志文件名
- log4j.appender.E.Append = true
- log4j.appender.E.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
- log4j.appender.E.layout = org.apache.log4j.PatternLayout
- log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %l:%c:%t:%r ] - [ %p ] %m%n
运行结果(/home/homer/logs目录下):
控制台(console)
15:56:12,718 DEBUG java.lang.Class:24 - log4j debug
15:56:12,722 INFO java.lang.Class:25 - log4j info
15:56:12,723 WARN java.lang.Class:26 - log4j warn
15:56:12,723 ERROR java.lang.Class:27 - log4j error
15:56:12,723 FATAL java.lang.Class:28 - log4j fatal
error.log
2012-09-20 15:56:12 [ com.homer.HMain.printLog(HMain.java:24):java.lang.Class:http-bio-9980-exec-3:0 ] - [ DEBUG ] log4j debug
2012-09-20 15:56:12 [ com.homer.HMain.printLog(HMain.java:25):java.lang.Class:http-bio-9980-exec-3:4 ] - [ INFO ] log4j info
2012-09-20 15:56:12 [ com.homer.HMain.printLog(HMain.java:26):java.lang.Class:http-bio-9980-exec-3:5 ] - [ WARN ] log4j warn
2012-09-20 15:56:12 [ com.homer.HMain.printLog(HMain.java:27):java.lang.Class:http-bio-9980-exec-3:5 ] - [ ERROR ] log4j error
2012-09-20 15:56:12 [ com.homer.HMain.printLog(HMain.java:28):java.lang.Class:http-bio-9980-exec-3:5 ] - [ FATAL ] log4j fatal
log.log
2012-09-20 15:56:12 [ http-bio-9980-exec-3:0 ] - [ DEBUG ] log4j debug
2012-09-20 15:56:12 [ http-bio-9980-exec-3:4 ] - [ INFO ] log4j info
2012-09-20 15:56:12 [ http-bio-9980-exec-3:5 ] - [ WARN ] log4j warn
2012-09-20 15:56:12 [ http-bio-9980-exec-3:5 ] - [ ERROR ] log4j error
2012-09-20 15:56:12 [ http-bio-9980-exec-3:5 ] - [ FATAL ] log4j fatal
参考推荐:
log4j日志配置
log4j.properties配置详解
Apache logging services
这篇关于Log4j用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!