Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

Different logging level for package in json config of Log4j2

How do I set different logging level for package in json config of Log4j2 ? Here is my config (minus definition of appenders)

{
  "configuration": {
    "name": "Default2",
    "loggers": {
      "io.netty": {
        "level": "info",
        "appender-ref": [{
          "ref": "Console"
        }]
      },
      "root": {
        "level": "debug",
        "appender-ref": [{
          "ref": "Console"
        }, {
          "ref": "File"
        }]
      }
    }
  }
}

it works with root logger but fails when I add io.netty with following error

2021-01-26 13:16:04,677 main ERROR Unable to locate plugin for io.netty
2021-01-26 13:16:18,132 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.config.LoggersPlugin for element loggers: java.lang.NullPointerException java.lang.NullPointerException
    at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:52)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:258)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:622)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:695)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:712)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:267)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:245)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:664)

What am I missing ? Maybe log4j2 doesn't understand wildcard setting ?

question from:https://stackoverflow.com/questions/65902171/different-logging-level-for-package-in-json-config-of-log4j2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Turned out package prefix should be specified in "name" property, no in the key of "loggers" element.

"loggers": {
  "logger": [
    {
      "name": "root",
      "level": "debug",
      "additivity": false,
      "appender-ref": [ { "ref": "Console" }, { "ref": "File" } ]
    },
    {
      "name": "io.netty",
      "level": "info",
      "additivity": false,
      "appender-ref": [ { "ref": "Console" }, { "ref": "File" } ]
    }
  ]
}

Also note that asterisk (io.netty.*) doesn't work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...