LoggerConfig class
Configuration of logger levels.
Logger levels extracted from a config. Used to set up loggers from the logging package, with levels obtained from a config.
Using this class involves:
- Using the LoggerConfig constructor (or the optional convenience method) to create a LoggerConfig from a config map; and
- Invoking the applyLevels method to use the levels in it.
Expected config
A config map needs to contain keys which are used as logger names and their values are used as levels (either strings or integers).
For example,
logger:
foo: FINER
foo.bar: FINE
baz: INFO
baz.special: 150
Details
Since logs are usually optional, it is usually represented as an optional config map. If the config map exists, use the LoggerConfig constructor to extract the levels from it.
final loggerMap = cfg.map('logger', optional: true);
if (loggerMap != null) {
final levels = LoggerConfig(loggerMap);
...
}
The optional convenience method incorporates the check for an optional config map. So the above code can be reduced to:
final levels = LoggerConfig.optional(cfg);
if (levels != null {
...
}
The levels can be set using the applyLevels method.
A more complete example:
import 'package:logging/logging.dart';
...
/// Pass in the top-level config map
void setupLogging(ConfigMap map) {
final levels = LoggerConfig.optional(map); // default key = 'logger'
if (levels != null) {
hierarchicalLoggingEnabled = true;
Logger.root.onRecord.listen((LogRecord r) {
final t = r.time.toUtc();
stdout.write('$t: ${r.loggerName}: ${r.level.name}: ${r.message}\n');
});
Logger.root.level = Level.OFF;
levels.applyLevels(); // setup levels specified in the config
}
}
Constructors
- LoggerConfig(ConfigMap map)
- Constructs a collection of logger levels.
Properties
Methods
-
applyLevels(
) → void - Sets the level for the named loggers.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
optional(
ConfigMap parentMap, {String key = recommendedKey}) → LoggerConfig? - Convenience method to create a LoggerConfig from an optional config map.
Constants
- recommendedKey → const String
- Recommended key for the config map containing the levels.