I add an appender programmatically. It was working until I added maven-shade-plugin
. I wonder what makes my appender fail.
The appender works ✅ or not ❌ in these scenarios:
- ✅ From IDE (IntelliJ IDEA)
- ❌ With the shade (Uber/fat) jar <-- now works ✅ see answer
- ✅ With separate jars (app jar, log4j jars)
- ❌ With the app jar, and the log4j jars unzipped into a folder
- ✅ With the app jar, and the log4j folder re-zipped
Sample project
- https://github.com/fmaylinch/log4j2-thread-context
- Here's the point where I add the custom appender: ThreadContextFileLogHandler.java#L79
- When the appender fails, it's because it doesn't receive the events here: ThreadContextFileLogHandler.java#L156
Reproduce scenarios with the sample project above
mvn clean compile
mkdir -p local/log4j-jars
unzip $HOME/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar -d local/log4j-jars
unzip -o $HOME/.m2/repository/org/apache/logging/log4j/log4j-core/2.17.2/log4j-core-2.17.2.jar -d local/log4j-jars
cd local/log4j-jars
zip -r ../log4j-jars.zip .
cd ../..
# Scenario 2 ❌ uses fat jar
java -cp "target/log4j-test-1.0-SNAPSHOT.jar" org.example.Main
# Scenario 3 ✅ uses separate jars
java -cp "target/original-log4j-test-1.0-SNAPSHOT.jar:$HOME/.m2/repository/org/apache/logging/log4j/log4j-core/2.17.2/log4j-core-2.17.2.jar:$HOME/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar" org.example.Main
# Scenario 4 ❌ uses log4j files unzipped
java -cp "target/original-log4j-test-1.0-SNAPSHOT.jar:local/log4j-jars" org.example.Main
# Scenario 5 ✅ uses log4j files re-zipped
java -cp "target/original-log4j-test-1.0-SNAPSHOT.jar:local/log4j-jars.zip" org.example.Main
Extra notes
In the scenario 5, I have noticed that I can remove some files in META-INF
, but for my appender to work, I need to keep the following:
META-INF
org
(containsLog4j2Plugins.dat
)services
(without this, the app even crashes)versions
MANIFES.MF
Related questions
- log4j2 JsonTemplateLayout not working with maven shade plugin - this seems to be a problem with
Log4j2Plugins.dat
, but in my project there's only one file, inlog4j2-core
. - is there any diff between run a java program with jar or with a package unpacked? - according to my problem, there IS a difference (maybe the
META-INF
folder is not processed like in a jar)