Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Log record of MyBatis source code parsing

Shulou Source: shulou.com Published: 2022-06-03 07:38:38 10月01日 Update

One. Overview

MyBatis does not provide log implementation classes and needs to access third-party logging components, but third-party logging components have their own Log levels and are different, but MyBatis provides four levels: trace, debug, warn and error.

Automatic log scanning is implemented, and the loading priority of the third-party log plug-in is as follows: slf4J → commonsLoging → Log4J2 → Log4J → JdkLog

The use of the log should be elegantly embedded in the main function.

two。 Design pattern

Output various log components such as (slf4J, commonsLoging, Log4J2, Log4J, JdkLog) uniformly, and define four levels: trace, debug, warn and error.

Log module class diagram

Agent mode

3. MyBatis source log implementation

1.MyBatis integrates log components for each party

When the container starts, it is loaded according to the priority of slf4J → commonsLoging → Log4J2 → Log4J → JdkLog, and each log primary key is the implementation class of the adapter.

2.jdbc log enhancement module

Link phase:

It is realized that the InvocationHandler interface is a proxy class, which returns the Connection with logging ability, and prints the parameters at the same time.

And called the

PreparedStatementLogger to create the proxy object.

As a result, the jdbc goes from the link phase to the preparation phase:

PreparedStatementLogger implements that the InvocationHandler interface is a proxy class, returns ResultSet with logging capabilities, and prints parameters.

And ResultSetLogger is called to create the proxy object

As a result, jdbc moves from the link phase to the execution phase:

ResultSetLogger implements that the InvocationHandler interface is a proxy class that returns ResultSet with logging capabilities and prints specific sql statements. Pass in parameters and return the result.

Summary:

Mybatis's own implementation of no log integrates the log components of various log vendors through the adapter mode, unifies the output interface, and uses the proxy mode, from link to compilation, and finally to the execution phase, layer by layer proxy, so that the log is elegantly embedded into the main body function.

For example: an apple from the fruit tree to the hands of consumers, fruit growers-purchasers-wholesalers-farmers' markets-consumers. In this agency chain, consumers do not increase the price of apples in a certain link, but layer by layer. At the same time, consumers only need to buy apples in the farmers' market, and do not need to know how apples enter the farmers' market.

The same is true for farmers' markets. Apple to consumers through a total of acquirers-wholesalers-farmers market three agents, each agent returned is the price increase of the agent, shielding the source of Apple to consumers.

Tags: Logs agents consumers components Apple stages consumption Farmers' Market Market at the same time Interface Mode Link Parameter object third Party level capability Unification elegance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information macOS Shulou Technology MySQL