How MyBatis Handles Logging: From SLF4J Facade to Dynamic Proxies

This article explains MyBatis's logging architecture, covering the supported log implementations, the role of SLF4J, how MyBatis selects and configures a logger via LogFactory, and the use of JDK dynamic proxies to route JDBC logging through separate logger objects.

Programmer DD
Programmer DD
Programmer DD
How MyBatis Handles Logging: From SLF4J Facade to Dynamic Proxies

MyBatis Logging Categories

MyBatis supports six logging types configured via the logImpl attribute in the global configuration: SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, plus NO_LOGGING which disables logging.

The corresponding module package names are:

Log Property

Module Package

Implementation

SLF4J

slf4j

Uses the SLF4J logging framework

LOG4J

log4j

Uses Log4J (1.x)

LOG4J2

log4j2

Uses Log4J (2.x)

JDK_LOGGING

jdk14

Uses java.util.logging

COMMONS_LOGGING

commons

Uses Apache Commons Logging

STDOUT_LOGGING

stdout

Uses System.out

NO_LOGGING

nologging

No logging output

Note: SLF4J is only a facade; it requires an actual logging implementation (e.g., Logback) to produce output.

Simple Overview of SLF4J

SLF4J (Simple Logging Facade for Java) provides a unified API for various logging frameworks, allowing developers to switch implementations by replacing the underlying JAR without changing source code.

When only SLF4J is configured without an actual implementation, MyBatis will not print any logs and will issue a warning.

MyBatis Logging Implementation Mechanism

Log Parsing

MyBatis reads the logImpl value from the configuration, looks up the corresponding class in TypeAliasRegistry, or loads it via Class.forName. Custom loggers can be supplied by implementing the Log interface.

The Configuration constructor registers default aliases, and setLogImpl ultimately delegates to LogFactory.

LogFactory

LogFactory

creates logger adapters. Its static block initializes built‑in loggers in a defined order, attempting each implementation until one succeeds. If a custom logger is specified, it overrides the default selection.

JDBC Logging

The org.apache.ibatis.logging.jdbc package splits logging into four proxy classes: ConnectionLogger, PreparedStatementLogger, ResultSetLogger, and StatementLogger. All extend BaseJdbcLogger and implement InvocationHandler, using JDK dynamic proxies.

Each proxy logs relevant JDBC actions; for example, ConnectionLogger logs connection method calls, and BaseJdbcLogger.debug prefixes messages with a stack‑depth indicator.

Summary

The article analyzes how MyBatis loads and selects a logging implementation via LogFactory, and how it employs dynamic proxies to delegate JDBC logging to separate logger objects, enabling flexible integration with various Java logging frameworks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaloggingMyBatisDynamic Proxyslf4j
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.