[logback-user] How to retrieve parent logger?

Christian Migowski chrismfwrd at gmail.com
Fri Nov 25 09:13:05 CET 2011


Ok, I looked into logback code and saw it is not possible to retrieve
the parent logger from a logger with the current code. Was there a
reason why this is not made possible? Would it be possible to add the
necessary

public Logger getParent() {
    return parent;
}

method in the next release?
Is there another way in logback to get the effective Appenders from a
given logger other then retrieving the appenders from all loggers and
then do name-string comparisons to find out who is whose parent?


best regards,
christian!



On Thu, Nov 24, 2011 at 2:37 PM, Christian Migowski
<chrismfwrd at gmail.com> wrote:
> Hi,
>
> how can I get the parent logger of a given logger in logback?
> What I want is to retrieve a list of all effective appenders on a
> given logger, I suppose therefore I need to call
> iteratorForAppenders() on the logger itself as well as on all parents,
> right?
> log4j had logger.getParent() but I cannot find something similar in logback.
>
>
> thanks in advance,
> best regards,
> christian!
>
>
> P.S. what I really want is to port this from log4j to logback, it gets
> a list of all logfiles attached to a given logger:
>
>        org.apache.log4j.Logger testlogger =
> org.apache.log4j.Logger.getLogger(log.getName());
>        ArrayList<String> recvdMsg = new ArrayList<String>();
>        while (testlogger != null && testlogger instanceof
> org.apache.log4j.Logger) {
>            Enumeration<Appender> loggers = testlogger.getAllAppenders();
>            while (loggers.hasMoreElements()) {
>                Appender x = loggers.nextElement();
>                if (x instanceof FileAppender) {
>                    String filename = ((FileAppender) x).getFile();
>                    if (filename != null) {
>                        File logfile = new File(filename);
>                        try {
>                            recvdMsg.add(logfile.getCanonicalPath());
>                        } catch (IOException e) {
>                            //so we don't add the path, thats enough handling
>                        }
>                    }
>                }
>            }
>            testlogger = (org.apache.log4j.Logger) testlogger.getParent();
>        }
>


More information about the Logback-user mailing list