0

DailyRollingFileAppender is not creating daily backup log file.

I am using the below config, which works on my local machine but it not working on the machine where my project has been deployed.

log4j.rootLogger=DEBUG, Appender2

log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Appender2.File=C:/Logs/AppLog.log
log4j.appender.Appender2.DatePattern='.'dd-MM-yyyy
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2.rootLogger = DEBUG

Framework - Spring MVC

I am not able to understand which part of the config is bloking DailyRollingFileAppender to create date wise log on my server machine.

Edit-

I updated my file as per the suggestion and it is not creating a new backup file at 12 am next day. means it updated AppLog.l‌​og till 12 then there was no backup file and all the previous day logs are gone and it starts writing from the beginning.

This is log4j properties now-

log4j.rootLogger=DEBUG, Appender2
log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Appender2.File=${catalina.home}/Logs/AppLog.log
log4j.appender.Appender2.DatePattern='.'yyyy-MM-dd
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.Append=false
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
Rahul
  • 73
  • 1
  • 2
  • 12

4 Answers4

0

The issue is with the file path here:

log4j.appender.Appender2.File=C:/Logs/AppLog.log

Please make sure that this path exists on the server where you have deployed your project.

Chinmay jain
  • 979
  • 1
  • 9
  • 20
0

I faced with this problem before, the cause turned to be I used wrong log4j dependency in pom.xml. The previous dependency is:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

I use spring boot in my project, so I changed it to the following, it worked.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
    <version>1.3.8.RELEASE</version>
</dependency>
Dave Pateral
  • 1,415
  • 1
  • 14
  • 21
  • I am using the same setting as you were using previously. Do you know the dependency in pom.xml for Spring-MVC? – Rahul Sep 12 '17 at 05:39
  • `spring-boot-starter-web` if you use spring boot, otherwise declare `spring-webmvc` is enough. – Dave Pateral Sep 12 '17 at 06:12
0

your DatePattern should be '.'yyyy-MM-dd

refer to https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html

Wei Kleeff
  • 188
  • 1
  • 11
-1

u can use this for get the daily rolling log file,

########## Appender Daily Rolling
log4j.logger.appender=Daily
log4j.appender.Daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Daily.Threshold=INFO
log4j.appender.Daily.File=D:/backup/RFLI1010.log
log4j.appender.Daily.DatePattern='.'yyyy-MM-dd
# Append to the end of the file or overwrites the file at start.
log4j.appender.Daily.Append=true
log4j.appender.Daily.MaxBackupIndex=20
log4j.appender.Daily.layout=org.apache.log4j.PatternLayout
log4j.appender.Daily.layout.ConversionPattern=  [%5p] %d %r %t (%F:%M:%L)%m%n%n
Balasubramanian
  • 700
  • 7
  • 26
  • Tried your way but it did not make any changes. Also shouldn't log4j.appender.Daily.Append be false? if I write true then it will append to same file, right? – Rahul Sep 12 '17 at 10:47
  • yes. if true means it will append same file. also share ur log4j.property ? – Balasubramanian Sep 12 '17 at 11:00
  • I have done some changes in the file- llog4j.rootLogger=DEBUG, Appender2 log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender log4j.appender.Appender2.File=${catalina.home}/Logs/AppLog.log log4j.appender.Appender2.DatePattern='.'yyyy-MM-dd log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout log4j.appender.Appender2.Append=false log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n – Rahul Sep 13 '17 at 08:09
  • Now it is creating a new file at 12 AM, but no backup file. means it starts writing in the same file from the beginning. – Rahul Sep 13 '17 at 08:12
  • log4j.appender.Appender2.Append=true.did u checked like this? – Balasubramanian Sep 13 '17 at 08:32
  • I tried, not on 12 am next day its not creating a new log file and not even writing in old one. means it updated AppLog.l‌​og till 12 then there was no backup file and code is not updating AppLog.l‌​og either. – Rahul Sep 14 '17 at 06:00
  • see this link, it might be help, https://stackoverflow.com/questions/27941144/how-to-create-multiple-log-files-using-log4j – Balasubramanian Sep 14 '17 at 06:06
  • sry. it is updating the log file. It appending the log file with next day logs also. Still no backup file. – Rahul Sep 14 '17 at 07:15