New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SLF4j 311 - Enable swapping of NOPLogger in SubstituteLoggerFactory #58
Conversation
Hi Chetan, looking at the code, I can see how the delegate is useful in switching implementations. However, how and when is the switch made? Is it done inside SLF4J or were you thinking of doing the switch in user code, i.e. in Sling? |
The switch can be done in either place. User CodeFor example in Sling we do wait till A proper fix would probably require a change in Logback where such a task is done in org.slf4j.impl.StaticLoggerBinder#StaticLoggerBinder constructor. Slf4j CodeHere we can do such a change in org.slf4j.LoggerFactory#bind. So instead of emitting the substitute logger name list we fix the substituted loggers. If that is fine with you I can update the patch to include that. |
OK. I think it makes most sense to set the delegates of the SubstituteLoggers while in the LoggerFactory.bind method. The LoggerFactory.emitSubstituteLoggerWarning() seems like a good place (although the method would need to renamed). I'd keep the warning but change the message to indicate that a set of substitute loggers may have been accessed during the initialization phase and that logging calls during this phase were not honored but that calls in the future to those loggers will be honored. The http://slf4j.org/codes.html#substituteLogger would need to be changed as well. The source is plain old XML and is located in codes.html under the slf4j-site/src/site/pages/ folder. |
synchronized (loggerNameList) { | ||
loggerNameList.add(name); | ||
SubstitutableLogger logger; | ||
synchronized (loggers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you use ConcurrentMap, there is no need to use synchronized block.
See https://github.com/qos-ch/slf4j/blob/master/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerFactory.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Refactored as per suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other LoggerFactory impls for a better way of using the concurrent map.
SLF4j 311 - Enable swapping of NOPLogger in SubstituteLoggerFactory
Changed the implementation of
SubstituteLoggerFactory
to return aSubstitutableLogger
which can delegate to a different logger implementation. By default it usesNOPLogger
but enables changing the delegate.An application can later find out the list of
SubstitutableLogger
and replace the delegate with right implementation. An application must also clear the SubstitutableLogger map to prevent any possible memory leaksFix for http://bugzilla.slf4j.org/show_bug.cgi?id=311