Logging

NuoDB supports three types of Loggers:

  • Slf4JLogger

  • Jdk14Logger

  • StandardLogger

SLF4J Logger

The Simple Logging Facade for Java (SLF4J) serves as a facade 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 is will be required at runtime. Any of the logging frameworks mentioned above can be used.

Recomendation: 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

String url = "jdbc:com.nuodb://localhost/test?slf4jLogger=true";
Properties properties = new Properties();
properties.put("url", "dba");
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);
    ...
}
String url = "jdbc:com.nuodb://localhost/test";
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(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.

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

To use Logback:

  • slf4j-api-xxx.jar

  • logback-classic.xxx.jar

  • logback-core.xxx.jar

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

String url = "jdbc:com.nuodb://localhost/test?jdk14Logger=true";
Properties properties = new Properties();
properties.put("url", "dba");
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);
    Logger parentLogger = datasource.getParentLogger();
    ...
}
String url = "jdbc:com.nuodb://localhost/test";
Properties properties = new Properties();
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");
properties.put("jdk14Logger", "true");

Logger parentLogger = DriverManager.getDriver(url).getParentLogger();

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

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

Standard Logger

NuoDB’s fall-back logging option if you do not wish to use SLF4J or JDK logging. The StandardLogger 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

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 the user of StandardLogger:

String loggingProperties = "standardLogger=true"
    + "&standardLogger.level=DEBUG"
    + "&standardLogger.pattern=[%date] (%level) (%line(long))";
String url = "jdbc:com.nuodb://localhost/test?" + loggingProperties;
Properties properties = new Properties();
properties.put("url", "dba");
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");

try {
    DataSource datasource = new DataSource(properties);
    datasource.setLogWriter(new PrintWriter(System.out));
    ...
}
String url = "jdbc:com.nuodb://localhost/test";
Properties properties = new Properties();
properties.put("user", "dba");
properties.put("password", "dba");
properties.put("schema", "logschema");
properties.put("standardLogger", "true");
properties.put("standardLogger.level", "DEBUG");
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(url, properties) ){
        ...
    }
}