Logs printing in dependency library jar logs location in log4j2

Viewed 24

I am facing log issue in maven application in which I am using a dependency jar of another non maven application both are using log4j 2.18.0 version. But, all the logs of main applications are also printing in dependency jar logs location file after the call goes to dependency jar method.

I have mentioned below both the log4j2.properties file content:

Main application log4j2.properties:

status = warn

appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = C:/LOG/Main.log
appender.rolling.filePattern = C:/LOG/Main.log.%d{yyyy-MM-dd}
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.strategy.type = DefaultRolloverStrategy

# Log to console and rolling file
logger.app.name = com.indra
logger.app.level = debug
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole
logger.app.additivity=false

rootLogger.level = info
rootLogger.appenderRef.rolling.ref = LogToRollingFile

Dependency jar log4j2.properties:

status = warn

appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %5p | %d | %t | %F | %L | %m%n

# Rotate log file
appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = C:/LOG2/dependency.log
appender.rolling.filePattern = C:/LOG2/dependency.log.%d{yyyy-MM-dd}
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %5p | %d | %t | %F | %L | %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.strategy.type = DefaultRolloverStrategy

# Log to console and rolling file
logger.app.name = com.indra
logger.app.level = info
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole
logger.app.additivity=false

rootLogger.level = debug
rootLogger.appenderRef.rolling.ref = LogToRollingFile

Please help me with this!

1 Answers

All the loggers of an application use the same logger context with a single configuration (cf. architecture).

Since logging configuration is up to user of the application, not the developer, library jars should not contain any Log4j2 configuration file nor should they depend upon log4j-core (they should only depend upon log4j-api, cf. API separation).

Your application can contain a Log4j2 configuration and log4j-core to provide a default configuration to the application user. However the configuration should not use paths specific to your system.

Conventionally a loggers name is equal to the fully qualified name of the class that uses it, which allows you to easily split your library's and main application logs. If all the packages in your library start with com.indra.dependency you can use a single configuration similar to this:

appender.1.type = RollingFile
appender.1.name = main
...

appender.2.type = RollingFile
appender.2.name = dependency
...

logger.1.name = com.indra
logger.1.appenderRef.1.ref = main

logger.2.name = com.indra.dependency
logger.2.appenderRef.1.ref = dependency
logger.2.additivity = false
Related