Enforce usage of a specific logging API like Slf4j in an application and avoid usage of unwanted frameworks.

This tutorial is written for version 1.4.0 of jQAssistant.

1. Overview

Java frameworks or libraries very often introduce transient dependencies into a project. This way usually a set of multiple logging APIs becomes available but there should only be one used by the application code. As the provided APIs look very similar it is quite easy for developers to use the wrong framework by accident.

This tutorial demonstrates how popular logging frameworks can be identified and usage of the SLF4j API can be enforced.

This tutorial assumes that jQAssistant has already been integrated into the build process. The example provided under Resources is based on Apache Maven. The shown rules are located in the file jqassistant/logging.adoc.

2. Identify Logging Frameworks

In a first step the logger types of all known and potentially available logging frameworks are identified by the concept logging:Logger:

.Labels the Logger APIs of known logging frameworks with `Logger`.
  logger.fqn in [
  logger as Logger

The types listed in the WHERE clause are the APIs providing methods that are invoked to log messages.

There’s a logging framework missing in the list? Just add it and let us know!

The second concept logging:Slf4jLogger selects the logging framework that shall be used by the application, in this case it’s the SLF4j API:

.Adds the label `Slf4j` to the type `org.slf4j.Logger`.
  slf4jLogger.fqn = "org.slf4j.Logger"

The concept is based on logging:Logger and adds another label Slf4j to the type org.slf4j.Logger.

3. Find Invocations Of Unwanted Logging Frameworks

Relying on these concepts the constraint logging:Slf4jLoggerMustBeUsed can be defined:

.Only methods provided by the Slf4j API must be used for logging.
  method as Method, invokes.lineNumber as LineNumber, logger as InvalidLogger

The rule returns all methods that invoke a logger method which is not provided by the Slf4j API as violations and reports the affected method, the line number and the API of the used logging framework.

4. Activate The Rules

For execution the constraint should be included within a group. The file jqassistant/logging.adoc therefore defines logging:Default:

== Logging

This document describes the rules regarding logging.

The group itself is included in the group default defined in jqassistant/index.adoc:

== Default Rules

This section describes that default rules that are executed during each build.

- <<logging:Default>>

5. Resources