Limit Derby Log File Size

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

To get detailed logs, we set the 'derby.infolog.append' property to true.

However, we notice extremely large log files, since the logs also contain boot output every time a connection is made to the database.

NOTE: we use Derby in native mode.

Is there a way to limit the derby to the total number of lines that it writes to the derby.log file?

For example, it only logs the most recent 1000 lines of logs and then overwrites the oldest entries.

Our goal is to get useful debugging information from end users, but so that the log files do not grow to unmanageable sizes.

Thanks in advance,

Jim

+3
source share
3 answers

I am not so familiar with derby, but I could not find an “easy” way to do this.

But there are some derby properties that you could set to implement this yourself.

Check these

derby.stream.error.field

derby.stream.error.file

derby.stream.error.method

derby.stream.error.logSeverityLevel

So, I think you are writing some class that subclasses java.io.OutputStream or java.io.Writer , and then you either

  • implements desired behavior or
  • ? + wrap
  • ripp-off RollingFileLoggerClass (RollingFileAppender log4j, RollingFileWriter clapper,...)
+1

derby.stream.error.field, . - , .

, , . , ( ) ( ).

:

import java.io.CharArrayWriter;

public class CustomLog {

    public static CharArrayWriter log = new CharArrayWriter();

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

CharArrayWriter dump(), , .

, :

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();
    }
}
+1

, , , Derby.log Derby.

, Derby , , . , , Network Server, derby.log?

, derby.log, , . , ; .

, derby.log, Derby , , Derby .

0

Source: https://habr.com/ru/post/1720643/


All Articles