how to log only one level with log4j2?

后端 未结 3 663
日久生厌
日久生厌 2020-12-10 07:51

I\'m using log4j2 in my application.

What I want is everything up to \'debug\' to go to console, everything up to \'info\' to go to myapp.log, and ONLY \'info\' to g

3条回答
  •  [愿得一人]
    2020-12-10 08:48

    You can use LevelRangeFilter. It has minLevel and maxLevel properties(and onMatch and onMismatch, ofcourse, too).

    For example(in json) we need to print log in console only on info and warn levels:

    "Appenders": {
      "Console": {
        "PatternLayout": {
          "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
        },
        "name": "Console",
        "target": "SYSTEM_OUT",
        "LevelRangeFilter": {
          "minLevel": "warn",
          "maxLevel": "info",
          "onMatch": "ACCEPT",
          "onMismatch": "DENY"
        }
      }
    }
    

    And if you want only info level then write "info" to both properties min and max.

提交回复
热议问题