log4j does not log for file - java

I am using log4j to log to both console and file. It just works with console, but not with file, thought the file is being created
This is my configuration file:
name=PropertiesConfig
property.filename = logs
appenders = console, file
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
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/my_server_logs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = file, stdout
rootLogger.appenderRef.stdout.ref = STDOUT
What am I missing please?

The connection between file appender and root logger is missing.
Add the following line at the bottom of your properties file:
rootLogger.appenderRef.file.ref = LOGFILE
It will work as expected.

I think you expect that the variable ${filename} be substituted with the value of property.filename.
Can you try to substitute ${filename} with a hard path and try again (E.g: 'C:\' on Windows or '/tmp/' on Unix-like).

Related

Not Every log is printing in Log files using LOG4j2 Properties file

I have configured my webapp in java using log4j2 for logs using properties file.
When I am starting tomcat ,there are so many logs printing in console but in my file only few lines are printing.
What should I do in order to print every line which is present in console to my Log File.
Recently I have migrated from log 1.x to log 2.16
When I am using log1.x everything is printed in log files which are in console but in log2 its not printing.
For Example when I am staring tomcat ,I am getting only my app related logs in the file but not anything related to tomcat startup logs or any other jar related logs
Can anyone help me on this
below is my config file
log4j2 properties:
status = all
appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#appender.file.type = File
#appender.file.name = LogToFile
#appender.file.fileName=logs/app.log
#appender.file.layout.type=PatternLayout
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = ${catalina.home}/logs/SunnyLogs.log
appender.rolling.filePattern = ${catalina.home}/logs/$${date:yyyy-MM}/SunnyLogs-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10
# Log to console and rolling file
logger.app.name = com.sunny.common
logger.app.level = all
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole
rootLogger.level = all
rootLogger.appenderRef.stdout.ref = LogToConsole
After deep Research I have found one Jar as my solution
Log4j-jcl-2.16 jar.
After adding this to my build path I am getting all Logs printed in my file
-Dlog4j.configurationFile=/src/main/resources/log4j2.xml in setEnv.bat file solved my problem.

Upgrading log4j 1.2.14 to 2.12.1 Error No Root logger was configured and No log file created

We are upgrading log4j 1.2.14 to 2.12.1 and I have the log4j2.properties file as below
status = error
log4j2.appender.console.type = Console
log4j2.appender.console.name = LogToConsole
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#log4j2.appender.file.type = File
#log4j2.appender.file.name = LogToFile
#log4j2.appender.file.fileName=logs/app.log
#log4j2.appender.file.layout.type=PatternLayout
#log4j2.appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
log4j2.appender.rolling.type = RollingRandomAccessFile
log4j2.appender.rolling.name = LogToRollingRandomAccessFile
log4j2.appender.rolling.fileName = ${server.home}/logs/server.log
log4j2.appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
log4j2.appender.rolling.layout.type = PatternLayout
log4j2.appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
log4j2.appender.rolling.policies.type = Policies
log4j2.appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.size=10MB
log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy
log4j2.appender.rolling.strategy.max = 10
# Log to console and rolling file
#logger.app.type = AsyncLogger
log4j2.logger.app.name = com.ssl.server
log4j2.logger.app.level = info
log4j2.logger.app.additivity = false
log4j2.logger.app.appenderRef.rolling.ref = LogToRollingRandomAccessFile
log4j2.logger.app.appenderRef.console.ref = LogToConsole
log4j2.rootLogger.level = info
log4j2.rootLogger.appenderRef.stdout.ref = LogToConsole
I have below dependancies in my pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
I am initializing the logger as below
private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(SSLServer.class.getName());
When I start the standalone Java application using the main() am getting below error and server.log file is not created, I did some research regarding the below error and most of the articles were referring to log4j2.xml configuration and none of the config changes worked in my log4j2.properties
log warn: org.apache.logging.log4j.status.StatusLogger - No Root logger was configured, creating default ERROR-level Root logger with Console appender
No Root logger was configured, creating default ERROR-level Root logger with Console appender
Few things to look at:
make sure you don't have any other log4j2 config files on your classpath. The list is not small and can trip up. See it in the documentation here.
get debug info out of log4j2 by increasing status messages: status = info (debug or trace )
remove log4j2. as the prefix from all your properties so that log4j2.appender.console.type = Console becomes appender.console.type = Console
add file logging to your rootlogger: rootLogger.appenderRef.rolling.ref = LogToFile
I adapted your configuration to work. Please check this out:
# Print configuration errors only
status=error
# Console appender
appender.console.type=Console
appender.console.name=LogToConsole
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type=RollingRandomAccessFile
appender.rolling.name=LogToRollingRandomAccessFile
appender.rolling.fileName=${SERVER_HOME}/logs/server.log
appender.rolling.filePattern=logs/${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type=PatternLayout
appender.rolling.layout.pattern=%d %p %C{1.} [%t] %m%n
appender.rolling.policies.type=Policies
appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=1KB <-- for testing only!
appender.rolling.strategy.type=DefaultRolloverStrategy
appender.rolling.strategy.max=10
# Root logger
rootLogger.level=info
rootLogger.appenderRef.console.ref=LogToConsole
rootLogger.appenderRef.rolling.ref=LogToRollingRandomAccessFile
Please make sure you have the SERVER_HOME variable in your environment.
I don't see where you are declaring a root logger in you log4j.properties file.
I see, at the bottom, where you are setting the "level" on the rootlogger but I don't see where you've established the rootLogger.
My log4j.properties file start out like this:
log4j.rootLogger=WARN, FILE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %20t %25F:%L - %m%n
I think you need two modifications in your properties configuration:
Remove the prefix "log4j2."
Remove the space after "appender.rolling.type =
RollingRandomAccessFile "
Also check if your package name is correct:
logger.app.name = com.ssl.server
status = error
appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#appender.file.type = File
#appender.file.name = LogToFile
#appender.file.fileName=logs/app.log
#appender.file.layout.type=PatternLayout
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
# Rotate log file
appender.rolling.type = RollingRandomAccessFile
appender.rolling.name = LogToRollingRandomAccessFile
appender.rolling.fileName = logs/server.log
appender.rolling.fileName = ${server.home}/logs/server.log
appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10
# Log to console and rolling file
#logger.app.type = AsyncLogger
logger.app.name = com.ssl.server
logger.app.level = info
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingRandomAccessFile
logger.app.appenderRef.console.ref = LogToConsole
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole

Log4j2 does not create the logfile in Spring boot

I have a Spring boot app and I want to use LOG4J2 to manage my logs in different files, but the logfile isn't created.
I have a log4j2.properties configuration under "resources"
My log file configuration:
name=PropertiesConfig
#output folder
property.filename=G:\\PRUEBAS
property.packagename=com.package.main
appenders=console, infoLoggerAppender, commonLoggerAppender, errorLoggerAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#common appender
appender.commonLoggerAppender.type=File
appender.commonLoggerAppender.name=RollingFile
appender.commonLoggerAppender.fileName=${filename}\\log.log
appender.commonLoggerAppender.layout.type=PatternLayout
appender.commonLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#error appender
appender.errorLoggerAppender.type=RandomAccessFile
appender.errorLoggerAppender.name=RandomAccessFile
appender.errorLoggerAppender.fileName=${filename}\\error.log
appender.errorLoggerAppender.layout.type=PatternLayout
appender.errorLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log error levels.
appender.errorLoggerAppender.filter.threshold.type=LevelRangeFilter
appender.errorLoggerAppender.filter.threshold.minLevel=error
appender.errorLoggerAppender.filter.threshold.maxLevel=error
#info appender
appender.infoLoggerAppender.type=File
appender.infoLoggerAppender.name=LOGFILE
appender.infoLoggerAppender.fileName=${filename}\\info.log
appender.infoLoggerAppender.layout.type=PatternLayout
appender.infoLoggerAppender.layout.info=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log info levels.
appender.infoLoggerAppender.filter.threshold.type=LevelRangeFilter
appender.infoLoggerAppender.filter.threshold.minLevel=info
appender.infoLoggerAppender.filter.threshold.maxLevel=info
# creating only one logger, we can use this with multiple appenders.
loggers=fileLogger
# this is package name. This package and all of it's child packages will use this logger
logger.fileLogger.name=${packagename}
# logger base level
logger.fileLogger.level=debug
logger.fileLogger.appenderRefs=infoLoggerAppender, commonLoggerAppender, errorLoggerAppender
logger.fileLogger.appenderRef.infoLoggerAppender.ref=LOGFILE
logger.fileLogger.appenderRef.commonLoggerAppender.ref=RollingFile
logger.fileLogger.appenderRef.errorLoggerAppender.ref=RandomAccessFile
rootLogger.level=debug
rootLogger.appenderRefs=stdout
rootLogger.appenderRef.stdout.ref=STDOUT
What can I do? Someone can help me?
I waste my last two days with this...
Check the log4j2 jar is available in sprint boot application if not then add the maven dependencies
<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-log4j2</artifactId>
</dependency>
Create Log4j2.properties file and declare all the
Create that file inside the resource folder
name = ABC
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ABClog/ABCapplication.log
appender.rolling.filePattern = ABClog/ABCapplication.%d{dd-MMM}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l]- %msg%n
appender.rolling.policies.type = Policies
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath =log
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ABC*.log
appender.rolling.strategy.action.ifAny.type = IfAny
appender.rolling.strategy.action.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.ifAny.ifLastModified.age = 14d
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.exceeds = 200MB
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
logger.rolling.name = com.currentobject.controller
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
provide the path of log4j2.properties file into the application.properties file
logging.config=classpath:properties/log4j2.properties

How do I print IST in log file using log4j2?

I want to print IST in my log file using log4j2. Currently it is printing time in UTC. Below is my log4j2.properties file.
name = log4j2
appenders = console, file
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/home/dockerdev/code/stmtParser/log/fcu_analyzer.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = EnhancedPatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss}{IST} %-5p %c{1}:%L - %m%n
loggers=file
logger.file.name=/home/dockerdev/code/stmtParser/log4j2.properties
logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = file ,stdout
rootLogger.appenderRef.file.ref = LOGFILE
rootLogger.appenderRef.console.ref = STDOUT
This I am trying to run in local docker. Log generated from this shows UTC. How can i get IST ? what change is required?
Try change timezone inside your Dockerfile
I got the solution, since i was using log4j2 I had to only add {IST} after date pattern for file appender. So final properties file which would print IST in log file is
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/home/dockerdev/code/stmtParser/log/fcu_analyzer.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}{IST} [%t] %c{1} - %msg%n

Tomcat Save Log History

I am using log4j in webapp deployed on tomcat, but cuurently my logs comes under catalina.log file, but only error logs remain in history, i want my all logs to be remain in history , additionally it will be good if if i get logs datewise.
my logs4j looks like this
log4j.rootLogger=INFO, myConsoleAppender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern= [%t] %d{dd MMM yyyy HH:mm:ss,SSS} : %c %x : %m%n
i am only using info level of logs.
Try this
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
# Define the daily rolling file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/logs/app.log
log4j.appender.FILE.DatePattern='.'yyyy-MMM-dd
# Define the layout for the file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
Above will create a log file on the tomcat installation directory under the folder logson daily basis. You can change the log file path by modifying the log4j.appender.FILE.File=/path/to/logfile

Categories