Tomcat 7将所有日志记录到catalina.out中,但不记录到log4j2定义的文件中

sli*_*hyi 7 java web.xml log4j classpath tomcat7

在将我的应用程序转换为Web应用程序之前 Log4j将日志记录在我在log4j2.xml中定义的正确日志文件(info.log和debug.log)中.

但是当我将我的应用程序更改为web.app时,所有日志现在都记录到servlet容器(tomcat 7)中的catalina.out文件中.部署应用程序后,log4j2会创建这些日志文件,但它们仍为空,所有日志都会转到catalina.out文件.

你能说出我做错了吗?

web.xml中,我添加了必要的配置.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>project-service</display-name>

    <!-- Support for Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>log4j2.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>


</web-app>
Run Code Online (Sandbox Code Playgroud)

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" strict="true" name="XMLConfigTest"
               packages="">         

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="RollingRandomAccessFileDebug" fileName="/local/deploy/logs/debug.log"
                                 filePattern="logs/$${date:yyyy-MM}/project-%d{MM-dd-yyyy}-%i.log.gz"
                                 immediateFlush="false"
                                 append="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="RollingRandomAccessFile" fileName="/local/deploy/logs/info.log"
                                 filePattern="logs/$${date:yyyy-MM}/project-%d{MM-dd-yyyy}-%i.log.gz"
                                 immediateFlush="false"
                                 append="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <!--<Async name="AsyncConsole">-->
        <!--<AppenderRef ref="Console"/>-->
        <!--</Async>-->
    </Appenders>
    <Loggers>
        <Root  level="TRACE">
            <AppenderRef ref="RollingRandomAccessFileDebug" level="DEBUG"/>
            <AppenderRef ref="RollingRandomAccessFile" level="INFO"/>
            <AppenderRef ref="Console"  level="TRACE"/>
        </Root>
    </Loggers>
</Configuration>
Run Code Online (Sandbox Code Playgroud)

小智 5

从中更改fileName属性

文件名= "/本地/配置/日志/ info.log建立"

这样的事情(无论你的情况如何)

文件名= "$ {SYS:的catalina.home} /logs/info.log"

如果它同时写入catalina和日志文件,请参阅此链接

http://app-inf.blogspot.com/2012/10/java-util-logging-behaviour-in-tomcat.html