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 |
|
Attempts to load and activate SLF4J facade. |
|
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):
-
slf4j-api-xxx.jar
-
slf4j-simple-xxx.jar
(which logs directly to the console via System error).
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 |
|
Enables or disables logging to |
|
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 fromcom.nuodb.jdbc.DataSource.getLogWriter()
orDriverManager.getLogWriter()
. -
The logger must be initialized using
com.nuodb.jdbc.DataSource.setLogWriter()
orDriverManager.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 |
---|---|---|---|
|
|
Enables or disables logging. |
|
|
|
Sets the logging level |
|
|
file-path |
Defines the path to a file to write log output into.
If the file cannnot be opened for write a |
null |
|
|
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 todateFormatStyle
or defaultyyyy-MM-dd HH:mm:ss.SSS
, ifdateFormatStyle
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) ){
...
}
}