log4j 1.3的配置和主要应用

一.Log4j的基本概念
1.Logger: 日志记录器.
日志的级别 级别越低 日志记录将更详细
级别有(由高到底)off,fatal,error,warn,info,debug,all 级别高的可覆盖级别低的日志输出
2.Appender: 输出端 指定日志输出到那个地方.一个Logger可以有多个Appender输出端,每个Appender输出端也可以被多个Logger使用.
常用的输出端有:
ConsoleAppender 将日志输出到控制台
org.apache.log4j.rolling.RollingFileAppender 可指定文件和日期的输出端
WriterAppender 将日志以流格式发送到任何指定的地方
JDBCAppender 通过JDBC把日志输出到数据库中
3.Layout: 日志格式化
log4j有三种Layout:
HTMLLayout 格式化日志输出为HTML表格;
PatternLayout 根据指定的格式(ConversionPattern)格式化日志输出(常用)
SimpleLayout 以非常简单的方式输出

二.配置
1. log4j的配置文件 log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration >
<configuration xmlns='http://logging.apache.org/' debug="true">

<!-- 把日志输出到控制台中 -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) %m%n" />
</layout>
</appender>

<!-- 把日志按日期输出到日期日志文件中 -->
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="${catalina.home}/log4jlogs/mywebsite.%d{yyyy-MM-dd}.log.zip" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd HH:mm:ss} %p [%t] %C{2} (%F:%L) - %m%n" />
</layout>
</appender>


<!-- 终端日志文件 -->
<appender name="terminal-logger" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.home}/logs/kiosks/TerminalService.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd HH:mm:ss} [%p] [%t] (%F:%L): %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="com.macaufly.kiosks">
<level value="INFO" />
<appender-ref ref="terminal-logger" />
</logger>


<!-- 设置接收所有输出的通道(但在logger中定义的级别在这里不起作用) -->
<root>
<level value="debug" /><!-- 设置级别 -->
<appender-ref ref="console" />
</root>
<!--
设置域名限制,即 com.dchaoxiong.studentmanage.servlets
包及以下的日志均输出到下面指定的通道(appender-ref)中
-->
<logger name="com.dchaoxiong.studentmanage.servlets">
<level value="debug" /> <!-- 设置级别 -->
<appender-ref ref="ROLL" /><!-- 与前面的通道id相对应 -->
</logger>
</configuration>

2. 把该配置文件放在工程的类路径下
3.private Logger logger = Logger.getLogger(ClassName.class);取得loger类
apache-log4j-1.2.15.jar, apache-log4j-extras-1.0.jar, apache-log4j-extras-1.1.jar, apache-log4j.jar, log4j-1.2-api-2.0.2-javadoc.jar, log4j-1.2-api-2.0.2-sources.jar, log4j-1.2-api-2.0.2.jar, log4j-1.2.11.jar, log4j-1.2.12.jar, log4j-1.2.13-sources.jar, log4j-1.2.13.jar, log4j-1.2.13.src.jar, log4j-1.2.14-sources.jar, log4j-1.2.14.jar, log4j-1.2.15-sources.jar, log4j-1.2.15.jar, log4j-1.2.16-sources.jar, log4j-1.2.16.jar, log4j-1.2.5.jar, log4j-1.2.6.jar, log4j-1.2.7.jar, log4j-1.2.8.jar, log4j-1.2.9.jar, log4j-1.2.91.jar, log4j-1.2.jar, log4j-1.2rc1.jar, log4j-1.3alpha-7.jar, log4j-api-2.0.2-javadoc.jar, log4j-api-2.0.2-sources.jar, log4j-api-2.0.2.jar, log4j-boot.jar, log4j-bridge-0.9-sources.jar, log4j-bridge-0.9-tests.jar, log4j-bridge-0.9.4-sources.jar, log4j-bridge-0.9.4-tests.jar, log4j-bridge-0.9.4.jar, log4j-bridge-0.9.6-sources.jar, log4j-bridge-0.9.6-tests.jar, log4j-bridge-0.9.6.jar, log4j-bridge-0.9.jar, log4j-config.jar, log4j-core-2.0.2-javadoc.jar, log4j-core-2.0.2-sources.jar, log4j-core-2.0.2-tests.jar, log4j-core-2.0.2.jar, log4j-core.jar, log4j-ext.jar, log4j-flume-ng-2.0.2-javadoc.jar, log4j-flume-ng-2.0.2-sources.jar, log4j-flume-ng-2.0.2.jar, log4j-java1.1.jar, log4j-jcl-2.0.2-javadoc.jar, log4j-jcl-2.0.2-sources.jar, log4j-jcl-2.0.2.jar, log4j-jmx-gui-2.0.2-javadoc.jar, log4j-jmx-gui-2.0.2-sources.jar, log4j-jmx-gui-2.0.2.jar, log4j-nosql-2.0.2-javadoc.jar, log4j-nosql-2.0.2-sources.jar, log4j-nosql-2.0.2.jar, log4j-over-slf4j-1.5.0.jar, log4j-over-slf4j-1.5.10.jar, log4j-over-slf4j-1.5.3.jar, log4j-over-slf4j-1.5.5.jar, log4j-over-slf4j-1.5.6-sources.jar, log4j-over-slf4j-1.5.6.jar, log4j-over-slf4j-1.5.8-sources.jar, log4j-over-slf4j-1.5.8.jar, log4j-over-slf4j-1.6.1.jar, log4j-slf4j-impl-2.0.2-javadoc.jar, log4j-slf4j-impl-2.0.2-sources.jar, log4j-slf4j-impl-2.0.2.jar, log4j-snmp-appender.jar, log4j-taglib-2.0.2-javadoc.jar, log4j-taglib-2.0.2-sources.jar, log4j-taglib-2.0.2.jar, log4j-to-slf4j-2.0.2-javadoc.jar, log4j-to-slf4j-2.0.2-sources.jar, log4j-to-slf4j-2.0.2.jar, log4j-web-2.0.2-javadoc.jar, log4j-web-2.0.2-sources.jar, log4j-web-2.0.2.jar, log4j-wjw.jar, log4j-xml-1.3alpha-7.jar, log4j.jar, org.apache.log4j_1.2.15.v201005080500.jar, org.apache.log4j_1.2.15.v201012070815.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值