遇到从YAML文件自动配置log4j 2的问题。

huangapple go评论64阅读模式
英文:

Trouble auto-configuring log4j 2 from YAML file

问题

这是我的应用程序代码。运行时,只有错误字符串被记录。我可以看到yaml文件被复制到build/resources文件夹中。尽管我遵循了命名约定并将yaml文件放在正确的位置,但我无法诊断为什么自动配置不起作用。

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.");
    }
}

我的build.gradle文件如下。

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'
}

我已将log4j2.yaml文件放在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

更新

我重新阅读了文档,他们提到JSON和YAML配置文件需要额外的依赖才能工作。我错过了这一点,因为我只看了初始段落和示例文件。
将Jackson Core和Databind依赖项添加到我的build.gradle文件中可以使配置生效。

  // 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'
英文:

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

Update

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'

答案1

得分: 2

Karanveer 在“更新”部分提供的解决方案对我帮助很大,谢谢(它需要添加 Jackson 依赖)。

但对于我来说,是 Jackson dataformat:

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.3'

以下是可能需要的不同 Log4J 配置的依赖描述:
Log4j 运行时依赖

英文:

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

答案2

得分: 1

你的日志记录器被配置为调试级别。第一条和第三条日志记录调用的级别是trace。Trace级别比debug更精细,因此这些不会被记录。这样只剩下了你的错误日志事件。由于默认情况下,你的应用程序使用的日志记录器的名称将为“App”,它将使用根日志记录器,从而将其路由到控制台。

英文:

Your loggers are configured at the debug level. The first and third logging calls are at trace level. Trace is more fine grained that debug so those won't be logged. That leaves only your error log event. Since by default the name for the Logger your application is using will be "App" it will use the root logger which will route it to the console.

huangapple
  • 本文由 发表于 2020年4月7日 23:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61083150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定