How do I get a list of configured loggers?

How do I get a list of configured loggers (In logback.xml) programmatically? e.g. I have the following loggers configured in my logback.xml file: ... <logger name="org.springframework.security"> <level value="DEBUG"/> </logger> <logger name="org.apache.struts"> <level value="INFO"/> </logger> ... How do I get those loggers? (E.g. I like to dynamically inspect and change my setup loggers at runtime) I was able to do this easily in Log4J but I seem to have issues doing so in logback. I thought I could do: LoggerContext lc = ((ch.qos.logback.classic.Logger)LOGGER).getLoggerContext(); List<String> strList = new ArrayList<String>(); Iterator<ch.qos.logback.classic.Logger> it = lc.getLoggerList().iterator(); while(it.hasNext()) { Logger log = it.next(); strList.add(log.getName()); } But this basically adds hundreds of loggers but I only want the ones I have configured in my logback.xml file. What do I miss here? Thanks a lot! Gunnar -- View this message in context: http://www.nabble.com/How-do-I-get-a-list-of-configured-loggers--tp20433216p... Sent from the Logback User mailing list archive at Nabble.com.

I thing you should check if the level is != null, i.e. if (log.getLevel() != null) { strList.add(log.getName()); } cheers jl On Tue, Nov 11, 2008 at 05:07, Gunnar Hillert <gunnar@hillert.com> wrote:
How do I get a list of configured loggers (In logback.xml) programmatically?
e.g. I have the following loggers configured in my logback.xml file:
... <logger name="org.springframework.security"> <level value="DEBUG"/> </logger> <logger name="org.apache.struts"> <level value="INFO"/> </logger> ...
How do I get those loggers? (E.g. I like to dynamically inspect and change my setup loggers at runtime)
I was able to do this easily in Log4J but I seem to have issues doing so in logback.
I thought I could do:
LoggerContext lc = ((ch.qos.logback.classic.Logger)LOGGER).getLoggerContext(); List<String> strList = new ArrayList<String>(); Iterator<ch.qos.logback.classic.Logger> it = lc.getLoggerList().iterator(); while(it.hasNext()) { Logger log = it.next(); strList.add(log.getName()); }
But this basically adds hundreds of loggers but I only want the ones I have configured in my logback.xml file.
What do I miss here?
Thanks a lot!
Gunnar
-- View this message in context: http://www.nabble.com/How-do-I-get-a-list-of-configured-loggers--tp20433216p... Sent from the Logback User mailing list archive at Nabble.com.
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

Hi, I'm using weblogic 10, and deployed my application as EAR. EAR Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.mycompany.CustomeAppender at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45) at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:23) at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:50) at ... 51 common frames omitted Caused by: java.lang.ClassNotFoundException: com.mycompany.CustomeAppender at at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at at java.security.AccessController.doPrivileged(Native Method) at at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

Hi, I'm using weblogic 10, and deployed my application as EAR. EAR Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.mycompany.CustomeAppender at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45) at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:23) at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:50) at ... 51 common frames omitted Caused by: java.lang.ClassNotFoundException: com.mycompany.CustomeAppender at at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at at java.security.AccessController.doPrivileged(Native Method) at at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

Hi, I'm using weblogic 10, and deployed my application as EAR. EAR Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.mycompany.CustomeAppender at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45) at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:23) at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:50) at ... 51 common frames omitted Caused by: java.lang.ClassNotFoundException: com.mycompany.CustomeAppender at at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at at java.security.AccessController.doPrivileged(Native Method) at at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

Hi, I'm using weblogic 10, and deployed my application as EAR. EAR Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.mycompany.CustomeAppender at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45) at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:23) at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:50) at ... 51 common frames omitted Caused by: java.lang.ClassNotFoundException: com.mycompany.CustomeAppender at at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at at java.security.AccessController.doPrivileged(Native Method) at at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

Hi, I'm using weblogic 10, and deployed my application as EAR. EAR Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.mycompany.CustomeAppender at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45) at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:23) at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:50) at ... 51 common frames omitted Caused by: java.lang.ClassNotFoundException: com.mycompany.CustomeAppender at at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at at java.security.AccessController.doPrivileged(Native Method) at at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

Hello Gunnar, Logback and log4j are similar, albeit not identical, in the way they store loggers. So I can't imagine why in one case you would get only the loggers you configured and in the other "hundreds" of loggers, unless you search for the loggers at different times during life cycle of your application. More precisely, in the log4j case you were probably iterating on the loggers just after log4j was configured and in the logback case later in the application life cycle. Was that the case? Anyway, in logback, you can filter out non-configured loggers with the following code: List<String> findNamesOfConfiguredAppenders() { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); List<String> strList = new ArrayList<String>(); for (ch.qos.logback.classic.Logger log : lc.getLoggerList()) { if(log.getLevel() != null || hasAppenders(log)) { strList.add(log.getName()); } } return strList; } boolean hasAppenders(ch.qos.logback.classic.Logger logger) { Iterator<Appender<LoggingEvent>> it = logger.iteratorForAppenders(); return it.hasNext(); } HTH, Gunnar Hillert wrote:
How do I get a list of configured loggers (In logback.xml) programmatically?
e.g. I have the following loggers configured in my logback.xml file:
... <logger name="org.springframework.security"> <level value="DEBUG"/> </logger> <logger name="org.apache.struts"> <level value="INFO"/> </logger> ...
How do I get those loggers? (E.g. I like to dynamically inspect and change my setup loggers at runtime)
I was able to do this easily in Log4J but I seem to have issues doing so in logback.
I thought I could do:
LoggerContext lc = ((ch.qos.logback.classic.Logger)LOGGER).getLoggerContext(); List<String> strList = new ArrayList<String>(); Iterator<ch.qos.logback.classic.Logger> it = lc.getLoggerList().iterator(); while(it.hasNext()) { Logger log = it.next(); strList.add(log.getName()); }
But this basically adds hundreds of loggers but I only want the ones I have configured in my logback.xml file.
What do I miss here?
Thanks a lot!
Gunnar
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch

Hello Ceki, Thank you very much for the very helpful response! Turns out I was almost there; just 'overlooked' the if statement for checking whether the logger has a configured appender. Also, just went through the exercise of converting my Maven project over to use SLF4J and logback. It has been a very easy and pleasant process! http://hillert.blogspot.com/2008/11/migrating-from-jcllog4j-to-slf4jlogback.... http://hillert.blogspot.com/2008/11/migrating-from-jcllog4j-to-slf4jlogback.... Thanks a lot! Cheers, Gunnar Ceki Gulcu-2 wrote:
Hello Gunnar,
Logback and log4j are similar, albeit not identical, in the way they store loggers. So I can't imagine why in one case you would get only the loggers you configured and in the other "hundreds" of loggers, unless you search for the loggers at different times during life cycle of your application. More precisely, in the log4j case you were probably iterating on the loggers just after log4j was configured and in the logback case later in the application life cycle. Was that the case?
Anyway, in logback, you can filter out non-configured loggers with the following code:
List<String> findNamesOfConfiguredAppenders() { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); List<String> strList = new ArrayList<String>(); for (ch.qos.logback.classic.Logger log : lc.getLoggerList()) { if(log.getLevel() != null || hasAppenders(log)) { strList.add(log.getName()); } } return strList; }
boolean hasAppenders(ch.qos.logback.classic.Logger logger) { Iterator<Appender<LoggingEvent>> it = logger.iteratorForAppenders(); return it.hasNext(); }
HTH,
Gunnar Hillert wrote:
How do I get a list of configured loggers (In logback.xml) programmatically?
e.g. I have the following loggers configured in my logback.xml file:
... <logger name="org.springframework.security"> <level value="DEBUG"/> </logger> <logger name="org.apache.struts"> <level value="INFO"/> </logger> ...
How do I get those loggers? (E.g. I like to dynamically inspect and change my setup loggers at runtime)
I was able to do this easily in Log4J but I seem to have issues doing so in logback.
I thought I could do:
LoggerContext lc = ((ch.qos.logback.classic.Logger)LOGGER).getLoggerContext(); List<String> strList = new ArrayList<String>(); Iterator<ch.qos.logback.classic.Logger> it = lc.getLoggerList().iterator(); while(it.hasNext()) { Logger log = it.next(); strList.add(log.getName()); }
But this basically adds hundreds of loggers but I only want the ones I have configured in my logback.xml file.
What do I miss here?
Thanks a lot!
Gunnar
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
-- View this message in context: http://www.nabble.com/How-do-I-get-a-list-of-configured-loggers--tp20433216p... Sent from the Logback User mailing list archive at Nabble.com.
participants (4)
-
Ceki Gulcu
-
Gunnar Hillert
-
Jean-Luc Geering
-
Jenny