Log pattern provided in log4j.properties file is not being used - java

I am using slf4j logging library and have provide logging pattern in log4j.properties file under resources folder in my maven project but still the pattern I have provided does not take affect and is printed with default logging pattern.
i am using slf4j logging library
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
I am initializing the logger by
private static final Logger logger = LoggerFactory.getLogger(CurrentClassName.class);
My log4j.properties file content is
# Direct log messages to stdout
log4j.rootLogger=DEBUG, STDOUT, file
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=%5p[] [%t] (%F:%L) - %m%n
log4j.appender.stdout.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-30logger{20}] [${application.name}] [%X{request_id}] - %msg%n"
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-30logger{20}] [${application.name}] [%X{request_id}] - %msg%n"
ACTUAL LOG OUTPUT
11:07:45.091 [main] DEBUG com.abc.def.LoggingClassName - message logged from within the class
Expected LOG OUTPUT
2019-07-17 12:03:59 [INFO ] [c.b.CurrentLoggingClassName ] [aplication-name] [] - message logged from class
P.S.
I have also tried renaming the log4j.properties file to log4j2.properties
And I have also tried both properties file placing under /src folder one by one

try using this
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

Related

log File not generating in log4j

I am new to log4j. I have created a sample java program implementing log4j in it.
Below is the java program:
package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
/**
* #param args the command line arguments
*/
private static Logger logger = Logger.getLogger(Logging.class);
public static void main(String[] args) {
BasicConfigurator.configure();
logger.trace("This is a Trace");
logger.debug("This is a Debug");
logger.info("This is an Info");
logger.warn("This is a Warn");
logger.error("This is an Error");
logger.fatal("This is a Fatal");
}
}
I am getting the output in the console screen.But the log file is not getting generated. I have also configured my project in the Eclipse neon using the following link:
Configuration
I have done everything good. But the log file is not generating.When I implement log4j programmatically the file is getting generated.The following is my properties file:
#root
log4j.logger.com.apress.logging.log4j=debug,dest
log4j.additivity.com.apress.logging.log4j=false
#define the appender
log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender
#set the name of the file
log4j.appender.dest.File=${user.home}/log.out
#setting the immediate flush to true (default)
log4j.appender.dest.ImmediateFlush=true
#setting the threshold
log4j.appender.dest.Threshold=ERROR
#setting the append to false, overwrite
log4j.appender.dest.Append=true
#set the DatePattern
log4j.appender.dest.DatePattern='.' yyyy-MM-dd
What do I need to do to have Log4J write to the log file?
Ensure log4j.properties is in default package
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
log4j.logger.infoLogger=DEBUG, infoLogger
log4j.additivity.infoLogger = false
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\LOG\\ConvertorLogger.log
log4j.appender.file.MaxFileSize=5MB
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.infoLogger=org.apache.log4j.RollingFileAppender
log4j.appender.infoLogger.File=E:\\LOG\\ConvertorInfoLogger.log
log4j.appender.infoLogger.MaxFileSize=5MB
log4j.appender.infoLogger.MaxBackupIndex=10
log4j.appender.infoLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.infoLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can refer to this fileļ¼šlog4j.properties
log4j.rootLogger=WARN,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c -%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${user.home}/log.out
log4j.appender.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %p %t %c -%m%n
log4j.logger.com.foo=WARN
I had to give the absolute path (for Windows, using Intellij IDEA) in my log4j2.xml file as following:
fileName="C:\Users\Fotios.Kolytoumpas\IdeaProjects\jetbrains-hibernate\logs\your-app-name.log"

Log4j's RollingFileAppender doesn't create a file

I have a Spring Boot application which uses Log4j. I've set up RollingFileAppender and ConsoleAppender. The second one works fine, but RollingFile Appender doesn't create a file.
Here's a file with properties:
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j-application.log
log4j.appender.file.MaxFileSize=5MB
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
Any ideas will be very appreciated.
Use this configurarion
log4j.rootLogger=DEBUG, consoleAppender, fileAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=demoApplication.log
For more info visit this link http://howtodoinjava.com/2013/04/08/how-to-configure-log4j-using-properties-file/
Or maybe you can try the following for daily-logging
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=logfile.log
log4j.appender.logfile.Threshold = INFO
log4j.appender.logfile.DatePattern='_'yyyyMMdd'.log'
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

Tomcat System.out redirection

We have got a legacy Java application, running on Tomcat. It logs its events in somewhat unconsistent way, sometimes even relying on System.out/err. These stdout/stderr records end up in catalina.log where they are mixed with "proper logged" events. We would like to separate the stdout/stderr log streams into separate files (and eventually get rid of them) but we don't know how to do it. The server runs default JULI configuration but we are open to any other option (such as SLF4J + Logback).
This explains it in full: Log4J Just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application. Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
Example of log4j.properties that would cease output to stdout:
log4j.rootLogger=INFO, yourapp
#Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
#Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
#Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\INFO, HOST-MANAGER
#You can define your Applog here:
log4j.appender.yourapp=org.apache.log4j.RollingFileAppender
log4j.appender.yourapp.File=${catalina.base}/logs/yourapp.log
log4j.appender.yourapp.MaxFileSize=100MB
log4j.appender.yourapp.MaxBackupIndex=10
log4j.appender.yourapp.layout=org.apache.log4j.PatternLayout
log4j.appender.yourapp.layout.ConversionPattern=%d %p [%c] (%t) - <%m>%n
Hope this helps!

Log messages except from a particular package to a file

I am using log4j for logging , my requirement is to log all the logs from package
pkg1 (except pkg1.pkg2) to pkg1.log
and pkg1.pkg2 (a sub package of the above package) to pkg2.log
is this possible ? I know i can achieve it through custom appenders but is there a way I can do it through configurations.
I am using the below properties file. Here the problem is PKG1.log contains logs from PKG2 also.
# Root logger option
log4j.rootLogger=WARN, CONSOLE
# Direct log messages to a log file
log4j.logger.com.pkg1=DEBUG, PKG1 log4j.appender.PKG1=org.apache.log4j.RollingFileAppender
log4j.appender.PKG1.File=logs/PKG1.log
log4j.appender.PKG1.MaxFileSize=1MB
log4j.appender.PKG1.MaxBackupIndex=1
log4j.appender.PKG1.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.com.pkg1.pkg2=DEBUG, PKG2
log4j.appender.PKG2=org.apache.log4j.RollingFileAppender
log4j.appender.PKG2.File=logs/PKG2.log
log4j.appender.PKG2.MaxFileSize=1MB
log4j.appender.PKG2.MaxBackupIndex=1
log4j.appender.PKG2.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can stop PKG2's logs from reaching it's ancestor's appenders by disabling the additivity flag in the configuration:
log4j.appender.PKG2.additivity=false

Disable log4j console logging and enable file logging

My log4j.properties file is:
log4j.rootLogger=WARN, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${jboss.server.log.dir}/afrodite.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.appender.scheduler=org.apache.log4j.RollingFileAppender
log4j.appender.scheduler.File=${jboss.server.log.dir}/afrodite_scheduler.log
log4j.appender.scheduler.MaxFileSize=10MB
log4j.appender.scheduler.MaxBackupIndex=10
log4j.appender.scheduler.Append=true
log4j.appender.scheduler.layout=org.apache.log4j.PatternLayout
log4j.appender.scheduler.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
If I want to disable console output for scheduler logger what should I do? That means scheduler logger only log into file not in console.
Thanks and Regards.
Add this line to your config:
log4j.appender.scheduler.additivity=false
log4j.additivity.info.release.scheduler=false
(sorry, first hint was incorrect.)
you can add two lines in your properties file.
log4j.rootLogger=ALL, stdout, file,scheduler (#here ALL declare for all level of logs)
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.additivity.info.release.scheduler=false

Categories

Resources