Levels at run time and appenders
The loggingSystem bean (LoggingSystem.instance) is the logging system behind SpringLog. Inject it to change levels while the application runs, or to add an output of your own.
Why
To look into one part of a running application, you want its debug lines now, without a restart. And some records belong somewhere else as well: errors in a crash reporter, everything in a remote log service. Spring Boot does this with LoggingSystem.setLogLevel and Logback appenders; here both are plain Dart.
Example
// An admin action turns on debug lines for the checkout.
@Service()
class LogLevels {
LogLevels(this.logging);
final LoggingSystem logging;
void debugCheckout() => logging.setLogLevel('neon.checkout', SpringLogLevel.debug);
void reset() => logging.setLogLevel('neon.checkout', null); // inherit again
}
// Errors also go to the crash reporter, with their stack trace.
class CrashReportAppender extends LogAppender {
CrashReportAppender() : super(threshold: SpringLogLevel.error);
@override
void write(SpringLogRecord record) =>
crashlytics.recordError(record.error ?? record.message, record.stackTrace);
}
Future<void> main(List<String> args) async {
LoggingSystem.instance.addAppender(CrashReportAppender());
await runApplicationAsync(args);
}
Levels at run time
| Call | Effect |
|---|---|
setLogLevel('neon', SpringLogLevel.debug) |
The level of neon and every logger below it without a level of its own |
setLogLevel('neon', null) |
Removes the logger's own level, so it inherits again |
setLogLevel('ROOT', SpringLogLevel.warning) |
The root level; ROOT in any case. The root always needs a level |
rootLevel |
The current root level |
This is Spring Boot's LoggingSystem.setLogLevel. There is no actuator loggers endpoint yet: expose a level change through a controller of your own when you need it remotely.
Your own appenders
An appender is one output of the logging system: the console, the file and the buffer are appenders. Extend LogAppender, give it a threshold if it should take fewer records, and add it with LoggingSystem.instance.addAppender.
writeruns synchronously on the logging call, so queue slow work such as a network call.- A failing appender does not stop the others.
- An added appender stays until
removeAppender, also across application restarts (hot reload). - Records logged during the start, before the system was set up, are replayed into added appenders.
In Spring this is a Logback Appender declared in logback-spring.xml; here it is added in code.
Without SpringApplication
Without SpringApplication, configure and install the logging system yourself:
LoggingSystem.instance
..configure(environment) // the outputs: patterns, thresholds, buffer, file
..install(); // from now on SpringLog records come here
SpringApplication.applyLoggingLevels(environment); // logging.level.*, logging.group.*
configure takes a ConfigurableEnvironment, sets up the outputs and the default root level info. A value it cannot read fails with a ConfigurationPropertiesBindException listing the property (a pattern or a charset with a FormatException). Calling it again closes an open file and opens one from the current values.
In Spring (Java)
loggingSystem.setLogLevel("neon.checkout", LogLevel.DEBUG);
The same method. Appenders are classes added in code, not XML.