简体   繁体   中英

Store Logging information to oracle database

I have currently implemented a java swing application. In that application I have used java.util.logging to log things in to a text file. But It is difficult to go through the text file since the file is very big.

So I'm thinking to store logging information in to a oracle database(Which i am using for the application) and provide swing interface to access that table. So I will be able search that table for certain logging levels like INFO and SEVERE. Is there way to do that using java util package or using Log4j. Pls help

看看这些appender: org.apache.log4j.jdbc.JDBCAppender或改进版本org.apache.log4j.jdbcplus.JDBCAppender

You could write your own Appender by extending the org.apache.log4j.AppenderSkeleton . You can make him configurable for several data storages and define how to split up the LoggingEvent where you can get the seperated informations as line-number, class-name, message, logger-severity etc.

public class StorageBasedAppender
    extends AppenderSkeleton
{
    [...]

    @Override
    protected void append(LoggingEvent event)
    {
        // Write to your database or other storages
    }
}

You could enhance this class by making it configurable and more. If you don't need somewhat specific, regardings to the other questions make use of the JDBCAppenderConfiguration . Which can be configured easily

<appender name="jdbcAppender" class="org.apache.log4j.jdbc.JDBCAppender"> 
    <param name="URL" value="jdbc:oracle:thin:@sd1.hbs.edu:1521:sc1" /> 
    <param name="Driver" value="oracle.jdbc.driver.OracleDriver" /> 
    <param name="User" value="user" /> 
    <param name="Password" value="password" /> 
    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" 
          value="INSERT INTO LOGGING_SAMPLES_TEST 
          (log_date, log_level, location, message) 
          VALUES ( '%d{ISO8601}','%p', '%C;%L', '%m' )" 
        /> 
    </layout> 
</appender> 

You can make use of a JDBC Log4j Appender.

Check out Tutorialspoint or Apache Wiki

You might need to change your configuration accordingly whether you use .properties or .xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM