Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

log4j

log4j2

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
like image 981
RanPaul Avatar asked Oct 10 '19 21:10

RanPaul


People also ask

What is root logger in Log4j2?

This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.

What is difference between log4j and Log4j2?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.


3 Answers

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
like image 156
diginoise Avatar answered Oct 24 '22 16:10

diginoise


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.

like image 2
Sergey Nemchinov Avatar answered Oct 24 '22 15:10

Sergey Nemchinov


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
like image 1
djhallx Avatar answered Oct 24 '22 16:10

djhallx