Jaybird provides logging support as documented on the Jaybird Wiki. The page is subtitled “information outdated” but it works nonetheless.

Here is an example class which shows whether logging is enabled or not, and then opens a connection:

public static final void main(String... args) throws SQLException {

    if ("true".equals(System.getProperty("FBLog4j"))) {
      System.out.println("[*] logging over Log4J");
    } else {
      System.out.println("[*] not logging over Log4J");
    }

    System.out.println("[*] create connection");

    Properties props = new Properties();
    props.setProperty("user", "SYSDBA");
    props.setProperty("password", "masterkey");
    props.setProperty("encoding", "UTF8");

    try (Connection connection = DriverManager.getConnection(
            "jdbc:firebirdsql://localhost:3050/"
            + "C:\\Program Files\\Firebird\\Firebird_2_5"
            + "\\examples\\empbuild\\EMPLOYEE.FDB",
            props)) {

      System.out.println("[*] close connection");
    }
  }

Note that our projects requires Log4j and the Jaybird 2.2.14 driver on the classpath.

We use the log4j.properties configuration in the documentation:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%c{1},%p] %m%n

log4j.rootCategory=DEBUG, stdout
log4j.category.org.firebirdsql=DEBUG, stdout

If we run the program and include the -DFBLog4j=true JVM property, the output is:

[*] logging over Log4J
[*] create connection
[AbstractJavaGDSImpl,DEBUG] Got socket
[AbstractJavaGDSImpl,DEBUG] Got socket
[AbstractJavaGDSImpl,DEBUG] user.name: ***
[AbstractJavaGDSImpl,DEBUG] user.name: ***
[AbstractJavaGDSImpl,DEBUG] op_connect
[AbstractJavaGDSImpl,DEBUG] op_connect
...

To fix the double output, we can add this line to the configuration:

...
# exclude messages from parent logger:
log4j.additivity.org.firebirdsql=false

With this fix, the log output becomes:

[*] logging over Log4J
[*] create connection
[AbstractJavaGDSImpl,DEBUG] Got socket
[AbstractJavaGDSImpl,DEBUG] user.name: ***
[AbstractJavaGDSImpl,DEBUG] op_connect
[AbstractJavaGDSImpl,DEBUG] sent
[AbstractJavaGDSImpl,DEBUG] op_accept
[AbstractJavaGDSImpl,DEBUG] received
[AbstractJavaGDSImpl,DEBUG] op_attach
[AbstractJavaGDSImpl,DEBUG] sent
[AbstractJavaGDSImpl,DEBUG] op_response
[AbstractJavaGDSImpl,DEBUG] readStatusVector arg:isc_arg_gds int: 0
[AbstractJavaGDSImpl,DEBUG] received
[AbstractJavaGDSImpl,DEBUG] op_info_database
[AbstractJavaGDSImpl,DEBUG] sent
[AbstractJavaGDSImpl,DEBUG] op_response
[AbstractJavaGDSImpl,DEBUG] readStatusVector arg:isc_arg_gds int: 0
[AbstractJavaGDSImpl,DEBUG] received
[AbstractJavaGDSImpl,DEBUG] parseDatabaseInfo: first 2 bytes are 318 or: 62, 1
[AbstractJavaGDSImpl,DEBUG] isc_info_db_sql_dialect:3
[AbstractJavaGDSImpl,DEBUG] isc_info_firebird_version:WI-V2.5.8.27089 Firebird 2.5-WI-V2.5.8.27089 Firebird 2.5/tcp (***)/P10
[AbstractJavaGDSImpl,DEBUG] isc_info_ods_version:11
[AbstractJavaGDSImpl,DEBUG] isc_info_ods_minor_version:2
[*] close connection
[AbstractJavaGDSImpl,DEBUG] op_detach
[AbstractJavaGDSImpl,DEBUG] sent
[AbstractJavaGDSImpl,DEBUG] op_response
[AbstractJavaGDSImpl,DEBUG] readStatusVector arg:isc_arg_gds int: 0
[AbstractJavaGDSImpl,DEBUG] received
[AbstractJavaGDSImpl,DEBUG] About to invalidate db handle
[AbstractJavaGDSImpl,DEBUG] successfully invalidated db handle

Use case

The log output can become very useful to detect issues with the client application code.

For this example, we remove one line from the code with sets the encoding for the JDBC connection.

Now the log will include a warning because the client does not specifiy an encoding:

    Properties props = new Properties();
    props.setProperty("user", "SYSDBA");
    props.setProperty("password", "masterkey");
    // encoding undefined
    // props.setProperty("encoding", "UTF8");

(to be continued)

[*] logging over Log4J
[*] create connection
[FBManagedConnection,WARN] WARNING: No connection characterset specified (property lc_ctype, encoding, charSet or localEncoding), defaulting to characterset NONE
[AbstractJavaGDSImpl,DEBUG] Got socket
...
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s