I am trying log4j2 to create log file to the system I am developing right, I have followed the instruction on their site and there is no error occurred when I run it, but the log is not saved on where I set it (ex. "D:\logs\app.log").

Here is My log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
  <appenders>
    <RollingFile name="MyRollingFile" fileName="D:/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>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="20"/>
    </RollingFile>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </appenders>
  <loggers>
    <logger name="Log_RollingFile" level="TRACE" additivity="false">
      <appender-ref ref="MyRollingFile"/>
    </logger>
    <root level="ERROR">
      <appender-ref ref="Console"/>
    </root>
  </loggers>
</configuration>

I tried to :

  • delete app.log to see if my configuration (D:\logs\app.log) works. When I run the application it creates app.log so I think it means that it sees the configuration and the only thing is it is NOT SAVING the log.info that I did in java application
  • Change root level to "TRACE", and it prints the log.info.

[EDIT:]

I have also these libraries on my classpath

  • log4j-api-2.0-beta3.jar
  • log4j-core-2.0-beta3.jar

Am I missing something on RollingFile configuration or a library (maybe)?

Thanks in advance.

有帮助吗?

解决方案

Your logger name is incorrect.

As explained in the configuration instructions you linked to, the logger should be named according to the package/classes you wish to capture logging for.

In their example the logger named com.foo.Bar would log everything from the Bar class in package com.foo with TRACE level.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top