Skip to content
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

Merged
merged 5 commits into from Feb 3, 2014

Conversation

chetanmeh
Copy link

Changed the implementation of SubstituteLoggerFactory to return a SubstitutableLogger which can delegate to a different logger implementation. By default it uses NOPLogger 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 leaks

Fix for http://bugzilla.slf4j.org/show_bug.cgi?id=311

@ceki
Copy link
Member

ceki commented Feb 3, 2014

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?

@chetanmeh
Copy link
Author

The switch can be done in either place.

User Code

For example in Sling we do wait till LoggerFactory.getILoggerFactory() returns an instance of LoggerContext. So post that we know that initialization phase is done and then logic can check back the list of SubstitutedLoggers from SubstituteLoggerFactory and swap out the delegates.

A proper fix would probably require a change in Logback where such a task is done in org.slf4j.impl.StaticLoggerBinder#StaticLoggerBinder constructor.

Slf4j Code

Here 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.

@ceki
Copy link
Member

ceki commented Feb 3, 2014

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) {
Copy link
Contributor

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

Copy link
Author

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

Copy link
Member

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.

ceki added a commit that referenced this pull request Feb 3, 2014
SLF4j 311 - Enable swapping of NOPLogger in SubstituteLoggerFactory
@ceki ceki merged commit 8b3cc12 into qos-ch:master Feb 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants