Skip to content

Commit 0619426

Browse files
committed
http://code.google.com/p/mybatis/issues/detail?id=771 : Enable setting logger impl in mybatis-config.xml
1 parent 8f210c9 commit 0619426

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ private void settingsElement(XNode context) throws Exception {
220220
configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
221221
configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
222222
configuration.setLogPrefix(props.getProperty("logPrefix"));
223+
configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
223224
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
224225
}
225226
}

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
5454
import org.apache.ibatis.executor.statement.StatementHandler;
5555
import org.apache.ibatis.io.ResolverUtil;
56+
import org.apache.ibatis.logging.Log;
57+
import org.apache.ibatis.logging.LogFactory;
5658
import org.apache.ibatis.mapping.BoundSql;
5759
import org.apache.ibatis.mapping.Environment;
5860
import org.apache.ibatis.mapping.MappedStatement;
@@ -92,6 +94,7 @@ public class Configuration {
9294
protected boolean cacheEnabled = true;
9395
protected boolean callSettersOnNulls = false;
9496
protected String logPrefix;
97+
protected Class <? extends Log> logImpl;
9598
protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION;
9699
protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
97100
protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" }));
@@ -158,6 +161,13 @@ public Configuration() {
158161
typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class.getName());
159162
typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class.getName());
160163

164+
typeAliasRegistry.registerAlias("SLF4J", org.apache.ibatis.logging.slf4j.Slf4jImpl.class.getName());
165+
typeAliasRegistry.registerAlias("COMMONS_LOGGING", org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class.getName());
166+
typeAliasRegistry.registerAlias("LOG4J", org.apache.ibatis.logging.log4j.Log4jImpl.class.getName());
167+
typeAliasRegistry.registerAlias("JDK_LOGGING", org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class.getName());
168+
typeAliasRegistry.registerAlias("STDOUT_LOGGING", org.apache.ibatis.logging.stdout.StdOutImpl.class.getName());
169+
typeAliasRegistry.registerAlias("NO_LOGGING", org.apache.ibatis.logging.nologging.NoLoggingImpl.class.getName());
170+
161171
languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
162172
languageRegistry.register(RawLanguageDriver.class);
163173
}
@@ -170,6 +180,16 @@ public void setLogPrefix(String logPrefix) {
170180
this.logPrefix = logPrefix;
171181
}
172182

183+
public Class<? extends Log> getLogImpl() {
184+
return logImpl;
185+
}
186+
187+
@SuppressWarnings("unchecked")
188+
public void setLogImpl(Class<?> logImpl) {
189+
this.logImpl = (Class<? extends Log>) logImpl;
190+
LogFactory.useCustomLogging(this.logImpl);
191+
}
192+
173193
public boolean isCallSettersOnNulls() {
174194
return callSettersOnNulls;
175195
}

src/test/java/org/apache/ibatis/logging/LogFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919

20+
import java.io.Reader;
21+
22+
import org.apache.ibatis.io.Resources;
2023
import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;
2124
import org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl;
2225
import org.apache.ibatis.logging.log4j.Log4jImpl;
2326
import org.apache.ibatis.logging.nologging.NoLoggingImpl;
2427
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
2528
import org.apache.ibatis.logging.stdout.StdOutImpl;
29+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2630
import org.junit.Test;
2731

2832
public class LogFactoryTest {
@@ -75,6 +79,17 @@ public void shouldUseNoLogging() {
7579
assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
7680
}
7781

82+
@Test
83+
public void shouldReadLogImplFromSettings() throws Exception {
84+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/logging/mybatis-config.xml");
85+
new SqlSessionFactoryBuilder().build(reader);
86+
reader.close();
87+
88+
Log log = LogFactory.getLog(Object.class);
89+
log.debug("Debug message.");
90+
assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
91+
}
92+
7893
private void logSomething(Log log) {
7994
log.warn("Warning message.");
8095
log.debug("Debug message.");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2012 The MyBatis Team
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<settings>
24+
<setting name="logImpl" value="NO_LOGGING"/>
25+
</settings>
26+
27+
</configuration>

0 commit comments

Comments
 (0)