applyLoggingLevels static method
Sets the levels of SpringLog from the logging.level.* and
logging.group.* properties of environment.
run calls it once the config files are read, with or without a
logging starter, so applications only write the properties. Call it
yourself only when you prepare an environment without
SpringApplication:
logging.level.root=info
logging.level.neon.checkout=debug
logging.group.payments=neon.payments,neon.checkout
logging.level.payments=trace
A level applies to the logger and every logger below it
(neon.checkout.cart); a group sets the level of each logger it
lists. The environment variable LOGGING_LEVEL_NEON_CHECKOUT=debug
is the same as logging.level.neon.checkout; false (what YAML
makes of an unquoted off) means off; an empty value leaves the
level inherited. The levels set by an earlier call are cleared first.
A value that is no level throws a
ConfigurationPropertiesBindException, which fails the start.
Implementation
static void applyLoggingLevels(ConfigurableEnvironment environment) {
// Bound like Boot's LoggingApplicationListener: groups as lists (a
// comma list or `logging.group.x[0]`), levels as a map whose values
// must be log levels (anything else fails the start).
final binder = Binder.get(environment);
final groups = <String, List<String>>{
...predefinedLoggerGroups,
...binder
.bind(
'logging.group',
const MapType<String, List<String>>(
ClassType<String>(),
ListType<String>(ClassType<String>()),
),
)
.orElse(const {}),
};
final values = binder
.bind(
'logging.level',
const MapType<String, String>(
ClassType<String>(),
ClassType<String>(),
),
)
.orElse(const {});
final levels = <String, SpringLogLevel>{};
final invalid = <String>[];
for (final MapEntry(key: logger, :value) in values.entries) {
if (value.trim().isEmpty) {
continue; // no level: inherited
}
final level = _parseLevel(value);
if (level == null) {
invalid.add(
'logging.level.$logger: failed to convert to LogLevel (was '
'"$value"; use trace, debug, info, warn, error, fatal or off)',
);
} else {
levels.putIfAbsent(logger, () => level);
}
}
final scalar = environment.getProperty('logging.level');
if (scalar != null &&
scalar.trim().isNotEmpty &&
_parseLevel(scalar) == null) {
invalid.add(
'logging.level: failed to convert to LogLevel (was "$scalar")',
);
}
if (invalid.isNotEmpty) {
throw ConfigurationPropertiesBindException(
'logging.level',
null,
invalid,
);
}
// `root` in any case, like Spring Boot; the first (highest) source's.
final rootKey = levels.keys
.where((name) => name.toLowerCase() == 'root')
.firstOrNull;
final root = rootKey != null ? levels[rootKey] : _parseLevel(scalar);
levels.removeWhere((name, _) => name.toLowerCase() == 'root');
if (root != null) {
SpringLog.level = root;
}
SpringLog.resetLevels();
// `--debug` and `--trace` come first: the levels of the properties win,
// as in Boot's LoggingApplicationListener.
final debug = bindBootProperties(environment, DebugProperties());
for (final (value, level, loggers) in [
(debug.debug, SpringLogLevel.debug, debugLoggers),
(debug.trace, SpringLogLevel.trace, traceLoggers),
]) {
if (_isSet(value)) {
for (final name in loggers) {
SpringLog.setLevel(name, level);
}
}
}
for (final MapEntry(key: logger, value: level) in levels.entries) {
for (final name in groups[logger] ?? [logger]) {
if (name.trim().isNotEmpty) {
SpringLog.setLevel(name.trim(), level);
}
}
}
}