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: Using the same tables for log4j with JDBC appender and Log4PLSQL
Date Wed, 08 Mar 2006 13:19:48 GMT
The DB Appender is self written and configured as follows:

    <appender name="DB_APPENDER" class="ch.ergonomics.pms.common.supervision.PMSJDBCAppender">
        <param name="PreparedSQL" value="{ ? = call INS_LOG( ?, ?, ?, ?, ?, ? ) }"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%F:%L@@%-5p@@%-10X{category}@@%-7t@@%m"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="warn"/>
            <param name="LevelMax" value="fatal"/>
            <param name="AcceptOnMatch" value="false"/>
        </filter>
    </appender>

Here the code of the appender class. Some properties are configured at run-Time (user, password,
connection string etc.)

/**
 * PMSJDBCAppender can be used within the log4j framework to log into a DB using connection
pooling and
 * prepared statements.
 * @author bender
 */
public class PMSJDBCAppender extends JDBCAppender
{
    /** Holds the pattern enumeration as defined in the property file */
    private String myValues;
    /** the sql is defined in property file */
    private String myPreparedSQL;
    /** the DB access code */
    private String myDBAccessCode;
    /** the user name */
    private String myUserName;
    /** the password */ 
    private String myPassword;
    /** the connection string */
    private String myConnectionString; 
    /** the JDBCDriver */
    private String myJDBCDriver;
    /** the statement to insert log entries */
    private CallableStatement myCstmt = null;
    /** DB connector utility */
    private DBConnector myDBConnector = null;
    

    /** standard constructor */
    public PMSJDBCAppender()
    {
        super();
    }

    /** closes the appender before disposal */
    public void finalize() 
    {
        if ( myCstmt != null )
        {
            try
            {
                myCstmt.close();
            }
            catch ( SQLException e )
            {
                // ignore
            }
        } // if myCstmt != null

        myCstmt = null;
        
        super.finalize();
    }

    /**
     * Creates and prepares the used statement for inserting log informations into DB.
     * <P>
     * All exceptions are ignored with the only drawback that no logging into DB occurs.
     * 
     * @return true if the statement has been prepared, false in all other cases, especially
     *         if one of the needed strings is not initialized (ConnectionString, Username,
     *         Password, JDBCDriver, DBAccessCode). 
     */
    private boolean createStatement()
    {
        if (    ( myConnectionString == null )
             || ( myUserName == null         )
             || ( myPassword == null         )
             || ( myJDBCDriver == null       )
             || ( myDBAccessCode == null     ) )
        {
            // not yet properly initialized
            myCstmt = null;
            return false;
        } // if 
        
        try
        {
            if ( myDBConnector == null )
            {
                myDBConnector = new DBConnector( myConnectionString, 
                                                 myUserName, 
                                                 myPassword, 
                                                 myJDBCDriver, 
                                                 null ); // <-- no logger is provided,
so no loggin occurs 
                                                         // within DBConnector
            } // if myDBConnector == null
             
            myDBConnector.getConnection().setAutoCommit( true );
            myCstmt = myDBConnector.getConnection().prepareCall( myPreparedSQL );
            myCstmt.registerOutParameter( 1, java.sql.Types.INTEGER );
            myCstmt.setString( 2, myDBAccessCode );
            
            return true;
        }
        catch ( SQLException e )
        {
            myCstmt = null;
            return false;
        }
        catch ( Exception e )
        {
            myCstmt = null;
            return false;
        }
    }
    
    /**
     * @see org.apache.log4j.jdbc.JDBCAppender#execute(java.lang.String)
     */
    public void execute( String aSql )
        throws SQLException
    {
        if ( myCstmt == null )
        {
            if ( ! createStatement() )
            {
                return;
            } // if ! createStatement() 
        } // if myCstmt == null
        
        
        StringTokenizer tokenizer = new StringTokenizer( aSql, "@@" );

        int i = 3;
        while ( tokenizer.hasMoreTokens() )
        {
            String token = tokenizer.nextToken().trim();
            switch ( i )
            {
                case 4 : // level. We have to convert the string into the level number
                    myCstmt.setInt( i, Level.toLevel( token ).toInt() );
                    break;
                case 6 : // field reference. We put the session id in it
                    if ( token.equals( "main" ) )
                    {
                        myCstmt.setInt( i, 0 );
                    }
                    else
                    {
                        try
                        {
                            myCstmt.setInt( i, new Integer( token.substring( 5 ) ).intValue()
);
                        }
                        catch ( NumberFormatException e )
                        {
                            // ignore and insert 0:
                            myCstmt.setInt( i, 0 );
                        }
                    }
                    break;
                default :
                    myCstmt.setString( i, token );
                    break;
            }
        
            i++;
        }
    
        myCstmt.executeUpdate();
    }
    
    /**
     * property access
     * @param aString - the new value of the property
     */
    public void setPreparedSQL( String aString )
    {
        myPreparedSQL = aString;
    }

    /**
     * property access
     * @param aString - the new value of the property
     */
    public void setValues( String aString )
    {
        myValues = aString;
    }

    /**
     * @see org.apache.log4j.jdbc.JDBCAppender#getLogStatement(org.apache.log4j.spi.LoggingEvent)
     */
    protected String getLogStatement( LoggingEvent aEvent )
    {
        // this formats only the message but not the stack trace of the throwable
        StringBuffer result = new StringBuffer( super.getLogStatement( aEvent ) );
        
        if ( layout.ignoresThrowable() ) 
        {
            String[] s = aEvent.getThrowableStrRep();
            if ( s != null ) 
            {
                int len = s.length;
                
                for ( int i = 0; i < len; i++ ) 
                {
                    result.append( Layout.LINE_SEP );
                    result.append( s[i] );
                }
            }    
        }
        
        return result.toString();
    }

    /**
     * property access
     * @param aString the new value of the property
     */
    public void setDBAccessCode( String aString )
    {
        myDBAccessCode = aString;
    }

    /**
     * @param aConnectionString The connectionString to set.
     */
    public void setConnectionString( String aConnectionString )
    {
        myConnectionString = aConnectionString;
    }
    /**
     * @param aDriver The jDBCDriver to set.
     */
    public void setJDBCDriver( String aDriver )
    {
        myJDBCDriver = aDriver;
    }
    /**
     * @param aPassword The password to set.
     */
    public void setPassword( String aPassword )
    {
        myPassword = aPassword;
    }
    /**
     * @param aUserName The userName to set.
     */
    public void setUserName( String aUserName )
    {
        myUserName = aUserName;
    }
}

> -----Original Message-----
> From: Rakesh Patel [mailto:rakesh.mailgroups@gmail.com]
> Sent: Wednesday, March 08, 2006 1:44 PM
> To: Log4J Users List
> Subject: Re: Using the same tables for log4j with JDBC appender and
> Log4PLSQL
> 
> 
> Hi Heri,
> 
> I would be vey interested to know how you configured all 
> that. Also, is 
> there no performance hit with all that logging from java to the db?
> 
> Rakesh
> 
> Bender Heri wrote:
> 
> >I dont use log4plsql, but I have a System which logs by a 
> JDBC-Appender to a Oracle-Table, and the stored procedures 
> within Oracle log exceptions to the same table (by self 
> written sql-code) without any problem.
> >Heri
> >
> >  
> >
> >>-----Original Message-----
> >>From: duvelbier-tsmets@yahoo.com [mailto:duvelbier-tsmets@yahoo.com]
> >>Sent: Tuesday, March 07, 2006 7:11 PM
> >>To: log4j-user@logging.apache.org
> >>Subject: Using the same tables for log4j with JDBC appender and
> >>Log4PLSQL
> >>
> >>
> >>We are foreseing to use Log4PLSQL in our DB & I was wandering 
> >>if we could use this for our WebApp too...
> >>I would like to kknow if there are people who use both and 
> >>preferably inserting the messages into the same tables ?
> >>
> >>Tx,
> >>
> >>\T, 
> >>-- Any fool can write code that a computer can understand. 
> >>Good programmers write code that humans can understand.
> >>                                                   Martin Fowler 
> >>T. : +32 (0)2 742 05 94
> >>M. : +32 (0)497 44 68 12
> >>@  : duvelbier-tsmets@yahoo.com
> >>Do you skype too ... ?
> >>
> >>
> >>
> >>
> >>------------------------------------------------------------
> ---------
> >>To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> >>For additional commands, e-mail: log4j-user-help@logging.apache.org
> >>
> >>
> >>    
> >>
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> >For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
> >
> >  
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 

---------------------------------------------------------------------
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