Ibatis using LOG4J2 output log

Source: Internet
Author: User

Original link Here

Ibatis is an old project, 2.3. After the release of 4.726, the project was renamed MyBatis, and the project home page is currently http://mybatis.github.io/.

I have been in touch with Ibatis since 08 and have been using the 2.3.4.726 version until the current project. Ibatis just right to meet the project team in the ORM, SQL maintenance needs, so has been lazy to switch to other similar open source software, such as MyBatis.

Recently participated in the new project development, everything must start from scratch. In order to save time, I directly to the original project database related operation code to use, found that encountered a problem. The new project outputs the log based on Log4j2, and after I have configured the Ibatis-related logger, I find that SQL executes without any log output. Ibatis in the log output post-assembled SQL, bound variables, query returned results, and so on, which is extremely useful for me, and now the relevant log in the new project has disappeared, then the correctness of the analysis of SQL has to rely on guessing. Project progress is relatively tight, I did not have time to carefully consider the issue of the log.
Fortunately, the urgent need to quickly take care of, I finally have a relatively sufficient time to solve this ibatis log output problem. The LOG4J2 configuration file used in the project is not easy to paste directly, after streamlining the following

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="java.sql" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<root level="debug">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>

According to the official website, I have repeatedly compared several times, to confirm that the configuration file is not a problem, it should be a problem elsewhere.

Find Ibatis source, open Ibatis class to create the loggercom.ibatis.common.logging.LogFactory, this class is the constructor factory class for the logger, as in the next section of code that defines the order in which the logger is found.

static
{
tryImplementation("org.apache.commons.logging.LogFactory", "com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl");
tryImplementation("org.apache.log4j.Logger", "com.ibatis.common.logging.log4j.Log4jImpl");
tryImplementation("java.util.logging.Logger", "com.ibatis.common.logging.jdk14.Jdk14LoggingImpl");
tryImplementation("java.lang.Object", "com.ibatis.common.logging.nologging.NoLoggingImpl");
}

As can be seen from this code, Ibatis supports four loggers, namely:

    • Commons-logging
    • apache-log4j
    • JDK Native Log system
    • Empty logger, log not logged

When I saw this code, my funny brain suddenly started to turn quickly. In the previous project, the Ibatis and log4j mates can output the log normally, because Ibatis implements a dedicated logger construction factory for log4j, and the current project is based on the log4j2,ibatis without a specially customized logger factory class, So the log does not output properly. Thought here, the log can not output the reason to find, but how to let Ibatis log back to normal, I hesitated for a long time. Consider if you want to modify the source code of Ibatis, modeledcom.ibatis.common.logging.log4j.Log4jImplA new logger builder is written. Hesitated for a long time, think or forget, I am not afraid to modify the source of the Ibatis, previously participated in a project, has been in order to modify the other issues modified Ibatis source code, the compilation environment and so on; To modify the point of the new project source code is not a thing at all, But considering that the project will be released when the open-source software scanning and rectification, I am counseling, or less to find something for themselves.
Back to Ibatis to create the Logger factory class code, pondering half a day, guess Commons-logging may be a breakthrough. If it is possible to solve the problem by extending Commons-logging's construction plant, there is no need to add new dependencies in the project because there are already commons-logging related jar files in the project's Lib directory.
Thank Csdn on the unknown enthusiastic netizen, I found a copy of commons-logging configuration instructions, follow the instructions in the code root path of the project to add the Commons-logging.properties file, the content is as follows

org.apache.commons.logging.Log=ibatis.Log4j2Impl

This ibatis. Log4j2impl class first occupy, the specific realization of the user's note did not write, the only information is the class Log4j2impl need to implement interface Org.apache.commons.logging.Log, but how to achieve unclear. Fortunately, there are some open source software in the project that use Commons-logging to record logs, In Eclipse, press F4 for the Org.apache.commons.logging.Log interface, find the class that currently implements the interface, and discover the Org.apache.commons.logging.impl.Jdk14Logg that commons-logging comes with Er is for reference, so directly copy the relevant code to Ibatis. Log4j2impl, after solving a few compilation problems, the factory class is done, and then the next step is to verify that the Ibatis can output normally.
Write a simple test class based on Ibatis, the code is as follows

package ibatis;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import Org.apache.logging.log4j.LogManager;
Import Org.apache.logging.log4j.Logger;

Import com.ibatis.sqlmap.client.SqlMapClient;
Import Com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Test {

private static Logger Logger = Logmanager.getlogger (Test.class);
public static void Main (string[] args) throws Exception {
Sqlmapclient sqlmapclient = sqlmapclientbuilder.buildsqlmapclient (Test.class.getClassLoader (). getResourceAsStream ("Sql-map-config.xml"));
Logger.info ("before query");
Sqlmapclient.queryforlist ("User.selectsubscriberinfo");
Logger.info ("Succeed to query");
}
}

Execute this code in Eclipse, and finally see the long-lost SQL run log.

Ibatis using LOG4J2 output log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.