3

So I have the right implementation and it's getting called just fine, but there is no method LoggerFactory#getContext(String name) ..so how can it make any decisions if I can't pass anything in on which to make decisions upon?

There is a ContextSelector#getLoggerContext(String name) but I dont have access to it?

So my point is: How can I make any sort of "selection" when I can't pass things into my selector or have any sort of access to the selector?

I need to have multiple contexts (with different configurations loaded obviously) from which I select at runtime according to my own logic.

I have found a solution to simply create LoggerContexts and configure them, but I just don't understand the point of this alternative mechanism then.

Blub
  • 13,014
  • 18
  • 75
  • 102

1 Answers1

0

ContextSelector is a Logback construct and not part of Slf4j API. Hence you do not find any context related method on the LoggerFactory

but there is no method LoggerFactory#getContext(String name) ..so how can it make any decisions if I can't pass anything in on which to make decisions upon

The context is determined based on some thread local information. Have a look at ContextJNDISelector which determines the right context based on JNDI. Now when say in a servlet which is part of a webapp invokes LoggerFactory.getLogger and JNDI ContextSelector is enabled then Logback would determine the JNDI conext of the thread and from that LoggerContext name would be determined. Apart from some Thread Local I do not see a way LoggerFactory can determine the context.

I have found a solution to simply create LoggerContexts and configure them, but I just don't understand the point of this alternative mechanism then.

if your context selection logic can work at entry level to your app (for eg servlet filters in case of webapp) then you can have a custom ContextSelector which looks from a ThreadLocal and have your filter setup the right context within that threadlocal

Chetan
  • 705
  • 5
  • 10
  • If one of you have some hands-on experience using logger contexts and context selectors independent of JNDI, maybe you could give me some insight over at http://stackoverflow.com/q/38208617/421049 . Thanks. – Garret Wilson Jul 05 '16 at 18:11