- <slf4j-api.version>1.5.11</slf4j-api.version>
- <log4j.version>1.2.16</log4j.version>
- <slf4j-log4j12.version>1.5.6</slf4j-log4j12.version>
- <!-- log4j -->
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>org.slf4j.slf4j-api</artifactId>
- <version>${slf4j-api.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>jakarta.log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>org.slf4j.slf4j-log4j12</artifactId>
- <version>${slf4j-log4j12.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>jakarta.log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
知识点梳理:
(1)让下一次增加的log内容添加到log文件最后,
而不是覆盖, <param name="Append" value="
true"/>
(2)分别控制每个部分的输出格式:分别在每种输出格式里面说明就可以了
(3)让console和file都输出:
将文件输出样式和标准输出样式都加到root标签中。
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
(4)Log4j的继承
Parent类:
log4j.xml另外一个模板
import org.apache.log4j.Logger;
public class Parent {
protected static Logger log = null; //不能为private
Parent(){
log= Logger.getLogger(getClass().getName());
}
public static void testParent(){
log.info( "--hello parent!--");
}
public static void main(String[] args) {
new Parent().testParent();
}
}
public class Parent {
protected static Logger log = null; //不能为private
Parent(){
log= Logger.getLogger(getClass().getName());
}
public static void testParent(){
log.info( "--hello parent!--");
}
public static void main(String[] args) {
new Parent().testParent();
}
}
Son类:
public
class Son
extends Parent {
public void testSon(){
log.info( "---hello son---"); //利用父类继承过来的log对象
}
public static void main(String[] args) {
new Son().testSon();
}
}
public void testSon(){
log.info( "---hello son---"); //利用父类继承过来的log对象
}
public static void main(String[] args) {
new Son().testSon();
}
}
(5)输出格式转换
转换模式(conversion pattern)为"%r [%t] %-5p %c - %m%n"的PatternLayout将生成类似于以下内容的输出:
176 [main] INFO org.foo.Bar - Located nearest gas station.
在上面的输出中:
* 第一个字段表示自程序开始到发出日志记录请求时所消耗的毫秒数
* 第二个字段表示发出日志记录请求的线程
* 第三个字段表示日志记录请求的Level
* 第四个字段表示发出日志记录请求的Logger的名称
* 第五个字段(-后的文本)表示日志记录请求的消息
176 [main] INFO org.foo.Bar - Located nearest gas station.
在上面的输出中:
* 第一个字段表示自程序开始到发出日志记录请求时所消耗的毫秒数
* 第二个字段表示发出日志记录请求的线程
* 第三个字段表示日志记录请求的Level
* 第四个字段表示发出日志记录请求的Logger的名称
* 第五个字段(-后的文本)表示日志记录请求的消息
log4j的配置文件如下:
<?
xml
version
="1.0"
encoding
="GB2312"
?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- #log4j中有5级logger ,#FATAL 0 ,#ERROR 3 ,#WARN 4 ,#INFO 6 ,#DEBUG 7 -->
< log4j:configuration xmlns:log4j ='http://jakarta.apache.org/log4j/' >
<!-- 输出到控制台-->
<!--
<appender name="LOG.Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
</appender>
-->
<!-- 将级别为DEBUG的信息输出到控制台-->
< appender name ="LOG.DEBUG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/debug.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="DEBUG" />
</ filter >
</ appender >
<!-- 将级别为INFO的信息输出到控制台-->
< appender name ="LOG.INFO" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/info.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="INFO" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
<!-- 将级别为WARN的信息输出到控制台-->
< appender name ="LOG.WARN" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/warn.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="WARN" />
< param name ="LevelMax" value ="WARN" />
</ filter >
</ appender >
<!-- 将级别为ERROR的信息输出到控制台-->
< appender name ="LOG.ERROR" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/error.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="ERROR" />
</ filter >
</ appender >
<!-- 将级别为FATAL的信息输出到控制台-->
< appender name ="LOG.FATAL" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/fatal.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="FATAL" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!--
<appender name="InitAction" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/dbcon.log"/>
<param name="MaxFileSize" value="5120KB"/>
<param name="MaxFileSize" value="10" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.'log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} - %m%n"/>
<param name="ConversionPattern" value="%d{DATE} [%-5p] %c {%F:%L} - %m%n"/>
</layout>
</appender>
-->
<!-- 设置org.logicalcobwebs包的输出级别为INFO-->
<!--
<category name="org.logicalcobwebs">
<priority value="INFO" />
<appender-ref ref="LOG.Console" />
</category>
-->
< root >
< priority value ="DEBUG" />
<!--
<appender-ref ref="LOG.Console" />
-->
< appender-ref ref ="LOG.DEBUG" />
< appender-ref ref ="LOG.INFO" />
< appender-ref ref ="LOG.WARN" />
< appender-ref ref ="LOG.ERROR" />
< appender-ref ref ="LOG.FATAL" />
</ root >
</ log4j:configuration >
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- #log4j中有5级logger ,#FATAL 0 ,#ERROR 3 ,#WARN 4 ,#INFO 6 ,#DEBUG 7 -->
< log4j:configuration xmlns:log4j ='http://jakarta.apache.org/log4j/' >
<!-- 输出到控制台-->
<!--
<appender name="LOG.Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
</appender>
-->
<!-- 将级别为DEBUG的信息输出到控制台-->
< appender name ="LOG.DEBUG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/debug.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="DEBUG" />
</ filter >
</ appender >
<!-- 将级别为INFO的信息输出到控制台-->
< appender name ="LOG.INFO" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/info.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="INFO" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
<!-- 将级别为WARN的信息输出到控制台-->
< appender name ="LOG.WARN" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/warn.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="WARN" />
< param name ="LevelMax" value ="WARN" />
</ filter >
</ appender >
<!-- 将级别为ERROR的信息输出到控制台-->
< appender name ="LOG.ERROR" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/error.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="ERROR" />
</ filter >
</ appender >
<!-- 将级别为FATAL的信息输出到控制台-->
< appender name ="LOG.FATAL" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="d:/log/fatal.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="FATAL" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!--
<appender name="InitAction" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/dbcon.log"/>
<param name="MaxFileSize" value="5120KB"/>
<param name="MaxFileSize" value="10" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.'log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} - %m%n"/>
<param name="ConversionPattern" value="%d{DATE} [%-5p] %c {%F:%L} - %m%n"/>
</layout>
</appender>
-->
<!-- 设置org.logicalcobwebs包的输出级别为INFO-->
<!--
<category name="org.logicalcobwebs">
<priority value="INFO" />
<appender-ref ref="LOG.Console" />
</category>
-->
< root >
< priority value ="DEBUG" />
<!--
<appender-ref ref="LOG.Console" />
-->
< appender-ref ref ="LOG.DEBUG" />
< appender-ref ref ="LOG.INFO" />
< appender-ref ref ="LOG.WARN" />
< appender-ref ref ="LOG.ERROR" />
< appender-ref ref ="LOG.FATAL" />
</ root >
</ log4j:configuration >
<?
xml
version
="1.0"
encoding
="GB2312"
?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
<!-- 设置监视器输出方式 -->
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern"
value ="%-4r [%t] %-5p %c %x - %m%n" />
</ layout >
<!-- 滤镜设置输出的级别-->
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="levelMin" value ="INFO" />
< param name ="levelMax" value ="ERROR" />
< param name ="AcceptOnMatch" value ="true" />
</ filter >
</ appender >
< appender name ="FILE"
class ="org.apache.log4j.RollingFileAppender" >
<!-- 设置通道file和输出方式:org.apache.log4j.RollingFileAppender -->
< param name ="File" value ="all.output.log" /><!-- 设置File参数:日志输出文件名 -->
< param name ="Append" value ="true" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%p (%c:%L)- %m%n" /><!-- 设置输出文件项目和格式 -->
</ layout >
</ appender >
< root ><!-- 设置接收所有输出的通道 -->
< priority value ="INFO" />
< appender-ref ref ="FILE" /><!-- 与前面的通道id相对应 -->
< appender-ref ref ="STDOUT" />
</ root >
</ log4j:configuration >
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
<!-- 设置监视器输出方式 -->
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern"
value ="%-4r [%t] %-5p %c %x - %m%n" />
</ layout >
<!-- 滤镜设置输出的级别-->
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="levelMin" value ="INFO" />
< param name ="levelMax" value ="ERROR" />
< param name ="AcceptOnMatch" value ="true" />
</ filter >
</ appender >
< appender name ="FILE"
class ="org.apache.log4j.RollingFileAppender" >
<!-- 设置通道file和输出方式:org.apache.log4j.RollingFileAppender -->
< param name ="File" value ="all.output.log" /><!-- 设置File参数:日志输出文件名 -->
< param name ="Append" value ="true" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%p (%c:%L)- %m%n" /><!-- 设置输出文件项目和格式 -->
</ layout >
</ appender >
< root ><!-- 设置接收所有输出的通道 -->
< priority value ="INFO" />
< appender-ref ref ="FILE" /><!-- 与前面的通道id相对应 -->
< appender-ref ref ="STDOUT" />
</ root >
</ log4j:configuration >
【注意】
1、Log4j设置循环记录日志文件
- log4j.appender.fileout=org.apache.log4j.RollingFileAppender
- log4j.appender.fileout.File=#### 为文件名,这个存放的位置为project工程的根目录
- log4j.appender.fileout.MaxFileSize=5MB
- log4j.appender.fileout.MaxBackupIndex=50
- log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
- log4j.appender.fileout.layout.ConversionPattern=%d{ABSOLUTE} %-5p %t %c{5} - %m%n
某个类输出日志到某个log文件配置
log4j.rootLogger=info, stdout,console
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.File=../log/updater.log
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
log4j.appender.stdout.Append=true
log4j.appender.stdout.File=../log/updater.log
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
log4j.appender.stdout.Append=true
#console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
#log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
#log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
#app log
#com.wonibo.projectx.service.index.ReadTxtDaemon类使用warn级别且使用
txtfailure类型的appender
log4j.logger.com.wonibo.projectx.service.index.ReadTxtDaemon=warn,
txtfailure
#未指定appender的则从root继承
log4j.logger.org.springframework=warn
log4j.logger.org.springframework=warn
log4j.logger.org.hibernate.ps.PreparedStatementCache=warn
log4j.logger.org.hibernate=warn
log4j.logger.org.hibernate.SQL=warn
log4j.logger.org.hibernate.type=warn
log4j.logger.org.hibernate=warn
log4j.logger.org.hibernate.SQL=warn
log4j.logger.org.hibernate.type=warn
log4j.logger.com.danga.MemCached=warn
log4j.logger.org.apache.velocity=warn
log4j.logger.org.apache.velocity=warn
log4j.appender.
txtfailure=org.apache.log4j.FileAppender
log4j.appender. txtfailure.File=../log/txtfailure.log
log4j.appender. txtfailure.layout=org.apache.log4j.PatternLayout
log4j.appender. txtfailure.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
log4j.appender. txtfailure.Append=true
log4j.appender. txtfailure.File=../log/txtfailure.log
log4j.appender. txtfailure.layout=org.apache.log4j.PatternLayout
log4j.appender. txtfailure.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %-17c{2} - %m%n
log4j.appender. txtfailure.Append=true
log4j.properties的放置位置
- public Logger m_log = null;
- public ETrafficPlanService(){
- PropertyConfigurator.configure("data//config//log4j//log4j_etraffic.properties");
- m_log = Logger.getLogger(ETrafficPlanService.class);
- m_log.debug("debug test...........");
- }
问题总结:
1、
PropertyConfigurator.configure("src\\test\\java\\log4j.properties");//位置相对于工程根目录的位置为起点
【注意】只针对Properties文件有效,xml配置文件无效
读取XML配置文件:
DOMConfigurator.configure ( String filename ) :读取XML形式的配置文件
- private Logger logger;
- public MyThread() {
- super();
- //PropertyConfigurator.configure(System.getProperty("user.dir") + "/src/main/java/com/alibaba/thread/log.properties");
- DOMConfigurator.configure(System.getProperty("user.dir") + "/src/main/java/com/alibaba/thread/log4j.xml");
- logger = LoggerFactory.getLogger(this.getClass().getName());
- }
【注意】
相对路径: DOMConfigurator.configure(this.getClass().getClassLoader().getResource("com/alibaba/thread/log4j.xml"));
绝对路径:
DOMConfigurator.configure(System.getProperty("user.dir") + "/src/main/java/com/alibaba/thread/log4j.xml");
2、log4j.appender.stdout.File=../log/updater.log //位置的起点为工程的根目录为起点
例如../log/updater.log:表示工程根目录的上级目录下的log,再生成updater.log文件
3、Logger
默认使用log4j.rootLogger, 也可以自定义某些包或类的logger:log4j.logger.com.alibaba.maven.hello.Simple
log的日志级别:ALL< TRACE <
DEBUG < INFO < WARN < ERROR < FATAL < OFF
常用为中间4种
4、Appender
AppenderSkeleton,
AsyncAppender,
ConsoleAppender,
DailyRollingFileAppender,
ExternallyRolledFileAppender,
FileAppender,
JDBCAppender,
JMSAppender,
LF5Appender,
NTEventLogAppender,
NullAppender,
RollingFileAppender,
SMTPAppender,
SocketAppender,
SocketHubAppender,
SyslogAppender,
TelnetAppender,
WriterAppender
设置DailyRollingFileAppender时,可以设置之前的log修改成什么样的格式
log4j.appender.simple.DatePattern='_'yyyy-MM-dd
5、输出类型:
layout
Simple layout-- org.apache.log4j.SimpleLayout
SimpleLayout formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message.
SimpleLayout formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message.
PatternLayout --org.apache.log4j.PatternLayout
PatternLayout formats the output based on a conversion pattern specified, or if none is specified, the default conversion pattern.
PatternLayout formats the output based on a conversion pattern specified, or if none is specified, the default conversion pattern.
HTML layout-- org.apache.log4j.HTMLLayout
HTMLLayout formats the output as a HTML table.
HTMLLayout formats the output as a HTML table.
XMLLayout-- org.apache.log4j.xml.XMLLayout
XMLLayout formats the output as a XML.
XMLLayout formats the output as a XML.
6、什么情况可以使用SLF4j
如果你开发的是类库或者嵌入式组件,那么就应该考虑采用SLF4J,因为不可能影响最终用户选择哪种日志系统。在另一方面,如果是一个简单或者独立的应用,确定只有一种日志系统,那么就没有使用SLF4J的必要。假设你打算将你使用log4j的产品卖给要求使用JDK 1.4 Logging的用户时,面对成千上万的log4j调用的修改,相信这绝对不是一件轻松的事情。但是如果开始便使用SLF4J,那么这种转换将是非常轻松的事情。
<!--
slf4j & log4j -->
< dependency >
< groupId >com.alibaba.external </ groupId >
< artifactId >org.slf4j.slf4j-api </ artifactId >
< version >1.5.11 </ version >
</ dependency >
< dependency >
< groupId >com.alibaba.external </ groupId >
< artifactId >jakarta.log4j </ artifactId >
< version >1.2.16 </ version >
</ dependency >
< dependency >
< groupId >com.alibaba.external </ groupId >
< artifactId >org.slf4j.slf4j-api </ artifactId >
< version >1.5.11 </ version >
</ dependency >
< dependency >
< groupId >com.alibaba.external </ groupId >
< artifactId >jakarta.log4j </ artifactId >
< version >1.2.16 </ version >
</ dependency >
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/244266,如需转载请自行联系原作者