tomcat使用log4j2打日志

开始前准备

  1. tomcat版本8.0.51 (下载地址:http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.51/bin/apache-tomcat-8.0.51.tar.gz) 2018-05-09可以访问
  2. tomcat日志扩展接口包 tomcat-juli.jar tomcat-juli-adapters.jar (下载地址:http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.51/bin/extras/) 2018-05-09可以访问
  3. log4j2版本2.3 log4j-core-2.3.jar log4j-api-2.3.jar log4j-1.2-api-2.3.jar (下载地址:http://www.apache.org/dist/logging/log4j/2.3/apache-log4j-2.3-bin.zip) 2018-05-09可以访问 (三个jar都在bin压缩包里)
  4. 新建log4j2.xml
<?xml version="1.0" encoding="utf-8"?>
<Configuration status="info">
    <Properties>
        <Property name="logdir">${sys:catalina.base}/logs</Property>
        <Property name="layout">%d [%t] %-5p %c-%m%n</Property>
    </Properties>
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="${layout}" />
        </Console>
        <RollingFile name="CATALINA" fileName="${logdir}/catalina.log"
            filePattern="${logdir}/catalina.%d{yyyy-MM-dd}.log.gz">
            <PatternLayout charset="UTF-8" pattern="${layout}" />
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
            <SizeBasedTriggeringPolicy size="50MB" />
            <DefaultRolloverStrategy max="20" />
        </RollingFile>
        <RollingFile name="LOCALHOST" fileName="${logdir}/localhost.log"
            filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout charset="UTF-8" pattern="${layout}" />
            <SizeBasedTriggeringPolicy size="50MB" />
            <DefaultRolloverStrategy max="20" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger
            name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
            level="info">
            <AppenderRef ref="LOCALHOST" />
        </Logger>
        <Root level="info">
            <AppenderRef ref="CATALINA" />
            <AppenderRef ref="CONSOLE" />
        </Root>
    </Loggers>
</Configuration>
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
36

操作

  1. jar包 三个 log4j2 的jar包 和 tomcat-juli-adapters.jar放到 tomact 目录下的lib文件夹下 tomcat-juli.jar 替换tomcat目录下 bin 文件夹下的同名文件
  2. 配置文件 删除或重命名tomact目录下conf文件夹下的logging.properties文件 log4j2.xml,放到tomcat目录下lib文件夹下

启动tomcat,在tomcat目录下logs文件夹下查看日志文件

原文转载自:https://blog.csdn.net/Json0108/article/details/80258933

上次更新: 2024-11-06, 19:27:10
最近更新
01
java playwright爬虫
11-06
02
连接chrome调试
07-23
03
连接chrome调试
07-23
更多文章>