ActiveJDBC uses SLF4J logging facade. Please, refer to SLF4J documentation to see how to configure it with Log4J, Java Logging, Apache logging, etc.

ActiveJDBC Logging

ActiveJDBC uses a system property activejdbc.log for specifying logging. The value of this property can be:

  • blank - in this case, ActiveJDBC will spit out all available information - every SQL statement, cache hits/misses, cache purge events, etc.
  • regular expression - in this case, ActiveJDBC will only log statements that match a regular expression

If you just want to see all messages from ActiveJDBC, start your program like this:

java -Dactivejdbc.log com.acme.YourProgram

If you only want to see select messages, you can provide an expression:

java -Dactivejdbc.log=select.* com.acme.YourProgram

Dynamically filter log output

Use this call:

org.javalite.activejdbc.LogFilter.setLogExpression("regular expression goes here");

This will dynamically change ActiveJDBC log output at run time.

Custom logger

If you want 100% control over logging, you can implement ActiveJDBCLogger and configure it by adding this property to the activejdbc.properties file:

activejdbc.logger=com.myproject.MyLogger

Ensure that this class has a default constructor.

JSON Log4j logging

Many companies use advanced tools to analyze structured logs using such services as Splunk as well as various ELK implementation.

Log4j configuration

You can use a classes JsonLog4jLayout to configure your Log4j logger to achieve JSON format.

Here is an example of the log4j.properties file:

log4j.rootLogger=INFO, FILE

log4j.appender.FILE=org.apache.log4j.RollingFileAppender  
log4j.appender.FILE.layout=org.javalite.logging.JsonLog4jLayout

Such configuration will convert every log line into a self-contained JSON document with the same values as a regular log line. For example, this code:

1
2
3
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("hello");
logger.error("world");

will print the following into a log:

{"level":"INFO","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"hello"}
{"level":"ERROR","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"world"}

Such information id easy to ship to a log analyzer such as Splunk as well as various ELK for easy search and analysis.

Using Context parameters

Sometimes you need to comprehend multiple log entries in a context (web request, user actions, etc). You can use a class Context to do just that.

Here is an example:

1
2
3
4
5
Logger logger = LoggerFactory.getLogger(getClass());
Context.put("user", "joeschmoe", "user_id", "234", "email", "joe@schmoe.me");
logger.info("hello");
logger.error("world");
Context.clear();

will print the following into a log:

{"level":"INFO","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"hello", "context":{"user":"joeschmoe","user_id":"234","email":"joe@schmoe.me"}}
{"level":"ERROR","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"world", "context":{"user":"joeschmoe","user_id":"234","email":"joe@schmoe.me"}}

As you can see, every log line now includes an object "context" with corresponding values.

Class Context attaches parameters to a current thread, making them available to execution down the stack trace.

Class JsonLog4jLayout locates context values on the current thread and appends them to every log line.


How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page