Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback logger name with wildcard

Is it possible to use a wildcard for the logger name which matches different packages so we don't have to specify them all individually? So instead of writing

<logger name="com.package1.web" level="debug"   
<logger name="com.package2.web" level="debug"  
<logger name="com.package3.web" level="debug"  

I would like to only specify one entry somewhat like this:

<logger name="com.*.web" level="debug"  

If anyone knows about a way or also just that it's not a possibility i'd highly appreciate it

like image 614
petrichory Avatar asked Feb 07 '18 06:02

petrichory


People also ask

What is logger name in Logback xml?

Logback Architecture A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.

How do I enable debug logs in Logback?

You can now use -Dlogback. debug=true to enable debugging of the logback setup. Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback.

What is root tag in Logback?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

Does Logback use Log4j?

Logback uses the same concepts as Log4j. So it's no surprise that even if they are using different file formats, their configurations are very similar. The following code snippet shows the same configuration as I used with Log4j.


1 Answers

TL;DR

Logback does not support wildcards at intermediate levels in the logger name.

Detail

Logback (implicitly) supports wildcards at the end of the logger name, so <logger name="com.package1.web" ...> effectively means:

  • Classes in com.package1.web and in any sub packages of com.package1.web

Logback does this by creating a hierarchy of loggers; the logger for com.package1.web is parented by a logger for com.package1 which is parented by a logger for com which is parent by the ROOT logger.

So, if you declare <logger name="com.package1.web" level="debug"> and then attempt to emit a debug log message for a logger on com.package1.web.foo.bar Logback will walk up that logger's hierarchy until it finds a logger for which the DEBUG level is enabled, it will find this at com.package1.web and hence it will emit the DEBUG log event.

However, Logback will not create a hierarchy based on a wilcard at an intermediate level in the logger name. So, this ...

<logger name="com.*.web" level="debug">

... will not cause Logback to create loggers for:

  • com.package1.web
  • com.package2.web
  • com.package3.web

Logback's hierarchy behaviour will not be applied when the wildcard is presented at an intermediate level in the logger name.

Possible Solution

One benefit of this hierarchy behaviour is that it allows you to apply logger configuration to a package and all classes below that package i.e. it creates an association between logger instances based on their parentage. You could make this association by providing an explicit logger name, rather than defaulting it to the current class name.

For example:

<logger name="DEBUG_LOGGER" level="debug">

Then everywhere you want to use the debug logger just create a Logger instance like so:

private final Logger logger = LoggerFactory.getLogger("DEBUG_LOGGER");

Clearly, there are drawback to this approach too, I'm just mentioning it here to show that there is another way (other than fully qualified class name) to associate logger instances and apply a level to them.

like image 196
glytching Avatar answered Sep 21 '22 13:09

glytching