Log4j2 does not create the logfile in Spring boot - java

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

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/

Why log4j won't create file?

i am trying to create a .log file for my app.
logs are going out to the console, but not on file.
Here is my .properties file:
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/Users/user/Downloads/demo/src/main/resourceslogigng.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
This is the maven dependency:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
and this is the injection in the #RestController class:
//SFL4J
Logger logger = LoggerFactory.getLogger(UserController.class);
User user = new ObjectMapper().readValue(message, User.class);
logger.info("New message read successfully!");

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

Log4j does not create the logfile

I want to use the unusuable log4J for logfiles in a java maven application. But the logfile is not created.
This is my a part of my .pom file:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
This is my log4j.properties file (under src/main/resources):
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:\\Test\\logfile.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
# 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
In my main method:
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
The log messages are in the console log, but the logfile has not been created. What am I doing wrong?
your pom.xml is not correct. it has to be <dependency> in the first line. (missing >)
Apart from that, your log4j.properties should do the work, I tried it and it writes a log-file.
How do you create the Logger instance log?
Do you have writing access to E:\\Test\\logfile.log?

log4j: Doesn't reflect settings in log4j.properties

I have an Java-SE app with log4j over slf4j.
At some phase of development, it stopped logging DEBUG level, and I can't figure out why.
I already excluded the causes I encountered earlier:
No WARNings
slf4j implementation used is log4j (AFAICT)
log4j.properties at right place (classpath root)
log4j.properties does end up in the .jar
The structure of the app didn't change
No log4j.properties or log4j.xml hidden in some .jar (like Weld-se-core has)
log4j.category. entries match (they didn't change since it worked)
Any ideas?
log4j.rootLogger=DEBUG, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %-5r %-5p [%c] (%t:%x) %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=JawaBot.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t] %c %m%n
log4j.category.org = DEBUG
log4j.category.org.jboss.jawabot = DEBUG
log4j.category.org.jboss.jawabot.irc = DEBUG
log4j.category.org.mortbay.log = WARN
log4j.category.org.apache.wicket = INFO
log4j.category.org.jboss.weld = DEBUG
#log4j.category.org.jboss.weld.bootstrap = DEBUG
log4j.category.org.jboss.weld.environment.se.jpa = DEBUG

Categories