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

4

5

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
Forayer answered 10/10, 2019 at 21:17 Comment(3)
Could you try getClass().getClassLoader().getResource("conf/log4j2.properties").getFile() ?Boxer
@Villat, Thanks for the response, as per the official log4j doc I see that by default it will look for log4j-test.properties if log4j.configurationFile is not provided, can you please check my log4j2.properties and see why its not creating the log fileForayer
Hey, the log4j2.properties file has priority over the log4j2.xml so, could you add your project structure to the post? Maybe a screenshot.Boxer
C
3

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
Conscription answered 14/10, 2019 at 15:44 Comment(0)
H
3

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.

Harris answered 16/10, 2019 at 12:24 Comment(0)
B
1

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
Brownfield answered 15/10, 2019 at 14:30 Comment(0)
C
1

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
Caprification answered 21/10, 2019 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.