DeooK
DeooK

Reputation: 545

How to log with MyBatis and Log4J2

I'm trying to understand how to have and configure logging with MyBatis, Spring MVC and Log4J2 on Wildfly.

My problem is to understand how to set up MyBatis which, at the moment, is ignoring my Log4J2 configuration and I want to set up it with Java only.

org.apache.ibatis.session.Configuration seems to be the class I need and i found how to set some configurations, like JdbcTypeForNull. I found to set Log4J2 (setLogImpl(Log4j2Impl.class)) but I've already done this with

    org.apache.ibatis.logging.LogFactory.useLog4J2Logging();

What I'm not understanding is why MyBatis ignores my log4j2.properties file and says:

Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration

This is my log4j2.properties

name=LoggingConfig
property.filename = logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=../LOGS/logs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg

loggers=console
logger.console.name=myPrj.database.mybatis.mappers
logger.console.level=DEBUG
logger.console.additivity=true
logger.console.appenderRef.console.ref = STDOUT
logger.console.myPrj.database.mybatis.mappers.MainMapper=TRACE

rootLogger.level = ALL
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Any advice? I found the offical docs really poor, with only xml configuration and only with log4j.

Upvotes: 0

Views: 5595

Answers (1)

jornathan
jornathan

Reputation: 856

Here is one of best practice Spring+myBatis. https://github.com/mybatis/jpetstore-6.

Inside project, you can find applicationContext.xml at "jpetstore-6-master\src\main\webapp\WEB-INF".

add property (value is path where config file located in)

<property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>

to mybatis-config.xml as below.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
</bean>

and create file "mybatis-config.xml" same directory, as below. Be careful when you writing this file, contents should follow mybatis-3-config.dtd.(especially order of elements)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J2"/>
    </settings>
</configuration>

The MyBatis log factory will use the first logging implementation it finds (implementations are searched in the above order) -- refer from : [1]: http://www.mybatis.org/mybatis-3/logging.html

So, you don't need to call this method in your code.

org.apache.ibatis.logging.LogFactory.useLog4J2Logging();

Let's get back,

Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration

  • configuration is element in mybatis-config.xml. --> There is no configuration for mybatis.

  • configLocation is property name in context.xml. --> Can't find where configuration file located is.

if this work well, you can see this line at console output.

"Slf4jImpl" is first items of its implemetation, but it will changed

2019-06-18 14:10:54,324 [main] DEBUG o.a.i.l.LogFactory:105 - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.

and some few lines after,

2019-06-18 14:10:59,849 [main] DEBUG o.a.i.l.LogFactory:105 - Logging initialized using 'class org.apache.ibatis.logging.log4j2.Log4j2Impl' adapter.

Upvotes: 1

Related Questions