简体   繁体   中英

How to use log4j2 on tomcat9

I am using log4j2 to print log to a file. It work when I use it on my own computer, but when I deploying war on tomcat, It didn't work. And I have wasted a whole day on this.
My tomcat version:

Server version: Apache Tomcat/9.0.62
Server built:   Mar 31 2022 14:34:15 UTC
Server number:  9.0.62.0
OS Name:        Linux
OS Version:     4.15.0-161-generic
Architecture:   amd64
JVM Version:    17.0.2+8-Ubuntu-118.04
JVM Vendor:     Private Build

My log4j2.xml:

<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" strict="true" status="ERROR" name="ConfigTest">
    <Properties>
        <Property name="FILE_NAME">./src/logs/myLog.log</Property>
    </Properties>
    <Appenders>
<!--        log to file-->
        <Appender type="File" name="File" fileName="${FILE_NAME}">
            <Layout type="PatternLayout">
                <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
            </Layout>
        </Appender>
<!--        log to console-->
        <Appender type="Console" name="STDOUT">
            <Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n"/>
            <Filters>
                <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
                <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/>
            </Filters>
        </Appender>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

And I use log4j2 like this:

@SpringBootApplication
public class DemoApplication extends ServletInitializer { //

    private static final Logger shortMessageLogger = LogManager.getLogger("shortMessageLogger");
    public static void main(String[] args) {
        shortMessageLogger.error("start running...");
        SpringApplication.run(DemoApplication.class, args);
    }
}

And my application.properies:

logging.config=classpath:log4j2.xml

And my maven pom.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

            <!-- exclusion embedded loggin -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- log4j2 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-appserver</artifactId>
            <version>2.17.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <finalName>Demo</finalName>
    </build>
</project>

I found maybe it's because the tomcat9 has its own logger configuration and covered my config. I also referred to theApache log4j2 documentation , but failed because it was not specific enough. So where I could get the more detailed process?

Tomcat by default uses java.util.logging as its logging backend, so it does not interfere with your application logging.

The most probable reason for your lack of logs is a permission problem, when creating the file ./src/logs/myLog.log . The error should be logged by Log4j's status logger (on stdout , which usually ends up in catalina.out ).

When you run a Tomcat server, you can never assume anything about the working directory of the server, so you should not use relative paths. A more safe location for your log file is ${sys:catalina.base}/logs/myLog.log , since Tomcat can write to that directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM