Log levels
- Dart course
- Advanced
- Lesson 62/83
- 5 min
Learn log levels (info, warning, severe).
Log levels
The logging package defines several levels. From high to low: Level.SEVERE (critical), Level.WARNING (warning), Level.INFO (info), Level.FINE (detail). You set the minimum level with Logger.root.level.
Example: Logger.root.level = Level.WARNING;. With this setting only WARNING and above (SEVERE) records are processed; INFO and below are ignored. This is the standard way to reduce noise in production. When debugging you turn everything on with Level.ALL; in live you keep only important records with Level.WARNING.
Level filtering
Logger.root.level sets the minimum threshold. If a record's level is below this threshold it is ignored; it never reaches the handler. This is the effective way to cut unnecessary records.
Example: when Logger.root.level = Level.WARNING; the record log.info('this is ignored') is not processed; log.warning('watch out') and log.severe('critical') are processed. This way in production only warning and critical records are seen; info and detail records do not make noise.
Practice Level-filtered records
Add import 'package:logging/logging.dart';. In main set the threshold with Logger.root.level = Level.WARNING;. Add a handler with Logger.root.onRecord.listen((r) => print('${r.level.name}: ${r.message}'));. Create final log = Logger('uygulama'); and log three records: log.info('this is ignored'), log.warning('watch out'), log.severe('critical').
Starter code
// import the logging package
void main() {
// set root level to WARNING, add handler, log info/warning/severe
}Sample solution
import 'package:logging/logging.dart';
void main() {
Logger.root.level = Level.WARNING;
Logger.root.onRecord.listen((r) => print('${r.level.name}: ${r.message}'));
final log = Logger('uygulama');
log.info('this is ignored');
log.warning('watch out');
log.severe('critical');
}Expected output
WARNING: watch out SEVERE: critical
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Setting Level.INFO
Logger.root.level = ____;
Logger.root.level = Level.INFO;
Explanation: Logger.root.level = Level.INFO sets the minimum level to INFO.
2 Which level has the highest priority?
- INFO
- SEVERE Correct answer
- FINE
- FINEST
Explanation: SEVERE is the highest-priority level.
3 Match the levels
| Term | Meaning |
|---|---|
SEVERE |
Highest |
WARNING |
Warning |
INFO |
Info |
FINE |
Detail |