Skip to content

Commit f6744be

Browse files
committed
Better logging, fix jansi loading on unsupported platforms
1 parent 54d6baa commit f6744be

File tree

27 files changed

+156
-60
lines changed

27 files changed

+156
-60
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Version 3.8.4 (2019-03-12)
2+
3+
* [fix] Fix Jansi loading on unsupported platforms.
4+
* [chg] Better logging of detected configuration resources.
5+
* [chg] Allow configuration through `seedstack.config.*` system properties to override any other configuration source.
6+
17
# Version 3.8.3 (2019-03-08)
28

39
* [chg] JSON home resource is now disabled by default.

cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.8.3-SNAPSHOT</version>
17+
<version>3.8.4-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-cli</artifactId>

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.8.3-SNAPSHOT</version>
17+
<version>3.8.4-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-core</artifactId>

core/src/main/java/org/seedstack/seed/core/internal/configuration/ConfigurationPlugin.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal.configuration;
910

1011
import com.google.common.collect.Sets;
@@ -21,7 +22,6 @@
2122
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Map;
24-
import java.util.Properties;
2525
import java.util.Set;
2626
import java.util.stream.Collectors;
2727
import org.seedstack.coffig.Coffig;
@@ -47,7 +47,7 @@
4747
*/
4848
public class ConfigurationPlugin extends AbstractPlugin implements ApplicationProvider {
4949
public static final String NAME = "config";
50-
private static final String EXTERNAL_CONFIG_PREFIX = "seedstack.config.";
50+
private static final String KERNEL_PARAM_CONFIG_PREFIX = "seedstack.config.";
5151
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationPlugin.class);
5252
private static final String CONFIGURATION_PACKAGE = "META-INF.configuration";
5353
private static final String CONFIGURATION_LOCATION = "META-INF/configuration/";
@@ -76,8 +76,14 @@ public void provideContainerContext(Object containerContext) {
7676
@Override
7777
public String pluginPackageRoot() {
7878
ApplicationConfig applicationConfig = coffig.get(ApplicationConfig.class);
79-
if (applicationConfig.getBasePackages().isEmpty() && applicationConfig.isPackageScanWarning()) {
80-
LOGGER.warn("No base package configured, only classes in 'org.seedstack.*' packages will be scanned");
79+
if (applicationConfig.getBasePackages().isEmpty()) {
80+
if (applicationConfig.isPackageScanWarning()) {
81+
LOGGER.warn("No application base package configured, only classes in 'org.seedstack.*' packages will " +
82+
"be scanned");
83+
}
84+
} else {
85+
LOGGER.info("Application base package(s) to be scanned: " + String.join(", ",
86+
applicationConfig.getBasePackages()));
8187
}
8288
Set<String> basePackages = new HashSet<>(applicationConfig.getBasePackages());
8389
basePackages.add(CONFIGURATION_PACKAGE);
@@ -97,7 +103,6 @@ public Collection<ClasspathScanRequest> classpathScanRequests() {
97103
@SuppressWarnings("unchecked")
98104
@Override
99105
public InitState init(InitContext initContext) {
100-
detectSystemPropertiesConfig();
101106
detectKernelParamConfig(initContext);
102107
detectConfigurationFiles(initContext);
103108

@@ -128,46 +133,37 @@ public void stop() {
128133
private void detectKernelParamConfig(InitContext initContext) {
129134
InMemoryProvider kernelParamConfigProvider = new InMemoryProvider();
130135

136+
int count = 0;
131137
for (Map.Entry<String, String> kernelParam : initContext.kernelParams().entrySet()) {
132-
if (kernelParam.getKey().startsWith(EXTERNAL_CONFIG_PREFIX)) {
138+
if (kernelParam.getKey().startsWith(KERNEL_PARAM_CONFIG_PREFIX)) {
133139
addValue(kernelParamConfigProvider, kernelParam.getKey(), kernelParam.getValue());
140+
count++;
134141
}
135142
}
136143

144+
if (count > 0) {
145+
LOGGER.info("Detected {} configuration value(s) through kernel parameters, enable debug-level logging to " +
146+
"see them", count);
147+
}
148+
137149
seedRuntime.registerConfigurationProvider(
138150
kernelParamConfigProvider,
139151
ConfigurationPriority.KERNEL_PARAMETERS_CONFIG
140152
);
141153
}
142154

143155
private void addValue(InMemoryProvider inMemoryProvider, String key, String value) {
144-
String choppedKey = key.substring(EXTERNAL_CONFIG_PREFIX.length());
156+
String choppedKey = key.substring(KERNEL_PARAM_CONFIG_PREFIX.length());
145157
if (value.contains(",")) {
146158
String[] values = Arrays.stream(value.split(",")).map(String::trim).toArray(String[]::new);
147-
LOGGER.debug("External array configuration property: {}={}", choppedKey, Arrays.toString(values));
159+
LOGGER.debug("Kernel parameter array config: {}={}", choppedKey, Arrays.toString(values));
148160
inMemoryProvider.put(choppedKey, values);
149161
} else {
150-
LOGGER.debug("External configuration property: {}={}", choppedKey, value);
162+
LOGGER.debug("Kernel parameter config: {}={}", choppedKey, value);
151163
inMemoryProvider.put(choppedKey, value);
152164
}
153165
}
154166

155-
private void detectSystemPropertiesConfig() {
156-
InMemoryProvider systemPropertiesProvider = new InMemoryProvider();
157-
158-
Properties systemProperties = System.getProperties();
159-
for (String systemProperty : systemProperties.stringPropertyNames()) {
160-
if (systemProperty.startsWith(EXTERNAL_CONFIG_PREFIX)) {
161-
addValue(systemPropertiesProvider, systemProperty, systemProperties.getProperty(systemProperty));
162-
}
163-
}
164-
165-
seedRuntime.registerConfigurationProvider(
166-
systemPropertiesProvider,
167-
ConfigurationPriority.SYSTEM_PROPERTIES_CONFIG
168-
);
169-
}
170-
171167
private void detectConfigurationFiles(InitContext initContext) {
172168
JacksonProvider jacksonProvider = new JacksonProvider();
173169
JacksonProvider jacksonOverrideProvider = new JacksonProvider();
@@ -182,7 +178,7 @@ private void detectConfigurationFiles(InitContext initContext) {
182178
URL url = urlEnumeration.nextElement();
183179

184180
if (isOverrideResource(configurationResource)) {
185-
LOGGER.debug("Detected override configuration resource: {}", url.toExternalForm());
181+
LOGGER.info("Detected override configuration resource: {}", url.toExternalForm());
186182

187183
if (isJacksonResource(configurationResource)) {
188184
jacksonOverrideProvider.addSource(url);
@@ -192,7 +188,7 @@ private void detectConfigurationFiles(InitContext initContext) {
192188
LOGGER.warn("Unrecognized override configuration resource: {}", url.toExternalForm());
193189
}
194190
} else {
195-
LOGGER.debug("Detected configuration resource: {}", url.toExternalForm());
191+
LOGGER.info("Detected configuration resource: {}", url.toExternalForm());
196192

197193
if (isJacksonResource(configurationResource)) {
198194
jacksonProvider.addSource(url);

core/src/main/java/org/seedstack/seed/core/internal/init/BaseConfigurationFactory.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import java.io.IOException;
1212
import java.net.URL;
1313
import java.util.ArrayList;
14+
import java.util.Arrays;
1415
import java.util.Enumeration;
1516
import java.util.List;
17+
import java.util.Properties;
1618
import javax.validation.ValidatorFactory;
1719
import org.seedstack.coffig.Coffig;
1820
import org.seedstack.coffig.CoffigBuilder;
1921
import org.seedstack.coffig.provider.EnvironmentProvider;
22+
import org.seedstack.coffig.provider.InMemoryProvider;
2023
import org.seedstack.coffig.provider.JacksonProvider;
2124
import org.seedstack.coffig.provider.PrefixProvider;
2225
import org.seedstack.coffig.provider.PropertiesProvider;
@@ -32,6 +35,7 @@ public class BaseConfigurationFactory {
3235
private static final Logger LOGGER = LoggerFactory.getLogger(BaseConfigurationFactory.class);
3336
private static final String ENVIRONMENT_VARIABLES_PREFIX = "env";
3437
private static final String SYSTEM_PROPERTIES_PREFIX = "sys";
38+
private static final String SYSTEM_PROPERTIES_CONFIG_PREFIX = "seedstack.config.";
3539
private final ValidatorFactory validatorFactory;
3640

3741
private BaseConfigurationFactory() {
@@ -63,6 +67,10 @@ public Coffig create() {
6367
buildPropertiesProvider("application.override.properties"),
6468
ConfigurationPriority.BASE_OVERRIDE
6569
)
70+
.registerProvider(
71+
detectSystemPropertiesConfig(),
72+
ConfigurationPriority.SYSTEM_PROPERTIES_CONFIG
73+
)
6674
.registerProvider(
6775
new PrefixProvider<>(ENVIRONMENT_VARIABLES_PREFIX, new EnvironmentProvider()),
6876
ConfigurationPriority.ENVIRONMENT_VARIABLES
@@ -110,7 +118,7 @@ private List<URL> getResources(String... resourceNames) {
110118
resourceName);
111119
while (configResources.hasMoreElements()) {
112120
URL url = configResources.nextElement();
113-
LOGGER.debug("Detected configuration resource: {}", url.toExternalForm());
121+
LOGGER.info("Detected configuration resource: {}", url.toExternalForm());
114122
result.add(url);
115123
}
116124
} catch (IOException e) {
@@ -120,6 +128,38 @@ private List<URL> getResources(String... resourceNames) {
120128
return result;
121129
}
122130

131+
private InMemoryProvider detectSystemPropertiesConfig() {
132+
InMemoryProvider systemPropertiesProvider = new InMemoryProvider();
133+
134+
Properties systemProperties = System.getProperties();
135+
int count = 0;
136+
for (String systemProperty : systemProperties.stringPropertyNames()) {
137+
if (systemProperty.startsWith(SYSTEM_PROPERTIES_CONFIG_PREFIX)) {
138+
addValue(systemPropertiesProvider, systemProperty, systemProperties.getProperty(systemProperty));
139+
count++;
140+
}
141+
}
142+
143+
if (count > 0) {
144+
LOGGER.info("Detected {} configuration value(s) through system properties, enable debug-level logging to " +
145+
"see them", count);
146+
}
147+
148+
return systemPropertiesProvider;
149+
}
150+
151+
private void addValue(InMemoryProvider inMemoryProvider, String key, String value) {
152+
String choppedKey = key.substring(SYSTEM_PROPERTIES_CONFIG_PREFIX.length());
153+
if (value.contains(",")) {
154+
String[] values = Arrays.stream(value.split(",")).map(String::trim).toArray(String[]::new);
155+
LOGGER.debug("System property array config: {}={}", choppedKey, Arrays.toString(values));
156+
inMemoryProvider.put(choppedKey, values);
157+
} else {
158+
LOGGER.debug("System property config: {}={}", choppedKey, value);
159+
inMemoryProvider.put(choppedKey, value);
160+
}
161+
}
162+
123163
private static class Holder {
124164
private static final BaseConfigurationFactory INSTANCE = new BaseConfigurationFactory();
125165
}

core/src/main/java/org/seedstack/seed/core/internal/init/ConsoleManager.java

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
import org.fusesource.jansi.AnsiConsole;
1313
import org.fusesource.jansi.AnsiPrintStream;
1414
import org.fusesource.jansi.FilterPrintStream;
15+
import org.fusesource.jansi.WindowsAnsiPrintStream;
1516
import org.fusesource.jansi.internal.CLibrary;
1617
import org.seedstack.seed.ApplicationConfig;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1720

1821
public class ConsoleManager {
22+
private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleManager.class);
1923
private final PrintStream savedOut = System.out;
2024
private final PrintStream savedErr = System.err;
2125

@@ -28,41 +32,83 @@ public static ConsoleManager get() {
2832
}
2933

3034
public synchronized void install(ApplicationConfig.ColorOutput colorOutput) {
31-
System.setOut(wrapPrintStream(System.out, CLibrary.STDOUT_FILENO, colorOutput));
32-
System.setErr(wrapPrintStream(System.err, CLibrary.STDERR_FILENO, colorOutput));
35+
System.setOut(wrapPrintStream(System.out, FileNo.STDOUT, colorOutput, true));
36+
System.setErr(wrapPrintStream(System.err, FileNo.STDERR, colorOutput, false));
3337
}
3438

3539
public synchronized void uninstall() {
3640
System.setOut(savedOut);
3741
System.setErr(savedErr);
3842
}
3943

40-
private PrintStream wrapPrintStream(final PrintStream inputStream, int fileno,
41-
ApplicationConfig.ColorOutput colorOutput) {
44+
private PrintStream wrapPrintStream(final PrintStream inputStream, FileNo fileno,
45+
ApplicationConfig.ColorOutput colorOutput, boolean log) {
4246
try {
43-
if (colorOutput == ApplicationConfig.ColorOutput.PASSTHROUGH || Boolean.getBoolean("jansi.passthrough")) {
47+
if (colorOutput == ApplicationConfig.ColorOutput.PASSTHROUGH) {
48+
if (log) {
49+
LOGGER.info("Color output passthrough: leaving stdout and stderr untouched");
50+
}
4451
return inputStream;
45-
} else if (colorOutput == ApplicationConfig.ColorOutput.DISABLE || Boolean.getBoolean("jansi.strip")) {
52+
} else if (colorOutput == ApplicationConfig.ColorOutput.DISABLE) {
53+
if (log) {
54+
LOGGER.info("Color output disabled: stripping stdout and stderr of ANSI codes");
55+
}
4656
return strippedOutput(inputStream);
4757
} else if (colorOutput == ApplicationConfig.ColorOutput.ENABLE) {
58+
if (log) {
59+
LOGGER.info("Color output enabled: allowing ANSI codes on stdout and stderr");
60+
}
4861
return ansiOutput(inputStream);
4962
} else if (colorOutput == ApplicationConfig.ColorOutput.AUTODETECT) {
5063
if (isXtermColor()) {
5164
// enable color in recognized XTERM color modes
65+
if (log) {
66+
LOGGER.info("XTERM detected: allowing ANSI codes on stdout and stderr");
67+
}
5268
return ansiOutput(inputStream);
5369
} else if (isIntelliJ()) {
5470
// enable color under Intellij
71+
if (log) {
72+
LOGGER.info("IntelliJ detected: allowing ANSI codes on stdout and stderr");
73+
}
5574
return ansiOutput(inputStream);
5675
} else {
5776
// let Jansi handle other detection
58-
return AnsiConsole.wrapPrintStream(inputStream, fileno);
77+
PrintStream result;
78+
switch (fileno) {
79+
case STDOUT:
80+
result = AnsiConsole.wrapPrintStream(inputStream, CLibrary.STDOUT_FILENO);
81+
break;
82+
case STDERR:
83+
result = AnsiConsole.wrapPrintStream(inputStream, CLibrary.STDERR_FILENO);
84+
break;
85+
default:
86+
result = strippedOutput(inputStream);
87+
break;
88+
}
89+
if (log) {
90+
if (result instanceof WindowsAnsiPrintStream) {
91+
LOGGER.info("Windows console detected: using native coloring for stdout and stderr");
92+
} else if (result instanceof AnsiPrintStream) {
93+
LOGGER.info("ANSI not supported: stripping stdout and stderr of ANSI codes");
94+
} else {
95+
LOGGER.info("ANSI supported: allowing ANSI codes on stdout and stderr");
96+
}
97+
}
98+
return result;
5999
}
60100
} else {
61101
// Fallback to stripping ANSI codes
102+
if (log) {
103+
LOGGER.info("Fallback to stripping ANSI codes");
104+
}
62105
return strippedOutput(inputStream);
63106
}
64-
} catch (Throwable e) {
107+
} catch (Throwable e1) {
65108
// If any error occurs, strip ANSI codes
109+
if (log) {
110+
LOGGER.info("Error loading Jansi (unsupported platform ?): stripping stdout and stderr of ANSI codes");
111+
}
66112
return strippedOutput(inputStream);
67113
}
68114
}
@@ -111,4 +157,9 @@ public void close() {
111157
super.close();
112158
}
113159
}
160+
161+
private enum FileNo {
162+
STDOUT,
163+
STDERR
164+
}
114165
}

core/src/main/java/org/seedstack/seed/core/internal/init/LogbackLogManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ class LogbackLogManager implements LogManager {
3636
"%highlight(%-5level) %d{ISO8601} %magenta(%-15thread) %cyan(%-40logger{40}) %msg%n%red(%throwable)";
3737
private static final String DEFAULT_FILE_PATTERN =
3838
"%-5level %d{ISO8601} %-15thread %-40logger{40} %msg%n%throwable";
39+
private static final String LOGGING_INITIAL_LEVEL_PROPERTY = "seedstack.logging.initialLevel";
3940
private final boolean underTomcat = Classes.optional("org.apache.catalina.startup.Catalina").isPresent();
4041
private final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
4142

4243
LogbackLogManager() {
43-
context.getLogger(Logger.ROOT_LOGGER_NAME).setLevel(Level.INFO);
44+
context.getLogger(Logger.ROOT_LOGGER_NAME)
45+
.setLevel(Level.valueOf(System.getProperty(LOGGING_INITIAL_LEVEL_PROPERTY, "INFO")));
4446
}
4547

4648
@Override

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>org.seedstack.seed</groupId>
2121
<artifactId>seed</artifactId>
22-
<version>3.8.3-SNAPSHOT</version>
22+
<version>3.8.4-SNAPSHOT</version>
2323
<packaging>pom</packaging>
2424

2525
<properties>

rest/core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.8.3-SNAPSHOT</version>
17+
<version>3.8.4-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-core</artifactId>

rest/jersey2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.8.3-SNAPSHOT</version>
17+
<version>3.8.4-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-jersey2</artifactId>

0 commit comments

Comments
 (0)