qos-ch / slf4j

Simple Logging Facade for Java
http://www.slf4j.org
MIT License
2.34k stars 990 forks source link

Makes `SubstituteLogger` location aware #438

Open ppkarwasz opened 1 month ago

ppkarwasz commented 1 month ago

Currently, SubstituteLogger neither supports location information from upstream callers nor passes location information to the delegated logger.

This PR:

This is related to #425.

ceki commented 1 month ago

@ppkarwasz Thank you for this PR.

For the moment, I fail to see the circumstances under which the LocationAwareLogger.log() method may be called. Still investigating though.

ppkarwasz commented 1 month ago

For the moment, I fail to see the circumstances under which the LocationAwareLogger.log() method may be called. Still investigating though.

I am not entirely sure when SubstituteLogger is used (or if it is used at all these days), but I assume that it could appear in OSGi environments if:

  1. The SLF4J bundle is activated first (or used by another bundle without being activated).
  2. The Logback bundle is activated only later.

Since LocationAwareLogger is the oldest available mechanism to transfer caller boundary information, it is commonly used in API-to-API bridges, e.g. the Log4j API-to-SLF4J bridge or Spring JCL-to-SLF4J bridge.

Newer mechanisms such as LoggingEventAware (for logger implementations) or CallerBoundaryAware (for logger builder implementations) are less common. The default LoggingEventBuilder also requires the underlying logger to either implement LocationAwareLogger or LoggingEventAware:

https://github.com/qos-ch/slf4j/blob/69c333de280100f7dc99ee00e302192690fcc761/slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java#L157-L169