Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 7 records all logs into catalina.out but not into log4j2 defined files

Before converting my application into the web app. Log4j was recording logs in the correct log files (info.log and debug.log) which I've defined in the log4j2.xml.

But when I've changed my application to web.app, all logs now records into the catalina.out file in the servlet container (tomcat 7). After deploying application log4j2 creates these log file, but they remain empty and all logs goes to catalina.out file.

Could you suggest what I am doing wrong.

In web.xml, I've added necessary configuration.

<?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>

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>
like image 237
slisnychyi Avatar asked Feb 24 '14 10:02

slisnychyi


People also ask

Is tomcat affected by log4j2?

Log4j may be used as the logging framework for Apache Tomcat. This support is implemented automatically by including the log4j-api, log4j-core, and log4j-appserver jars in the boot classpath. A file named log4j2-tomcat. xml, log4j2-tomcat.

Where are the Catalina logs?

By default, the catalina. out file is located in the logs directory under Tomcat's root directory. For example, /opt/netiq/idm/apps/tomcat/logs/catalina.


1 Answers

change fileName attribute from

fileName="/local/deploy/logs/info.log"

to something like this(whatever the path in ur case)

fileName="${sys:catalina.home}/logs/info.log"

if it writes in both catalina as well as in log file then refer this link

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

like image 81
Gomti Avatar answered Nov 02 '22 05:11

Gomti