0

So I have a main spring-boot project that has 2 other projects (not spring) as dependency.

All these 3 project's have LOG4J implemented with a log4j.properties file on the resources file. They also have these dependencies on the pom.xml:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.14.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.0</version>
</dependency>

But for some reason, only the logs on the main project seem to read the properties file, because of the layout configuration that I defined.

Main project properties file:

log4j.logger.com.example.MainProject=DEBUG, consoleAppender, FILE

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[MainProject] - %-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %C:%M:%L - %m%n

## File Appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/opt/app/log/MainProject.log
log4j.appender.FILE.DatePattern='_'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.FILE.layout.ConversionPattern=[MainProject] - %-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %C:%M:%L - %m%n

log4j.additivity.com.example.MainProject=false

Project B properties file:

log4j.logger.com.example.ProjectB=DEBUG, stdout, consoleAppender

# Redirect log messages to console
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=[ProjectB] - %-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %C:%M:%L - %m%n

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[ProjectB] - %-5p - %d{yyyy-MM-dd HH:mm:ss.SSS} - %C:%M:%L - %m%n

The other project its exactly the same has the example of ProjectB

On the logs, I can confirm that the logs inside of the Main Project are printed with the right layout:

[MainProject] - DEBUG - 2021-09-15 13:37:51.157 - com.example.MainProject.MainProjecApplication:start:35 - Starting Server...
[MainProject] - INFO  - 2021-09-15 13:37:52.241 - com.example.MainProject.MainProjecApplication:start:46 - Server Running!

But on the logs inside of the Project B that is a dependency of the Main Project, they wont print with the right layout. Instead they are printed like this:

0 [main] INFO com.example.ProjectB.Stan  - Connecting to NATS Server...
181 [main] INFO com.example.ProjectB.Stan  - Connected to NATS SUCCESSFULLY.

That said, it seems to me that the dependecy projects are not reading their own log4j properties file. I've searched but I can't find a solution to this problem...

Thank you all in advance! :)

1 Answers1

1

When it comes to log4j only one properties file is used (see Multiple Log4j.properties files in classpath and How to initialize log4j properly?) meaning that properties files defined for project A and B will not be taken into consideration as it looks that in your scenario the log properties file for the main project is the first one in the classpath .

Piotr Michalczyk
  • 249
  • 1
  • 13