Skip to content

Commit 89e5d4c

Browse files
authored
Merge pull request #148 from avaje/feature/builder-logging
Improve Builder logging for loading resources and files - followup for #141
2 parents aab7ad5 + 23b8dd0 commit 89e5d4c

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

avaje-config/src/main/java/io/avaje/config/CoreConfigurationBuilder.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,48 @@
1111
import java.util.ServiceLoader;
1212
import java.util.stream.Collectors;
1313

14+
import static java.lang.System.Logger.Level.DEBUG;
15+
import static java.lang.System.Logger.Level.INFO;
1416
import static java.util.Objects.requireNonNull;
1517

1618
@NonNullApi
1719
final class CoreConfigurationBuilder implements Configuration.Builder {
1820

21+
private ConfigurationLog log = initialiseLog();
1922
private final Parsers parsers = new Parsers();
2023
private final CoreEntry.CoreMap sourceMap = CoreEntry.newMap();
2124
private ResourceLoader resourceLoader = initialiseResourceLoader();
2225
private ModificationEventRunner eventRunner;
23-
private ConfigurationLog configurationLog;
2426
private boolean includeResourceLoading;
2527
private InitialLoader initialLoader;
2628

29+
private static ConfigurationLog initialiseLog() {
30+
return ServiceLoader.load(ConfigurationLog.class)
31+
.findFirst()
32+
.orElseGet(DefaultConfigurationLog::new);
33+
}
34+
2735
@Override
2836
public Configuration.Builder eventRunner(ModificationEventRunner eventRunner) {
29-
this.eventRunner = eventRunner;
37+
this.eventRunner = requireNonNull(eventRunner);
3038
return this;
3139
}
3240

3341
@Override
34-
public Configuration.Builder log(ConfigurationLog configurationLog) {
35-
this.configurationLog = configurationLog;
42+
public Configuration.Builder log(ConfigurationLog log) {
43+
this.log = requireNonNull(log);
3644
return this;
3745
}
3846

3947
@Override
4048
public Configuration.Builder resourceLoader(ResourceLoader resourceLoader) {
41-
this.resourceLoader = resourceLoader;
49+
this.resourceLoader = requireNonNull(resourceLoader);
4250
return this;
4351
}
4452

4553
@Override
4654
public Configuration.Builder put(String key, String value) {
47-
requireNonNull(key);
48-
requireNonNull(value);
49-
sourceMap.put(key, value, "initial");
55+
sourceMap.put(requireNonNull(key), requireNonNull(value), "initial");
5056
return this;
5157
}
5258

@@ -77,9 +83,12 @@ public Configuration.Builder load(String resource) {
7783
final var configParser = parser(resource);
7884
try {
7985
try (var inputStream = resourceLoader.getResourceAsStream(resource)) {
80-
if (inputStream != null) {
86+
if (inputStream == null) {
87+
log.log(INFO, "Configuration resource:{0} not found", resource);
88+
} else {
8189
var source = "resource:" + resource;
8290
configParser.load(inputStream).forEach((k, v) -> sourceMap.put(k, v, source));
91+
log.log(DEBUG, "loaded {0}", source);
8392
}
8493
return this;
8594
}
@@ -91,13 +100,15 @@ public Configuration.Builder load(String resource) {
91100
@Override
92101
public Configuration.Builder load(File file) {
93102
if (!file.exists()) {
103+
log.log(INFO, "Configuration file:{0} not found", file);
94104
return this;
95105
}
96106
final var configParser = parser(file.getName());
97107
try {
98108
try (var reader = new FileReader(file)) {
99109
var source = "file:" + file.getName();
100110
configParser.load(reader).forEach((k, v) -> sourceMap.put(k, v, source));
111+
log.log(DEBUG, "loaded {0}", source);
101112
return this;
102113
}
103114
} catch (IOException e) {
@@ -127,7 +138,6 @@ public Configuration.Builder includeResourceLoading() {
127138
@Override
128139
public Configuration build() {
129140
final var runner = initRunner();
130-
final var log = initLog();
131141
final var sources = ServiceLoader.load(ConfigurationSource.class).stream()
132142
.map(ServiceLoader.Provider::get)
133143
.collect(Collectors.toList());
@@ -159,15 +169,6 @@ private static ResourceLoader initialiseResourceLoader() {
159169
.orElseGet(DefaultResourceLoader::new);
160170
}
161171

162-
private ConfigurationLog initLog() {
163-
if (configurationLog == null) {
164-
configurationLog = ServiceLoader.load(ConfigurationLog.class)
165-
.findFirst()
166-
.orElseGet(DefaultConfigurationLog::new);
167-
}
168-
return configurationLog;
169-
}
170-
171172
private ModificationEventRunner initRunner() {
172173
if (eventRunner == null) {
173174
eventRunner = ServiceLoader.load(ModificationEventRunner.class)

avaje-config/src/main/java/io/avaje/config/InitialLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99
import java.io.UncheckedIOException;
10-
import java.lang.System.Logger.Level;
1110
import java.util.*;
1211
import java.util.regex.Pattern;
1312

1413
import static io.avaje.config.InitialLoader.Source.FILE;
1514
import static io.avaje.config.InitialLoader.Source.RESOURCE;
15+
import static java.lang.System.Logger.Level.WARNING;
1616

1717
/**
1818
* Loads the configuration from known/expected locations.
@@ -179,7 +179,7 @@ private boolean loadTest() {
179179
int before = loadContext.size();
180180
load("application-test", RESOURCE);
181181
if (loadProperties("test-ebean.properties", RESOURCE)) {
182-
log.log(Level.WARNING, "Loading properties from test-ebean.properties is deprecated. Please migrate to application-test.yaml or application-test.properties instead.");
182+
log.log(WARNING, "Loading properties from test-ebean.properties is deprecated. Please migrate to application-test.yaml or application-test.properties instead.");
183183
}
184184
return loadContext.size() > before;
185185
}
@@ -235,7 +235,7 @@ String[] splitPaths(String location) {
235235
private void loadMain(Source source) {
236236
load("application", source);
237237
if (loadProperties("ebean.properties", source)) {
238-
log.log(Level.WARNING, "Loading properties from ebean.properties is deprecated. Please migrate to use application.yaml or application.properties instead.");
238+
log.log(WARNING, "Loading properties from ebean.properties is deprecated. Please migrate to use application.yaml or application.properties instead.");
239239
}
240240
}
241241

@@ -244,7 +244,7 @@ private void loadViaSystemProperty() {
244244
if (fileName == null) {
245245
fileName = System.getProperty("props.file");
246246
if (fileName != null && !loadWithExtensionCheck(fileName)) {
247-
log.log(Level.WARNING, "Unable to find file {0} to load properties", fileName);
247+
log.log(WARNING, "Unable to find file {0} to load properties", fileName);
248248
}
249249
}
250250
}
@@ -296,10 +296,10 @@ private boolean loadCustom(String resourcePath, Source source) {
296296
boolean loadCustomExtension(String resourcePath, ConfigParser parser, Source source) {
297297
try (InputStream is = resource(resourcePath, source)) {
298298
if (is != null) {
299-
parser.load(is).forEach((k, v) -> loadContext.put(k, v, (source == RESOURCE ? "resource:" : "file:") + resourcePath));
299+
var sourceName = (source == RESOURCE ? "resource:" : "file:") + resourcePath;
300+
parser.load(is).forEach((k, v) -> loadContext.put(k, v, sourceName));
300301
return true;
301302
}
302-
303303
} catch (Exception e) {
304304
throw new IllegalStateException("Error loading properties - " + resourcePath, e);
305305
}

avaje-config/src/test/java/io/avaje/config/CoreConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ void builder() {
172172
.put("myExtraOne", "baz")
173173
.load(fileSource)
174174
.load("hi.properties")
175+
.load("i-dont-exist.properties")
176+
.load(new File("i-dont-exist-file.properties"))
175177
.build();
176178

177179
assertEquals(conf.get("a"), "1");

0 commit comments

Comments
 (0)