如何将tomcat8的运行日志框架切换到log4j2

Tomcat 专栏收录该内容
11 篇文章 0 订阅

很多项目都使用log4j作为日志框架,但是tomcat默认的框架不是log4j的,打印出来的日志也不会保存到项目指定的地方,而对于那些系统运行过程中出现的异常信息,我们是需要监控的,所以我们会希望tomcat和项目的日志放到一个地方,这时我们就需要修改tomcat默认的日志框架。

tomcat官网上面有相关日志框架切换的文档,https://tomcat.apache.org/tomcat-8.0-doc/logging.html,不过这个是针对log4j的,现在大家都已经开始使用log4j2了,所以今天给大家介绍下tomcat8如何集成log4j2。

1,下载相关jar包
从tomcat官网上下载扩展包:tomcat-juli.jar和tomcat-juli-adapters.jar,log4j2我们用的版本是2.10,选择log4j-1.2-api-2.10.0.jar(用于log4j2与log4j1的桥接)、log4j-api-2.10.0.jar和log4j-core-2.10.0.jar三个包。

2,部署jar包
将log4j2的3个jar包和 tomcat-juli-adapters.jar放到tomact目录下的lib文件夹下,将tomcat-juli.jar复制到tomcat目录下bin文件夹下替换原来的文件。

3,创建日志配置文件
在tomcat目录下lib文件夹下创建log4j2.xml,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="off">
    <Properties>
        <Property name="CATALINA_BASE_LOGPATH">../logs</Property>         
    </Properties>
    <appenders>
        <console name="CONSOLE" target="SYSTEM_OUT">
            <patternLayout charset="UTF-8"
                pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
        </console>

        <rollingFile name="CATALINA"
            fileName="${CATALINA_BASE_LOGPATH}/catalina.log"
            filePattern="${CATALINA_BASE_LOGPATH}/catalina.%date{yyyyMMdd}">              
            <patternLayout charset="UTF-8"
                pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
            <policies>
                <timeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </policies>
        </rollingFile>

        <rollingFile name="LOCALHOST"
            fileName="${CATALINA_BASE_LOGPATH}/localhost.log"
            filePattern="${CATALINA_BASE_LOGPATH}/localhost.%date{yyyyMMdd}">              
            <patternLayout charset="UTF-8"
                pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
            <policies>
                <timeBasedTriggeringPolicy />
            </policies>
        </rollingFile>

        <rollingFile name="MANAGER"
            fileName="${CATALINA_BASE_LOGPATH}/manager.log"
            filePattern="${CATALINA_BASE_LOGPATH}/manager.%date{yyyyMMdd}">              
            <patternLayout charset="UTF-8"
                pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
            <policies>
                <timeBasedTriggeringPolicy />
            </policies>
        </rollingFile>

        <rollingFile name="HOST-MANAGER"
            fileName="${CATALINA_BASE_LOGPATH}/host-manager.log"
            filePattern="${CATALINA_BASE_LOGPATH}/host-manager.%date{yyyyMMdd}">              
            <patternLayout charset="UTF-8"
                pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
            <policies>
                <timeBasedTriggeringPolicy />
            </policies>
        </rollingFile>             

    </appenders>

    <loggers>
        <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="INFO">
            <appender-ref ref="LOCALHOST" />     
        </logger>

        <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="INFO"  additivity="false"> 
            <appender-ref ref="MANAGER" />
        </logger>

        <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="INFO"  additivity="false">
        <appender-ref ref="HOST-MANAGER" />   
        </logger>

        <root level="INFO">
            <appender-ref ref="CATALINA" />
            <appender-ref ref="CONSOLE" />
        </root>
    </loggers>
</configuration>

日志输出的格式可以根据自己的需要进行修改。

4,删除tomcat默认日志配置
删除tomact目录下conf文件夹下的logging.properties文件。

现在启动项目,查看输出的日志文件已经是log4j打印出来的了。

  • 0
    点赞
  • 4
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
Tomcat8下使用Log4j接管生成日志文件,按天存放,日志转换成json格式 亲测可用,日志格式如下: {"time":"2017-09-06 10:24:48,356","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory C:\Users\admin\Desktop\Tomcat8\webapps\docs"} {"time":"2017-09-06 10:24:48,375","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deployment of web application directory C:\Users\admin\Desktop\Tomcat8\webapps\docs has finished in 19 ms"} {"time":"2017-09-06 10:24:48,376","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory C:\Users\admin\Desktop\Tomcat8\webapps\examples"} {"time":"2017-09-06 10:24:48,648","logtype":"INFO","loginfo":"org.apache.catalina.core.ContainerBase.[Catalina].[bms-res.hzfh.com].[/examples]:ContextListener: contextInitialized()"} {"time":"2017-09-06 10:24:48,649","logtype":"INFO","loginfo":"org.apache.catalina.core.ContainerBase.[Catalina].[bms-res.hzfh.com].[/examples]:SessionListener: contextInitialized()"} {"time":"2017-09-06 10:24:48,654","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deployment of web application directory C:\Users\admin\Desktop\Tomcat8\webapps\examples has finished in 279 ms"} {"time":"2017-09-06 10:24:48,654","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory C:\Users\admin\Desktop\Tomcat8\webapps\host-manager"} {"time":"2017-09-06 10:24:48,682","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deployment of web application directory C:\Users\admin\Desktop\Tomcat8\webapps\host-manager has finished in 28 ms"} {"time":"2017-09-06 10:24:48,682","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying web application directory C:\Users\admin\Desktop\Tomcat8\webapps\manager"} {"time":"2017-09-06 10:24:48,711","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deployment of web application directory C:\Users\admin\Desktop\Tomcat8\webapps\manager has f
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值