logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bender Heri" <HBen...@Ergonomics.ch>
Subject RE: Get all appenders configured in log4j - Possible?
Date Tue, 04 Jul 2006 11:33:00 GMT
    /**
     * Finds the next appender with the given name within the hierarchie which is
     * defined by the given Logger, incl. RootLogger
     * <p>
     * @param aLog the logger whose appender should be found.
     * @param aAppenderName the name of the desired appender
     * @return an array with two entries: entry[0] is the Logger to which the found
     *         appender is attached, entry[1] is the found Appender
     * @throws Exception if no appender could be found
     */
    public static Object[] getNextAppenderInHierarchie( Logger aLog, String aAppenderName
)
    throws Exception
    {
        Logger log = aLog;
        String name = aLog.getName();
        Appender a = null;
        
        while ( ( a = log.getAppender( aAppenderName ) ) == null )
        {
            int i = name.lastIndexOf( '.' );
            
            if ( i == -1 )
            {
                // the entire hierarchy was checked. Try also the root logger:
                log = Logger.getRootLogger();
                a = log.getAppender( aAppenderName );
                
                if ( a == null )
                {
                    throw new Exception( new StringBuffer( "appender " )
                                         .append( aAppenderName )
                                         .append( " not found for logger " )
                                         .append( aLog.getName() ).toString() );
                } // if a == null
                
                break;
            } // if name.length() == 0
            
            name = name.substring( 0, i );
            
            log = Logger.getLogger( name );
        } // while ( ( a = aLog.getAppender( "DB_APPENDER" ) ) == null )
        
        Object[] result = new Object[2];
        result[0] = log;
        result[1] = a;
        
        return result;
        
    }
    
With this method you can only find the appender CONSOLE because you didn't attacht the appender
FILE to any logger.

Heri


> -----Original Message-----
> From: jaikiran pai [mailto:jai_forums2005@yahoo.co.in]
> Sent: Tuesday, July 04, 2006 12:03 PM
> To: log4j-user@logging.apache.org
> Subject: Get all appenders configured in log4j - Possible?
> 
> 
> Is there some way (API), through which i can get a collection 
> of appenders that have been mentioned(configured) through 
> log4j config files? Consider the following log4j.xml:
>    
>   ----------------------------------------------------------------
>   <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
> <log4j:configuration 
> xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
>   
>    <appender name="FILE" 
> class="org.jboss.logging.appender.RollingFileAppender">
>      <errorHandler 
> class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
>      <param name="File" 
> value="${jboss.server.home.dir}/log/server.log"/>
>      <layout class="org.apache.log4j.PatternLayout">
>        <param name="ConversionPattern" value="%d{ISO8601} 
> [%X{TAPUSERNAME}] %-5p [%c] %m%n"/>
>      </layout>     
>    </appender>
>      <appender name="CONSOLE" 
> class="org.apache.log4j.ConsoleAppender">
>          <errorHandler 
> class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
>          <param name="Target" value="System.out"/>
>          <param name="Threshold" value="INFO"/>
>          <layout class="org.apache.log4j.PatternLayout">
>             <!-- The default pattern: Date Priority 
> [Category] Message\n -->
>           <param name="ConversionPattern" value="%d{ISO8601} 
> [%X{TAPUSERNAME}] %-5p [%c{1}] %m%n"/>
>          </layout>
>    </appender>
>    
>    <root>
>          <appender-ref ref="CONSOLE"/>
>          
>       </root>
>    
> </log4j:configuration>
>    
>   ----------------------------------------------------------------
>    
>   Can i get a collection containing the instances of appender 
> named "FILE" and "CONSOLE" in the above case?
>    
>   regards,
>   -Jaikiran
>  
> 
>  				
> ---------------------------------
>  Yahoo! India Answers: Share what you know. Learn something 
> new Click here
> Catch all the FIFA World Cup 2006 action on Yahoo! India Click here
> 

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


Mime
View raw message