简体   繁体   中英

Log4j2 prints on Console works find, but no log file created

I am using Spring + Jersey, but no Spring Boot. When I set up Log4j2, messages are printed on console, but no log file created.

The configuration XML:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
    <Appenders>
        <File name="FILE" fileName="logfile.log" append="true">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="com.karapax" level="error"/>

        <Root level="info">
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="FILE"/>
        </Root>
    </Loggers>

</Configuration>

It a Maven project, and in my pom.xml, the related Library are shown below. I didn't give all the other libraries being used in this project.

<!-- Spring dependencies -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${org.springframework.version}</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.11.0.RELEASE</version>
</dependency>
<!-- Log Dependencies -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>${log4j.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${log4j.version}</version>
</dependency>

I Run the code below on local Tomcat.

@Path("log/")
public class LogTestServelt extends BaseServlet {

    private final Logger logger = LogManager.getLogger(this.getClass());

    @GET
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
    public void testLog() {
        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.warn("This is a warn message");
        logger.error("This is an error message");
    }
}

When I am trying to send a request to this endpoint, the messages print out on console is OK, but no log file is created.

Any one can me?Thanks

The pathname "logfile.log" in your config file is a relative pathname .

Since you have specified a relative pathname for the logfile, the file will be created relative to the working directory of the running Tomcat process. The working directory location will depend on precisely how Tomcat was launched.

(We cannot say for certain where the logfile will be. It might be in your IDE's workspace. It might be in the Tomcat installation tree. It could be somewhere else. It is even possible that Tomcat's working directory is not writeable by the Tomcat process ... and the log file cannot be created.)

Solutions:

  • Use an OS specific tool to search for the logfile. For example, find on Linux.
  • Specify an absolute pathname for the logfile in the log4j2 config file.

I encountered with the same issue.

Actually, my file was being created ever time, but I was not able to find it.

As per your given configuration, it will be created where you IDE (ie, Eclipse / STS / Netbeans etc..) is installed. Try to find in that directory. I am sure that you will be able to find it there.

你会在 tomcat webapp 目录中找到

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