Skip to content

Commit ba51dc5

Browse files
committed
Make configuration of Liquibase’s logging more robust
We make Liquibase aware of our custom Commons Logging-based logger by adding its package to the Liquibase ServiceLocator’s packages to scan. Previously, this was happening too late so Liquibase may have already initialized and cached a particular logger. This commit moves the registration of the extra package from the Liquibase auto-configuration to the application listener that customises Liquibase’s ServiceLocator. This ensures that the package is added before Liquibase is used. Unfortunately, configuring Liquibase’s ServiceLocator and its packages to scan causes it to try to perform some logging, resulting in it caching the wrong type of logger. We work around this problem by resetting Liquibase’s LogFactory once we’ve finished setting everything up. Closes gh-6713
1 parent 9300c6d commit ba51dc5

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import javax.sql.DataSource;
2222

2323
import liquibase.integration.spring.SpringLiquibase;
24-
import liquibase.servicelocator.ServiceLocator;
2524

2625
import org.springframework.beans.factory.annotation.Autowired;
2726
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -35,7 +34,6 @@
3534
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
3635
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3736
import org.springframework.boot.context.properties.EnableConfigurationProperties;
38-
import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
3937
import org.springframework.context.annotation.Bean;
4038
import org.springframework.context.annotation.Configuration;
4139
import org.springframework.context.annotation.Import;
@@ -52,6 +50,7 @@
5250
* @author Marcel Overdijk
5351
* @author Dave Syer
5452
* @author Phillip Webb
53+
* @author Andy Wilkinson
5554
* @since 1.1.0
5655
*/
5756
@Configuration
@@ -87,9 +86,6 @@ public void checkChangelogExists() {
8786
+ " (please add changelog or check your Liquibase "
8887
+ "configuration)");
8988
}
90-
ServiceLocator serviceLocator = ServiceLocator.getInstance();
91-
serviceLocator.addPackageToScan(
92-
CommonsLoggingLiquibaseLogger.class.getPackage().getName());
9389
}
9490

9591
@Bean

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,33 +29,42 @@
2929
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
3131
import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
32+
import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener;
3233
import org.springframework.boot.test.EnvironmentTestUtils;
34+
import org.springframework.boot.test.OutputCapture;
3335
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3436
import org.springframework.test.util.ReflectionTestUtils;
3537

3638
import static org.hamcrest.Matchers.instanceOf;
39+
import static org.hamcrest.Matchers.not;
3740
import static org.junit.Assert.assertEquals;
3841
import static org.junit.Assert.assertFalse;
3942
import static org.junit.Assert.assertNull;
4043
import static org.junit.Assert.assertThat;
4144
import static org.junit.Assert.assertTrue;
45+
import static org.mockito.Matchers.contains;
4246

4347
/**
4448
* Tests for {@link LiquibaseAutoConfiguration}.
4549
*
4650
* @author Marcel Overdijk
51+
* @author Andy Wilkinson
4752
*/
4853
public class LiquibaseAutoConfigurationTests {
4954

5055
@Rule
5156
public ExpectedException expected = ExpectedException.none();
5257

58+
@Rule
59+
public OutputCapture outputCapture = new OutputCapture();
60+
5361
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5462

5563
@Before
5664
public void init() {
5765
EnvironmentTestUtils.addEnvironment(this.context,
5866
"spring.datasource.name:liquibasetest");
67+
new LiquibaseServiceLocatorApplicationListener().onApplicationEvent(null);
5968
}
6069

6170
@After
@@ -166,7 +175,8 @@ public void testLogger() throws Exception {
166175
this.context.refresh();
167176
SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class);
168177
Object log = ReflectionTestUtils.getField(liquibase, "log");
169-
assertThat(log, instanceOf(CommonsLoggingLiquibaseLogger.class));
178+
assertThat(log, instanceOf((CommonsLoggingLiquibaseLogger.class)));
179+
assertThat(this.outputCapture.toString(), not(contains(": liquibase:")));
170180
}
171181

172182
@Test

spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,8 +51,12 @@ public void onApplicationEvent(ApplicationStartedEvent event) {
5151
private static class LiquibasePresent {
5252

5353
public void replaceServiceLocator() {
54-
ServiceLocator.setInstance(new CustomResolverServiceLocator(
55-
new SpringPackageScanClassResolver(logger)));
54+
CustomResolverServiceLocator customResolverServiceLocator = new CustomResolverServiceLocator(
55+
new SpringPackageScanClassResolver(logger));
56+
customResolverServiceLocator.addPackageToScan(
57+
CommonsLoggingLiquibaseLogger.class.getPackage().getName());
58+
ServiceLocator.setInstance(customResolverServiceLocator);
59+
liquibase.logging.LogFactory.reset();
5660
}
5761

5862
}

0 commit comments

Comments
 (0)