Understanding MyBatis Logging Implementation and LogFactory Mechanism

This article explains how MyBatis loads and categorizes logging implementations, details the role of SLF4J and other frameworks, describes the LogFactory adapter selection process, and shows how JDBC log proxies use dynamic proxies to output SQL execution details.

Top Architect
Top Architect
Top Architect
Understanding MyBatis Logging Implementation and LogFactory Mechanism

Logging is a crucial aspect of software development, and MyBatis, a popular Java ORM framework, provides a unified logging interface while internally adapting to various logging frameworks such as SLF4J, Log4j, and java.util.logging through an adapter pattern.

MyBatis allows the logImpl setting in its global configuration to specify one of six logging implementations (SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, or NO_LOGGING). If no implementation is configured, MyBatis defaults to NO_LOGGING.

SLF4J is a simple logging façade that does not perform actual logging itself; it delegates to a concrete logging framework like Logback. When only SLF4J is configured without an underlying implementation, MyBatis emits a warning and produces no log output.

The log implementation is resolved by MyBatis during configuration parsing: the specified name is looked up in TypeAliasRegistry, and if not found, the class is loaded via Class.forName. Custom log classes can be used by implementing the Log interface.

After locating the log class, MyBatis calls setLogImpl, which ultimately invokes LogFactory. The static initializer of LogFactory attempts to instantiate each built‑in logger in order, and the tryImplementation method selects the first successful one, falling back to the next if an exception occurs.

MyBatis also defines a jdbc logging package that contains four proxy classes— ConnectionLogger, PreparedStatementLogger, ResultSetLogger, and StatementLogger —all extending BaseJdbcLogger and implementing InvocationHandler. These proxies use JDK dynamic proxies to intercept JDBC calls and log SQL statements, parameters, and execution details.

Each proxy’s invoke method ultimately calls the debug method of BaseJdbcLogger, which formats the log message with a prefix (e.g., "==>" for SQL execution) and outputs it through the selected logging framework.

The article concludes that MyBatis’s logging mechanism relies on the LogFactory adapter to choose an appropriate logger, and uses dynamic proxies to separate logging concerns for different JDBC objects, enabling flexible and detailed SQL logging.

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.

javaloggingMyBatisJDBCslf4jLogFactory
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.