Log4j property file is ingored - java

For some reason, my log4j configuration file is getting ignored.
The configuration is file found in
C:\dev\pa-backup-restore-utility\BackupRestore\log4j2.properties
Here are the contents of the 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=logs/backupRestore.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=log4j2.properties
logger.file.level = error
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = error
rootLogger.appenderRefs=stdout,logfile
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.logfile.ref=LOGFILE
The batch file that runs the class is located in the same directory as the configuration file.
The command from the batch file is run as follows:
set javaPath="%appDir%\jre\bin\java.exe"
%javaPath% -Dapp.properties.path=windows.app.properties -Dlog4j.configurationFile=file:log4j2.properties -jar backupRestore-jar-with-dependencies.jar -b
The entry in pom.xml for this is:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
Here's how a logger.info is called:
static void printWarning(String message) {
logger.info("============== warning ===============================");
logger.info("== " + message);
logger.info("============== warning ===============================");
}
However, when I run the batch job, an info message gets displayed in the console:
[main] INFO com.sick.backup.util.Utils - ============== warning ===============================
[main] INFO com.sick.backup.util.Utils - == here is an example message
[main] INFO com.sick.backup.util.Utils - ============== warning ===============================
Also, a log file is no longer getting generated.
This was all working at what point. I'm trying to track down the version to make sure it matches, but the only change I've made is to upgrade the version of log4j-to-slf4j due to prevent a vulnerability.

Related

Log4j does literally nothing?

My configuration file is path of the class path. At least I thought it is. I placed the log4j.properties file in the resources folder and log4j does nothing with it. Even if I delete it, no error occurs.
As anyone can see I'm using maven
Content of LoggerTest:
package com.dersimi.stella.logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LoggerTest {
public static void main(String[] args) {
System.out.println("xxx");
Logger logger = LogManager.getLogger(LoggerTest.class);
logger.info("Hello this is an info message");
System.out.println("xxx");
}
}
Program output:
xxx
xxx
Content of log4j.properties:
log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
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,SSS} %-5p - %m%n
In pom.xml is nothing out of the ordinary, just the dependency org.apache.logging.log4j log4j-core 2.17.2, compiler source target is 16, no plugins
The main problem is that you are trying to use log4j (the first one) configurations for log4j2.
In first place make sure you have the following dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
</dependency>
Second, have a file named log4j2.properties with a content like this:
status = warn
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
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole
Reference: https://mkyong.com/logging/log4j2-properties-example/

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

log4j does not log for file

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).

Categories