限制Derby日志文件大小 [英] Limit Derby Log File Size

查看:141
本文介绍了限制Derby日志文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当Apache Derby在我们的应用程序中抛出SQLException时,我们的应用程序会将derby.log文件的内容发送到我们的服务器。

Our app sends the contents of the derby.log file to our server whenever Apache Derby throws a SQLException in our app.

为了获取详细的日志,我们

In order to get detailed logs, we are setting the 'derby.infolog.append' property to true.

但是,我们注意到很大的日志文件,因为每次连接时日志还包含启动输出

However, we are noticing enormously large logs files since the logs also contain bootup output each time a connection is made to the database.

注意:我们在嵌入式模式下使用Derby。

NOTE: we are using Derby in embedded mode.

有没有办法

例如,仅记录最近的1000行日志,然后开始覆盖最早的日志行,从而限制了derby记录到derby.log文件的行总数吗?

For example, only logging the most recent 1000 lines of logs and then begin to overwrite the oldest entries.

我们的目标是从最终用户那里获得有用的调试信息,但要防止日志文件增长到难以管理的大小。

Our goal is to get useful debugging info from end users but to prevent the log files from growing to unmanageable sizes.

预先感谢

Jim

推荐答案

您可以创建一个自定义日志记录类,并使用derby.stre进行指定。如上所述的am.error.field。日志记录类不必实现为文件-如果您要限制日志记录数据的大小,则可以轻松地将其保存在内存中。

You can create a custom logging class, and specify this using derby.stream.error.field as mentioned above. The logging class doesn't have to implemented as a file - if you will be limiting the size of the logging data you can easily hold it in memory.

第二个优点这是因为在遇到问题时,您在处理日志数据方面具有很大的灵活性。也许压缩(或加密)数据并自动在帮助系统中打开票证(例如)。

The second advantage to this is that when a problem is encountered, you have a great deal of flexibility in what to do with the logging data. Perhaps compress (or encrypt) the data and automatically open a ticket in your help system (as an example).

以下是一个非常简单的自定义日志记录解决方案的示例:

Here's an example of a very simple custom logging solution:

import java.io.CharArrayWriter;

public class CustomLog {

    public static CharArrayWriter log = new CharArrayWriter();

    public static void dump() {
        System.out.println(log.toString());
    }
}

您可以将CharArrayWriter替换为大小受限的缓冲区

You can replace the CharArrayWriter with a size limited buffer of some sort, and add an implementation of dump() to do what you will with the resulting log data.

一个简短的示例程序演示如下:

A short example program demonstrating this follows:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DerbyLoggingExample {

    public DerbyLoggingExample() {
        System.setProperty( "derby.stream.error.field", "CustomLog.log");

        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        String dbName = "logdemoDB";
        String connectionURL = "jdbc:derby:" + dbName + ";create=true";

        String createCommand = "create table test_table ("
            + "test_id int not null generated always as identity, "
            + "test_name varchar(20)"
            + ")";

        try {
            Class.forName(driver);
        }
        catch( java.lang.ClassNotFoundException e ) {
            System.out.println( "Could not load Derby driver." );
            return;
        }

        Connection conn = null;
        Statement statement = null;

        try {
            conn = DriverManager.getConnection(connectionURL);
            statement = conn.createStatement();

            statement.execute(createCommand);
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
            System.out.println( "SQLException encountered. Dumping log.");
            CustomLog.dump();
            return;
        }
        finally {
            try {
                statement.close();
                conn.close();
            }
            catch( SQLException e ) {
                // Do nothing.
            }
        }

        System.out.println( "Processing done. Dumping log." );
        CustomLog.dump();
    }

    public static void main(String[] argv) {
        DerbyLoggingExample thisApp = new DerbyLoggingExample();
    }
}

这篇关于限制Derby日志文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆