1

I'm working on a spring-based application which has to communicate with a SQL database through mybatis: all right but the logs destination.

For some reason mybatis logs to the wrong file, could you help me to figure out why? Here's my configuration:

log4j.properties:

### Appenders
# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=WARN
log4j.appender.console.layout=org.apache.log4j.PatternLayout

# Application file appender
log4j.appender.main=org.apache.log4j.RollingFileAppender
log4j.appender.main.File=logs/app.log
log4j.appender.main.layout=org.apache.log4j.PatternLayout
log4j.appender.main.MaxFileSize=10MB
log4j.appender.main.MaxBackupIndex=15

# Libs file appender
log4j.appender.libs=org.apache.log4j.RollingFileAppender
log4j.appender.libs.File=logs/app_libs.log
log4j.appender.libs.layout=org.apache.log4j.PatternLayout
log4j.appender.libs.MaxFileSize=10MB
log4j.appender.libs.MaxBackupIndex=15



### Loggers & additivity
# Application
log4j.additivity.our.company.basepackage=false
log4j.logger.our.company.basepackage=TRACE,main,console


# Root logger
log4j.rootLogger=INFO,libs

pom.xml snippet

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <scope>runtime</scope>
</dependency>

I find TRACE-level rows of mybatis ("org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145)") in the file "app.log".

I excluded commons-logging from spring-core, and with a dependency tree I don't see commons-logging. Why isn't mybatis logging to the file "app_libs.log"? Why does mybatis not respect the specified level?

Thank you.

Edit 1

The code with which the database gets queried has been generated with mybatis-generator, and the generated code lives somewhere under the package "our.company.basepackage".

4

1 回答 1

1

自从问题发布以来,我一直没有停下来思考这个问题,直到现在:我找到了这种行为的原因。

决定性的建议是“代码已经用 mybatis-generator 生成”,并且已经生成在应用程序的同一个子包中:这意味着用于查询数据库的 *Mapper 类实际上是在应用程序包中所以他们的日志被视为“our.company.basepackage”的日志,而不是“org.apache.ibatis”的日志。

日志行中的“org.apache.ibatis”误导了我。

在这个小小的洞察之后,我在我的 log4j.properties 中添加了以下内容:

log4j.additivity.our.company.basepackage.persistence.mybatis=false
log4j.logger.our.company.basepackage.persistence.mybatis=INFO,libs

有了这 2 行,一切正常,即 app.log 中不再有“org.apache.ibatis”行。

我希望这对使用 mybatis-generator 的其他人有用。

于 2014-10-29T09:29:53.823 回答