简体   繁体   English

找不到Log4j2.xml配置文件

[英]Log4j2.xml configuration file not found

I have a project where I am trying to get Apache Log4j2 to work for logging in different files. 我有一个项目,试图将Apache Log4j2用于登录不同的文件。 My project is Spring application and I have a configuration file log4j2.xml in the project structure, but when I run the project, either as Spring Boot or as a war from tomcat, no logging seem to happen except the default standard output. 我的项目是Spring应用程序,并且在项目结构中有一个配置文件log4j2.xml,但是当我运行该项目时(无论是Spring Boot还是来自tomcat的战争),除了默认的标准输出外,似乎没有日志记录发生。

My project structure: 我的项目结构:

-src
--main
---resources
----static
----templates
----application.properties
----log4j2.xml

My configuration file: 我的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" monitorInterval="1800">

  <Properties>
        <Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex</Property>
        <Property name="log-path">${sys:catalina.home}/logs</Property>
        <Property name="secondary_path">C:/TEMP/logs</Property>
    </Properties>

  <Appenders>

    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="${logPattern}"/>
    </Console> 

    <RollingFile name="FileAppenderInfo" fileName="${secondary_path}/stream-info.log" filePattern="${secondary_path}/stream-info-%d{yyyy-MM-dd}-%i.log" append="true" immediateFlush="true">
        <PatternLayout>
            <Pattern>${logPattern}</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>


    <RollingFile name="FileAppenderError"  fileName="${secondary_path}/stream-error.log" filePattern="${secondary_path}/stream-error-%d{yyyy-MM-dd}-%i.log" append="true" immediateFlush="true">
        <PatternLayout>
            <Pattern>${logPattern}</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>

  </Appenders>

  <Loggers>

    <Logger name="InfoLogger" level="error">
        <AppenderRef ref="FileAppenderInfo"/>
    </Logger>

    <Logger name="ErrorLogger" level="error">
        <AppenderRef ref="FileAppenderError"/>
    </Logger>

    <Root level="error">
      <AppenderRef ref="STDOUT"/>
    </Root>

  </Loggers>

</Configuration>

I have tried looking for a solution but can't seem to find out what is wrong, I have even tried making a new project and implement the dependencies, xml file and tried logging in the code. 我曾尝试寻找解决方案,但似乎无法找出问题所在,我什至尝试创建一个新项目并实现依赖项,xml文件并尝试登录代码。

My code looks like: 我的代码如下:

private static final Logger log = LogManager.getLogger("InfoLogger");

log.info("POST HAS BEEN MADE");

Pom file: Pom文件:

<!-- Dependency for log4j2 -->

      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.1</version>
      </dependency>

I have also tried without using the version tag. 我也尝试过不使用版本标签。

Hope anyone can help Thanks 希望任何人都可以帮助谢谢

Two suggestions : 两个建议:

1) Use package name to specify the logger. 1)使用软件包名称指定记录器。

   <Logger name="com.sample" level="error">
     <AppenderRef ref="FileAppenderInfo"/>
   </Logger>

2) Change your code to match logger 2)更改您的代码以匹配记录器

private static final Logger log = LogManager.getLogger(); 私有静态最终Logger日志= LogManager.getLogger(); // Take the current class name by default //默认使用当前的类名

OR 要么

LogManager.getLogger(Test.class.getName()); LogManager.getLogger(Test.class.getName()); //Specify class name //指定类名

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

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