log4j 2 - configuration issue

2020-02-12 01:22发布

I am trying to configure log4j 2.0 to report logs.

My config is saved as log4j2.xml and this is its content:

  <?xml version="1.0" encoding="UTF-8"?>
  <configuration name="PRODUCTION" status="OFF">

    <appenders>
        <RollingFile name="MyFileAppender" 
            fileName="../Logs/app.log" 
            filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingFile>
    </appenders>

    <loggers>
        <root level="trace">
            <appender-ref ref="MyFileAppender"/>
        </root>
    </loggers>

 </configuration>

It exists in the classpath of the project and I tried putting it in many other directories..

I created a logger in the code like so:

    Logger          logger = LogManager.getLogger(MyClass.class.getName());
    logger.info("test");

And nothing is written and no file is created. When I debug the code I see that the logger is the default logger(console).

标签: java log4j2
12条回答
冷血范
2楼-- · 2020-02-12 02:03

In the documentation of log4j 2: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

"If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath."

but it is not working with classpath. Instead if we keep it in src folder, then it is working.

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>

similar problem is mentioned here : https://issues.apache.org/jira/browse/LOG4J2-357

查看更多
来,给爷笑一个
3楼-- · 2020-02-12 02:09

Ok, I solved the problem. I had to specify in the xml the package="myPackage"

查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-12 02:10

I also faced the same problem.I kept my log4j2.xml file in System environment variable.

Variable name : sys_logroot Variable value : D:\user\gouse

and no logs are created for me.

use the system variable-Dlog4j.configurationFile=path/to/log4j2.xmlSee here

This solve my problem

查看更多
不美不萌又怎样
5楼-- · 2020-02-12 02:17

I'm quite sure that you have to write down the full qualified name of the class whose messages you want to be logged - something like com.application.log4jtest.YourClass. If that doesn't work, try fiddling with the log level.

Also - just as a notice - you can also write

Logger logger = LogManager.getLogger(MyClass.class); // omit .getClass()
查看更多
祖国的老花朵
6楼-- · 2020-02-12 02:17

I had the same 'ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.' message over and over. It made me crazy. The log4j2.xml file was placed correctly at src/main/resources, like i did at thousands of projects before.

The solution in my case was to remove <packaging>pom</packaging> from the root pom.xml. packaging pom causes the content of src/main/resources not to be copied to target/classes.

Happy logging for anyone with the same root cause.

查看更多
家丑人穷心不美
7楼-- · 2020-02-12 02:18

I had similar problem. I put the file under src folder and it worked. I did not mention any package name in the log4j2.xml file.

查看更多
登录 后发表回答