简体   繁体   English

Log4j2 在控制台上打印工作找到,但没有创建日志文件

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

I am using Spring + Jersey, but no Spring Boot.我正在使用 Spring + Jersey,但没有使用 Spring Boot。 When I set up Log4j2, messages are printed on console, but no log file created.当我设置 Log4j2 时,消息打印在控制台上,但没有创建日志文件。

The configuration XML:配置 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.它是一个 Maven 项目,在我的 pom.xml 中,相关库如下所示。 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.我在本地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 .配置文件中的路径名"logfile.log"是一个相对路径名

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.由于您已为日志文件指定了相对路径名,因此将相对于正在运行的 Tomcat 进程的工作目录创建该文件。 The working directory location will depend on precisely how Tomcat was launched.工作目录位置将取决于 Tomcat 的启动方式。

(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.) (我们不能确定日志文件的位置。它可能在您 IDE 的工作区中。它可能在 Tomcat 安装树中。它可能在其他地方。甚至有可能 Tomcat 进程无法写入 Tomcat 的工作目录...并且无法创建日志文件。)

Solutions:解决方案:

  • Use an OS specific tool to search for the logfile.使用特定于操作系统的工具搜索日志文件。 For example, find on Linux.例如,在 Linux 上find
  • Specify an absolute pathname for the logfile in the log4j2 config file.在 log4j2 配置文件中指定日志文件的绝对路径名。

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.根据您给定的配置,它将在您安装 IDE(即 Eclipse/STS/Netbeans 等)的位置创建。 Try to find in that directory.尝试在该目录中查找。 I am sure that you will be able to find it there.我相信你会在那里找到它。

你会在 tomcat webapp 目录中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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