log4j使用配置方法及项目中的应用
时间:2026-01-23
时间:2026-01-23
log4j使用配置方法及项目中的应用
1. 概述
1.1. 背景
在应用程序中添加日志记录总的来说基于三个目的:监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作;跟踪代码运行时轨迹,作为日后审计的依据;担当集成开发环境中的调试器的作用,向文件或控制台打印代码的调试信息。
最普通的做法就是在代码中嵌入许多的打印语句,这些打印语句可以输出到控制台或文件中,比较好的做法就是构造一个日志操作类来封装此类操作,而不是让一系列的打印语句充斥了代码的主体。
1.2. Log4j简介
在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用的日志操作类外,Apache为我们提供了一个强有力的日志操作包-Log4j。
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
此 外,通过Log4j其他语言接口,您可以在C、C++、.Net、PL/SQL程序中使用Log4j,其语法和用法与在Java程序中一样,使得多语言分 布式系统得到一个统一一致的日志组件模块。而且,通过使用各种第三方扩展,您可以很方便地将Log4j集成到J2EE、JINI甚至是SNMP应用中。
2. 范例
参见简单例子
3. 配置过程
3.1. 创建属性文件(XXX.properties)
大家可以使用任何编辑工具,开发工具(IDE)自身提供的,还有像UtralEdit, notepad都可以。文件的名称任意,但是为了方便起见,通常都命名为log4j.properties。最终存放位置通常都放在了classes 文件中
log4j使用配置方法及项目中的应用
3.2. 属性文件(XXX.properties)的具体内容
Log的输出级别。Log4J中的一个核心概念是日志级别是有序的。Log4J内置了5种日志级别为:
Log 的根及子孙类输出级别的设定
log4j.logger.XXX.XXX.XXX=WARN 它的意思是在XXX.XXX.XXX 类中只输出警告的日志,DEBUG,INFO日志器忽略
一个Appender代表log信息要写向的一个地方。log4j可使用的Appender有很多类型,这里只讲解4种:ConsoleAppender,FileAppender,DailyRollFileAppender
3.2.1 ConsoleAppender
如果使用ConsoleAppender,那么log信息将写到Console。就是直接把信息打印到System.out上了。
3.2.2 FileAppender
使用FileAppender,那么log信息将写到指定的文件中。这应该是比较经常使用到的情况。相应地,在配置文件中应该指定log输出的文件名。如下配置指定了log文件名为log4j.log。
3.2.3 RollingFileAppender
使用FileAppender可以将log信息输出到文件中,但是如果文件太大了读起来就不方便了。这时就可以使用它,同过它可以通过设定文件的大小,自动产生多个日志文件。
log4j使用配置方法及项目中的应用
3.2.4 DailyRollingAppender
使用FileAppender可以将log信息输出到文件中,但是如果文件太大了读起来就不方便了。这时就可以使用 DailyRollingAppender。DailyRollingAppender可以把Log信息输出到按照日期来区分的文件中。如下配置文件就会 每天产生一个log文件,每个log文件只记录当天的log信息。
Layout指定了log信息输出的样式
log4j使用配置方法及项目中的应用
Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precisi
on specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". WARNING Generating the caller class information is slow. Thus, it's use should be avoided unless execution speed is not an issue. Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS} If no date format specifier is given HH:mm:ss,SSS}. then ISO8601 format is assumed. The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. Although part of the standard JDK, the performance of SimpleDateFormat is quite poor. For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE} %d{ABSOLUTE}. These dedicated date formatters perform significantly better than SimpleDateFormat. Used to output the file name where the logging request was issued. F WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue. Used to output location information of the caller which generated the logging event. l The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method
C