log4J配置及在web.xml文件中的配置

web.xml中的相关配置
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      <!-- webAppRootKey:值缺省为webapp.root,当tomcat下部署多个应用时(每个都用到了log4j),
        每个应用的web.xml中都要配置该参数,该参数与Log4j.xml文件中的${webapp.root}
        否则每个应用的webAppRootKey值都相同,就会引起冲突
     -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>webapp.root</param-value>
    </context-param>
     
    <!-- log4jConfigLocation:log4j配置文件存放路径 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/conf/log4j.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

log4j.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPElog4j:configuration SYSTEM "log4j.dtd">
 
<log4j:configurationxmlns:log4j="http://jakarta.apache.org/log4j/">
 
    <!-- 控制台的日志记录 -->
    <appendername="console"class="org.apache.log4j.ConsoleAppender">
        <paramname="Target"value="System.out"/>
        <layoutclass="org.apache.log4j.PatternLayout">
            <paramname="ConversionPattern"value="%-5p: %c - %m%n"/>
        </layout>
    </appender>
        <!-- 文件的日志记录 -->
    <appendername="file"class="org.apache.log4j.DailyRollingFileAppender">
        <paramname="File"value="${webapp.root}/logs/testdata.log"/>
        <paramname="Append"value="true"/>
        <paramname="DatePattern"value="'.'yyyy-MM-dd"/>
        <layoutclass="org.apache.log4j.PatternLayout">
            <paramname="ConversionPattern"value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p]: %c - %m%n"/>
        </layout>
    </appender>
    <!-- name要对应字节码文件所在的包路径,只会记录该包路径下的代码的日志 -->
    <loggername="com.bocloud">
        <levelvalue="debug,info,error"/>
        <appender-refref="file"/>
        <appender-refref="console"/>
    </logger>
     
    <!-- Root Logger -->
    <root>
        <priorityvalue="debug"/>
        <appender-refref="console"/>
    </root>
     
</log4j:configuration>

你可能感兴趣的:(log4J配置及在web.xml文件中的配置)