Log4J2 Yaml包级别

时间:2018-03-02 15:56:07

标签: java logging yaml packages log4j2

我正在尝试使用log4j2和yaml进行日志记录并且一直在努力。

包com.eclipse非常嘈杂,所以我只想在警告或以上时记录来自该包的消息。对于我的软件包,我想将调试及以上内容记录到滚动appender,并将错误或更高版本记录到错误日志中。在控制台上我想看看除了org.ecplise

之外的所有内容的调试

使用我当前的配置,我没有看到好的org.eclipse调试消息,我在控制台上看到了com.myorganization消息,但文件追加器似乎没有工作。如果我更改了最后两个记录器的顺序,那么org.eclipse消息会返回到控制台,但我的包的文件追加器开始工作。任何帮助将不胜感激。

这是我当前的配置:

Configuration:
  name: Default

  Properties:
    Property:
      name: log-path
      value: "logs"

  Appenders:

    Console:
      PatternLayout:
        pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'
      name: Console
      target: SYSTEM_OUT

    File:
      name: File_Appender
      fileName: ${log-path}/minimal-ws.error.log
      PatternLayout:
        pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'

    RollingFile:
      - name: RollingFile_Appender
        fileName: ${log-path}/minimal-ws.log
        filePattern: 'logs/archive/minimal-ws.log.%d{yyyy-MM-dd-hh-mm}.gz'
        PatternLayout:
          pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'
        Policies:
          SizeBasedTriggeringPolicy:
            size: 10 MB
        DefaultRollOverStrategy:
          max: 10

  Loggers:

    Root:
      AppenderRef:
      - ref: Console
      level: DEBUG

    Logger:
      - name: com.myorganization
        level: DEBUG
        AppenderRef:
          - ref: File_Appender
            level: ERROR
          - ref: RollingFile_Appender
            level: DEBUG

    Logger:
      - name: org.eclipse
        level: DEBUG
        additivity: false
        AppenderRef:
        - ref: Console
          level: WARN

1 个答案:

答案 0 :(得分:1)

您无需在Logger:中定义多个yaml定义。而是具有多个Logger:属性的单个- name:定义。以下是根据您的要求的工作解决方案。 [请相应更改包裹名称。]

Configuration:
  name: Default

  Properties:
    Property:
      name: log-path
      value: "."

  Appenders:

    Console:
      PatternLayout:
        pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'
      name: Console
      target: SYSTEM_OUT

    File:
      name: File_Appender
      fileName: ${log-path}/minimal-ws.error.log
      PatternLayout:
        pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'

    RollingFile:
      - name: RollingFile_Appender
        fileName: ${log-path}/minimal-ws.log
        filePattern: 'logs/archive/minimal-ws.log.%d{yyyy-MM-dd-hh-mm}.gz'
        PatternLayout:
          pattern: '%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n'
        Policies:
          SizeBasedTriggeringPolicy:
            size: 10 MB
        DefaultRollOverStrategy:
          max: 10

  Loggers:
    Root:
      AppenderRef:
      - ref: Console
        level: ERROR

    Logger:
      - name: eclipse
        level: DEBUG
        additivity: false
        AppenderRef:
        - ref: Console
          level: DEBUG
        - ref: File_Appender
          level: DEBUG
      - name: com
        level: DEBUG
        additivity: false
        AppenderRef:
          - ref: Console
            level: INFO
          - ref: File_Appender
            level: ERROR
          - ref: RollingFile_Appender
            level: DEBUG

此配置在ConsoleFile_AppenderRollingFile_Appender

中写入