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.log 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
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.
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>
your DatePattern should be '.'yyyy-MM-dd
refer to https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html
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
Related
I have a Maven project in Eclipse that has the following dependencies, among others:
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.4.13</version>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.2.2</version>
</dependency>
As part of my troubleshooting efforts, I would like to use log4j (or a similar library) to view the debug output that originates from these libraries. In my src/main/resources folder I have the following log4j.properties file:
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I have changed the level of rootLogger to DEBUG but I am still unable to see the debug messages.
I would like to look at the debug messages of one or more specific classes - org.hyperledger.fabric.sdk.NetworkConfig for instance. How should I configure my log4j.properties to achieve this?
Update: I have just been made aware of the log4j security vulnerability. Nevertheless, I would like to continue with seeking help for my question as I have confidence that the situation will be resolved soon.
I am deploying my Spring Boot Application on an external Tomcat Server. I can't seem to make my eclipse console display the log messages that I write in my code:
private static final Logger log = LoggerFactory.getLogger(PersonServiceImpl.class);
...
log.info("Running...");
I managed to log all hibernate trace but still can't show the log messages I am adding to my code:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.root=INFO
logging.level.org.springframework.web=INFO
And the catalina log folder doesn't contain the logged infos I am needing.
What should I do?
I think, you deploy your spring boot application in an external tomcat, you package it as war file.
You can check this configuration :
In your pom.xml
<!-- mark the embedded tomcat server as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
and in the packaging value :
<packaging>war</packaging>
In the dependencies, if you use the Web starter :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
it's depends transitively on the logging starter, that means we can only add the starter web dependecy, no more logging component to provide.
And In the application.properties file, add the following lines :
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
After that, using maven, you can type :
mvn clean package
And move it to your external tomcat :
cp target/project.war apache-tomcat-8.X.X/webapps/project.war
./apache-tomcat-8.X.X/bin/startup.sh
Please configure your application.properties or application.yml
file
application.properties
#Logging pattern for file.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
#output to a file
logging.file=/path_to_file/file_name.log
#Logging pattern for console and output.
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
I created a spring boot rest api and planning to use log4j for logging. I am able to log using dailyrollingfileappender but I dont like the format of the filename [app.log.date] so I tried using log4j extras. I'm having problems I have an error when I start tomcat
"log4j:ERROR Could not instantiate class [TimeBasedRollingPolicy].
java.lang.ClassNotFoundException: TimeBasedRollingPolicy"
Here's my pom.xml log4j dependencies
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
Here's my log4j properties file (Currently modifying auditLogger)
log4j.rootLogger=INFO, stdout
log4j.logger.auditLogger = INFO, auditLogger
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.generalLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.generalLogger.File=C:\\logs\\sms-syslog.log
log4j.appender.generalLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.generalLogger.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.generalLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.category.generalLogger=INFO, generalLogger
log4j.additivity.generalLogger=false
log4j.appender.auditLogger=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.auditLogger.rollingPolicy=TimeBasedRollingPolicy
log4j.appender.auditLogger.File=C:\\logs\\sms-audit.log
log4j.appender.auditLogger.rollingPolicy.FileNamePattern=C:\\logs\\sms-audit-%d{yyyy-MM-dd}.log
log4j.appender.auditLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.auditLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n
log4j.additivity.auditLogger=false
Here's my call for auditlogger in code
private static final Logger auditLogger = Logger.getLogger("auditLogger");
Please help. I am quite lost Thank you!
use below dependency for TimeBasedRollingPolicy :
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.2</version>
</dependency>
refer below link for reference: https://examples.javacodegeeks.com/enterprise-java/logback/logback-rollingfileappender-example/
So the answer was provided by Pete.
The properties file was fine, I just needed the FQN.
log4j.appender.auditLogger.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
I have a web application using log4j2. The logs should be created on a daily basis.
Problem: the content of the old file never gets deleted, but any new day is just appended to that file. So it grows continuously. Is the following confniguration correct in general, when running on a tomcat8?
log4j2.xml:
<Configuration>
<Appenders>
<RollingFile name="TEST" fileName="d:\test-application.txt" filePattern="d:\test-application-%d{yyyy-MM-dd}.log">
<Policies>
<TimeBasedTriggeringPolicy modulate="true"/>
</Policies>
//...
<RollingFile>
//...
</Appenders>
//...
</Configuration>
Maven:
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
You are not the first person to report this behavior and the common factor seems to be Windows. Log4j tries to rename the file. If that fails it tries to copy and delete then delete the file. If there is a lock on the file the rename will fail and then most likely the copy is succeeding but the delete is failing. But the code is still using the delete method of the File object, which is not good at reporting errors and Log4j isn't checking the return value, so it is failing silently.
This behavior is a bug and should be fixed, but it won't really solve your problem - it will just let you know about it. To fix it you need to find out what is preventing the rename from succeeding.
What we often see is multiple processes or multiple web applications using the same log4j2.xml configuration and logging to the same file. One process will then prevent the other from deletion the file.
I've had this problem when I kept the log file open in an editor, and the midnight rollover would fail because of this.
The "Hello World" example from SLF4J is not working for me. I guess this is because I added slf4j-log4 to my classpath. Should I configure log4j directly for the hello world to work?
log4j:WARN No appenders could be found for logger (HelloWorld).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Update: I added log4j initialization, and it still doesn't work:
public static void main(String[] params) {
org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender());
Logger logger = org.slf4j.LoggerFactory.getLogger(TestBase.class);
logger.info("Hello World");
}
And I'm getting:
log4j:ERROR No output stream or file set for the appender named [null].
If you want to use slf4j simple, you need these jar files on your classpath:
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
If you want to use slf4j and log4j, you need these jar files on your classpath:
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.16.jar
No more, no less. Using slf4j simple, you'll get basic logging to your console at INFO level or higher. Using log4j, you must configure it accordingly.
Following is an example. You can see the details http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/ and download the full codes here.
Add following dependency to your pom if you are using maven, otherwise, just download the jar files and put on your classpath
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
Configure log4j.properties
log4j.rootLogger=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSS} %-5p [%c] - %m%n
Java example
public class Slf4jExample {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(Slf4jExample.class);
final String message = "Hello logging!";
logger.trace(message);
logger.debug(message);
logger.info(message);
logger.warn(message);
logger.error(message);
}
}
you need to add 3 dependency ( API+ API implementation + log4j dependency)
Add also this
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
# And to see log in command line , set log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#And to see log in file , set log4j.properties
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Here a working example to use slf4j as façade with log4j in the backend:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>
src/main/resources/log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
src/main/java/Main.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
/**
* Default private constructor.
*/
private Main() {
}
/**
* Main method.
*
* #param args Arguments passed to the execution of the application
*/
public static void main(final String[] args) {
logger.info("Message to log");
}
}
I had the same problem. I called my own custom logger in the log4j.properties file from code when using log4j api directly. If you are using the slf4j api calls, you are probably using the default root logger so you must configure that to be associated with an appender in the log4j.properties:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender