This is my application code. When it is run, only the error string is logged. I can see the yaml file copied into the build/resources folder. I'm not able to diagnose why the auto-configuration isn't working even though I've followed the naming convention and placed the yaml file in the proper place.
public class App {
private static final Logger logger = LogManager.getLogger();
public static void main(String[] args) {
logger.trace("Entering application.");
logger.error("Some error");
logger.trace("Exiting application.");
}
}
My build.gradle looks like this.
plugins {
id 'java'
id 'application'
id 'groovy'
}
repositories {
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1'
implementation 'com.google.guava:guava:28.0-jre'
testImplementation 'org.codehaus.groovy:groovy-all:2.5.7'
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
testImplementation 'junit:junit:4.12'
}
application {
mainClassName = 'myapp.App'
}
I've put the log4j2.yaml file under src/main/resources.
Configuration:
status: warn
name: YAMLConfigTest
properties:
property:
name: filename
value: target/test-yaml.log
thresholdFilter:
level: debug
appenders:
Console:
name: STDOUT
PatternLayout:
Pattern: "%m%n"
File:
name: File
fileName: ${filename}
PatternLayout:
Pattern: "%d %p %C{1.} [%t] %m%n"
Filters:
ThresholdFilter:
level: error
Loggers:
logger:
-
name: org.apache.logging.log4j.test1
level: debug
additivity: false
ThreadContextMapFilter:
KeyValuePair:
key: test
value: 123
AppenderRef:
ref: STDOUT
-
name: org.apache.logging.log4j.test2
level: debug
additivity: false
AppenderRef:
ref: File
Root:
level: debug
AppenderRef:
ref: STDOUT
I re-read the docs and they mention that JSON and YAML config files need additional dependencies to work. I had missed it since I only looked at the initial paragraph and the sample file. Adding the Jackson Core and Databind dependencies to my build.gradle allowed the config to take effect.
// for JSON and YAML configs
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1'
Karanveer's solution in "Update" section helped me a lot, thanks (it needed to add Jackson dependency).
But for me it was Jackson dataformat:
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.3'
There is description of dependencies that might be required for different Log4J configurations: Log4j Runtime Dependencies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With