Reputation: 708
I have a java web app that uses logback with slf4j for logging. And this project has a dependency jar (which is a sub project). And this dependency jar uses org.apache.log4j.Logger for logging. All logs must go into one log file. My problem is, whatever I am logging with the code in the jar file is not being written to the log file. Initially I had logback.xml. To resolve the above problem I added log4j.properties file to my web app. It resolved my problem and now I can see all logs in one file.
Again my new probelm is: Earlier the log file was rolling every day according to the rolling policy in logback.xml. Now it is not rolling. Even after I configured the rolling policy in log4j.properties that matches with the rolling policy that is in logback.xml, rolling is not happening.
I don't want to replace log4j in my dependency with logback. And also I want to write alll my logs into one file. Is it possible to this, how can I resolve my issue?
FYI: I have "log4j-over-slf4j" dependency included in my pom.xml
Log4j.properties:
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=SOME_PATH/logs/studentApp.log
log4j.appender.file.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.FileNamePattern = SOME_PATH/logs/studentApp.%d{yyyy-MM-dd}.log
log4j.appender.file.maxBackupIndex=14
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.rootLogger=DEBUG,file
Upvotes: 4
Views: 6908
Reputation: 489
The following configuration will create a .log file for the current date and keep adding the log in that file. When the date chanages, it will create a new file with the current date. This configuration will also zip the files as or when they exceed a certain size limit.
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 500MB -->
<maxFileSize>300MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{0} - %msg%n</pattern>
</encoder>
</appender>
Hope it helps.
Upvotes: 1
Reputation: 2566
Assuming your rolling policy is correct, it's possible that log4j picks up the wrong configuration. log4j looks for the first configuration file in the classpath (first .xml
then .properties
) and if it happens to be the configuration from one of your dependencies your rolling policy will not work.
To test this you can add a system property -Dlog4j.configuration=...
with the path to your configuration to the tomcat startup script. Or simply set your log level to TRACE/DEBUG and see if it affects the output.
Edit: your log4j config looks good, but the date pattern is missing.
Try adding
log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm
(note that I've set the pattern to roll-over every minute, for easier testing)
Edit: you can remove every line containing rollingPolicy
and maxBackupIndex
- those will not be picked up in this case
Upvotes: 1