You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by David Killeffer <ra...@gmail.com> on 2008/01/03 21:41:18 UTC

Re: Is there a way to get all configured loggers?

Hi Nestor -
     I had to do something similar for a servlet I was writing recently - as
part of the servlet, I declare a logger which is then used to get all the
loggers that have been configured, and then show which appenders are
attached to which loggers.  The main function which I used to get all the
configured loggers and their appenders is like this:

    private void showLoggers(Logger mainLogger, PrintWriter out) {
        // get all the loggers
        Enumeration allLoggers = mainLogger.getLoggerRepository
().getRootLogger().getLoggerRepository().getCurrentLoggers();
        while (allLoggers.hasMoreElements()){
            Logger logger = (Logger)allLoggers.nextElement();
            // only show loggers that have appenders attached to them
            try {
                if ( logger.getAllAppenders().hasMoreElements() ) {
                    printLoggingSummary(logger, out);
                }
            } catch (Exception e) {
                System.out.println("in showLoggerst().... " +
e.getStackTrace() + "\nMessage: " + e.getMessage() );
            }
        }
    }

Hopefully this helps steer you in the right direction.

~ David

On Dec 31, 2007 3:25 PM, Néstor Boscán <ne...@tcs.com.ve> wrote:

> Hi
>
> Is there a way to get a list of all configured loggers?
>
> Regards,
>
> Néstor Boscán
>



-- 
Best Regards,
David Killeffer

RE: Is there a way to get all configured loggers?

Posted by Néstor Boscán <ne...@tcs.com.ve>.
What I did for this was to call logger.getLevel () if it returned not null
then this was a logger that I was interested in.

Regards,

Néstor Boscán 

-----Mensaje original-----
De: David Killeffer [mailto:rayden7@gmail.com] 
Enviado el: Jueves, 03 de Enero de 2008 04:14 p.m.
Para: Log4J Users List
Asunto: Re: Is there a way to get all configured loggers?

Just a point of clarification - the part of the code in the try block:

                if ( logger.getAllAppenders().hasMoreElements() ) {
                    printLoggingSummary(logger, out);
                }

was used because if you don't ensure that you're only looking at the loggers
that have appenders attached, you might get a list of a whole bunch of
loggers you don't care about (that was happening to me) - this way you're
only going to be calling the printLoggingSummary() function on loggers that
have appenders on them (presumably those are the ones you're interested in).


On Jan 3, 2008 3:41 PM, David Killeffer <ra...@gmail.com> wrote:

> Hi Nestor -
>      I had to do something similar for a servlet I was writing 
> recently - as part of the servlet, I declare a logger which is then 
> used to get all the loggers that have been configured, and then show 
> which appenders are attached to which loggers.  The main function 
> which I used to get all the configured loggers and their appenders is like
this:
>
>     private void showLoggers(Logger mainLogger, PrintWriter out) {
>         // get all the loggers
>         Enumeration allLoggers = 
> mainLogger.getLoggerRepository().getRootLogger().getLoggerRepository()
> .getCurrentLoggers();
>
>         while (allLoggers.hasMoreElements()){
>             Logger logger = (Logger)allLoggers.nextElement();
>             // only show loggers that have appenders attached to them
>             try {
>                 if ( logger.getAllAppenders().hasMoreElements() ) {
>                     printLoggingSummary(logger, out);
>                 }
>             } catch (Exception e) {
>                 System.out.println("in showLoggerst().... " +
> e.getStackTrace() + "\nMessage: " + e.getMessage() );
>             }
>         }
>     }
>
> Hopefully this helps steer you in the right direction.
>
> ~ David
>
>
> On Dec 31, 2007 3:25 PM, Néstor Boscán < nestor.boscan@tcs.com.ve> wrote:
>
> > Hi
> >
> > Is there a way to get a list of all configured loggers?
> >
> > Regards,
> >
> > Néstor Boscán
> >
>
>
>
> --
> Best Regards,
> David Killeffer




--
Best Regards,
David Killeffer


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Is there a way to get all configured loggers?

Posted by David Killeffer <ra...@gmail.com>.
Just a point of clarification - the part of the code in the try block:

                if ( logger.getAllAppenders().hasMoreElements() ) {
                    printLoggingSummary(logger, out);
                }

was used because if you don't ensure that you're only looking at the loggers
that have appenders attached, you might get a list of a whole bunch of
loggers you don't care about (that was happening to me) - this way you're
only going to be calling the printLoggingSummary() function on loggers that
have appenders on them (presumably those are the ones you're interested in).


On Jan 3, 2008 3:41 PM, David Killeffer <ra...@gmail.com> wrote:

> Hi Nestor -
>      I had to do something similar for a servlet I was writing recently -
> as part of the servlet, I declare a logger which is then used to get all the
> loggers that have been configured, and then show which appenders are
> attached to which loggers.  The main function which I used to get all the
> configured loggers and their appenders is like this:
>
>     private void showLoggers(Logger mainLogger, PrintWriter out) {
>         // get all the loggers
>         Enumeration allLoggers = mainLogger.getLoggerRepository().getRootLogger().getLoggerRepository().getCurrentLoggers();
>
>         while (allLoggers.hasMoreElements()){
>             Logger logger = (Logger)allLoggers.nextElement();
>             // only show loggers that have appenders attached to them
>             try {
>                 if ( logger.getAllAppenders().hasMoreElements() ) {
>                     printLoggingSummary(logger, out);
>                 }
>             } catch (Exception e) {
>                 System.out.println("in showLoggerst().... " +
> e.getStackTrace() + "\nMessage: " + e.getMessage() );
>             }
>         }
>     }
>
> Hopefully this helps steer you in the right direction.
>
> ~ David
>
>
> On Dec 31, 2007 3:25 PM, Néstor Boscán < nestor.boscan@tcs.com.ve> wrote:
>
> > Hi
> >
> > Is there a way to get a list of all configured loggers?
> >
> > Regards,
> >
> > Néstor Boscán
> >
>
>
>
> --
> Best Regards,
> David Killeffer




-- 
Best Regards,
David Killeffer

RE: Is there a way to get all configured loggers?

Posted by Néstor Boscán <ne...@tcs.com.ve>.
Thanks a lot!!! 

-----Mensaje original-----
De: David Killeffer [mailto:rayden7@gmail.com] 
Enviado el: Jueves, 03 de Enero de 2008 04:11 p.m.
Para: Log4J Users List
Asunto: Re: Is there a way to get all configured loggers?

Hi Nestor -
     I had to do something similar for a servlet I was writing recently - as
part of the servlet, I declare a logger which is then used to get all the
loggers that have been configured, and then show which appenders are
attached to which loggers.  The main function which I used to get all the
configured loggers and their appenders is like this:

    private void showLoggers(Logger mainLogger, PrintWriter out) {
        // get all the loggers
        Enumeration allLoggers = mainLogger.getLoggerRepository
().getRootLogger().getLoggerRepository().getCurrentLoggers();
        while (allLoggers.hasMoreElements()){
            Logger logger = (Logger)allLoggers.nextElement();
            // only show loggers that have appenders attached to them
            try {
                if ( logger.getAllAppenders().hasMoreElements() ) {
                    printLoggingSummary(logger, out);
                }
            } catch (Exception e) {
                System.out.println("in showLoggerst().... " +
e.getStackTrace() + "\nMessage: " + e.getMessage() );
            }
        }
    }

Hopefully this helps steer you in the right direction.

~ David

On Dec 31, 2007 3:25 PM, Néstor Boscán <ne...@tcs.com.ve> wrote:

> Hi
>
> Is there a way to get a list of all configured loggers?
>
> Regards,
>
> Néstor Boscán
>



--
Best Regards,
David Killeffer


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org