Logging

NuoDB’s JDBC driver supports three types of Loggers:

  • Slf4JLogger

  • Jdk14Logger

  • StandardLogger

This section is concerned with logging generated by the NuoDB JDBC driver in your client application(s).

Warnings and errors are logged automatically.

  • By default, enabling logging (using any of the three options described in this section) enables DEBUG (FINE) level logging This is sufficient to log the DataSource and its configuration when it is created.

  • For more detailed logging of connections, statements and result sets in use, enable TRACE (FINEST) level logging.

SLF4J Logger

The Simple Logging Façade for Java (SLF4J) serves as a façade or abstraction for various logging frameworks, such as java.util.logging, Commons logging, logback and log4j. It contains API compatible implementations of the interfaces of all these libraries and then channels all logging to a single underlying logging framework to produce output. SLF4J allows developers to plug in the desired logging framework at deployment time and is specifically designed to avoid problems with different dependency JARs requiring differing logging libraries at run time.

Note that using SLF4J in your library/application implies the addition of only a single mandatory dependency, slf4j-api-xxx.jar (where xxx is the version). However, a logging implementation will also be required at runtime. Any of the logging frameworks mentioned above can be used.

Recommendation: Use Logback which implements SLF4J directly.

NuoDB’s slf4JLogger implements the SLF4J API. To enable the slf4JLogger, simply set the slf4jLogger connection property:

Connection Property Possible Values Description Default Value

slf4jLogger

true| false

Attempts to load and activate SLF4J facade.

false

Example

public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DataSource */
Properties properties = new Properties();
properties.put("url", DATABASE_URL + "?slf4jLogger=true");
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);
    ...
}
public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DriverManager */
Properties properties = new Properties();
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");
properties.put("slf4jLogger", "true");

try {
    Class.forName("com.nuodb.jdbc.Driver");

    try (Connection conn = DriverManager.getConnection(DATABASE_URL, properties) ){
        ...
    }
}

The SLF4J JAR must be on your CLASSPATH if slf4jLogger connection property is set to true. A logging implementation is also required, otherwise you get this error and, although the application will run, no logging will be generated:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Simple Logger

The simplest SLF4J binding available is to include these 2 libraries on your CLASSPATH (where xxx is the version):

To set the logging level to TRACE:

/* In main() BEFORE creating your DataSource */
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel=trace");

Logback Logger

To use Logback:

  • slf4j-api-xxx.jar

  • logback-classic.xxx.jar

  • logback-core.xxx.jar

To set the logging level to TRACE:

/* BEFORE creating your DataSource */
Logger parent = LoggerFactory.getLogger("com.nuodb.jdbc");

if (parent instanceof ch.qos.logback.classic.Logger) {
    ((ch.qos.logback.classic.Logger) parent).setLevel(Level.TRACE);
}

For more information on SLF4J bindings refer to the SLF4J documentation at http://www.slf4j.org/manual.html.

JDK Logger

Directs logging to the standard Java logging API using java.util.logging.Logger. To use this logger simply set the jdk14Logger connection property:

Connection
Property
Possible
Values
Description Default
Value

jdk14Logger

true| false

Enables or disables logging to java.util.logging.Logger

false

Example

public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DataSource */
Properties properties = new Properties();
properties.put("url", DATABASE_URL + "?jdk14Logger=true");
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);

    /* Optionally get parent of this logger. Configuring this logger will */
    /* affect all loggers used by the driver.  Here we enable all logging */
    /* at all levels by raising logging level to FINEST.                  */
    Logger parentLogger = datasource.getParentLogger();
    parentLogger.setLevel(Level.FINEST);
    ...
}
public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DriverManager */
Properties properties = new Properties();
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");
properties.put("jdk14Logger", "true");

try {
    Class.forName("com.nuodb.jdbc.Driver");

    /* Optionally get parent of this logger. Configuring this logger will */
    /* affect all loggers used by the driver.  Here we enable all logging */
    /* at all levels by raising logging level to FINEST.                  */
    Logger parentLogger = DriverManager.getDriver(DATABASE_URL).getParentLogger();
    parentLogger.setLevel(Level.FINEST);

    try (Connection conn = DriverManager.getConnection(DATABASE_URL, properties) ){
        ...
    }
}

Standard Logger

NuoDB’s fall-back logging option if you do not wish to use SLF4J or JDK logging. The Standard Logger is enabled by default, but generates no output unless given a PrintWriter to log output to.

  • The Standard Logger uses the java.io.PrintWriter obtained from com.nuodb.jdbc.DataSource.getLogWriter() or DriverManager.getLogWriter().

  • The logger must be initialized using com.nuodb.jdbc.DataSource.setLogWriter() or DriverManager.setLogWriter() or no logging output is generated.

  • To use Standard Logger set the standardLogger connection property.

The connection properties related to Standard Logger are standardLogger.level, standardLogger.logWriter and standardLogger.pattern:

Connection
Property
Possible
Values
Description Default
Value

standardLogger

true | false

Enables or disables logging.

true

standardLogger. level

OFF| ERROR| WARN| INFO| DEBUG| TRACE

Sets the logging level. If debugging an issue, specify TRACE.

INFO

standardLogger. logWriter

file-path

Defines the path to a file to write log output into. If the file cannnot be opened for write a FileNotFoundException is thrown.

null

standardLogger. pattern

pattern

Sets the message format for logging - see below.

See below.

Formatting Log Output

The message pattern can include several placeholders which will be expanded at runtime:

  • %level outputs log level

  • %date(dateFormatStyle) produces current timestamp formatted to dateFormatStyle or default yyyy-MM-dd HH:mm:ss.SSS, if dateFormatStyle is not specified

  • %line(short\|long\|traceFormatStyle) outputs stack trace element. (traceFormatStyle can be built from any combination of {className}, {methodName}, {shortClassName}, {lineNumber} placeholders)

  • %message prints logging message.

Default logging pattern is : [%date(yyyy-MM-dd HH:mm:ss.SSS)] (%level) (%line(long)) - %message

Example

Here is an example of using StandardLogger:

public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DataSource */
String loggingProperties = "?standardLogger=true"
    + "&standardLogger.level=TRACE"
    + "&standardLogger.pattern=[%date] (%level) (%line(long))";

Properties properties = new Properties();
properties.put("url", DATABASE_URL + loggingProperties);
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);
    datasource.setLogWriter(new PrintWriter(System.out));
    ...
}
public static final String DATABASE_URL = "jdbc:com.nuodb://localhost/test";

/* Using a DriverManager */
Properties properties = new Properties();
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");
properties.put("standardLogger", "true");
properties.put("standardLogger.level", "TRACE");
properties.put("standardLogger.pattern", "[%date] (%level) (%line(long))");

DriverManager.setLogWriter(new PrintWriter(System.out));

try {
    Class.forName("com.nuodb.jdbc.Driver");

    try (Connection conn = DriverManager.getConnection(DATABASE_URL, properties) ){
        ...
    }
}