Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 saving file (using RollingFile appender)

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.

like image 956
mine26 Avatar asked Nov 25 '12 12:11

mine26


People also ask

What is Rollingfile in Log4j2?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

What is the use of Appender in Log4j?

Log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as console, files, NT event logs, Swing components, JMS, remote UNIX syslog daemons, sockets, etc.

What is JMS Appender Log4j2?

Log4j JMS appender can be used to send your log messages to JMS broker. To use ActiveMQ as a destination of your messages, you need to configure JMS appender properly.


1 Answers

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.

like image 64
darrengorman Avatar answered Sep 24 '22 10:09

darrengorman