Guide to Using the Logger Package in Dart
Published on by Flutter News Hub
Log level
You can log with different levels:
logger.t("Trace log"); logger.d("Debug log"); logger.i("Info log"); logger.w("Warning log"); logger.e("Error log", error: 'Test Error'); logger.f("What a fatal log", error: error, stackTrace: stackTrace);
To show only specific log levels, you can set:
Logger.level = Level.warning;
This hides all trace, debug and info log events.
Options
When creating a logger, you can pass some options:
var logger = Logger( filter: null, printer: PrettyPrinter(), output: null, );
If you use the PrettyPrinter, there are more options:
var logger = Logger( printer: PrettyPrinter( methodCount: 2, errorMethodCount: 8, lineLength: 120, colors: true, printEmojis: true, printTime: false ), );
Auto detecting
With the io package you can auto detect the lineLength and colors arguments. Assuming you have imported the io package with import 'dart:io' as io; you can auto detect colors with io.stdout.supportsAnsiEscapes and lineLength with io.stdout.terminalColumns.
You should probably do this unless there's a good reason you don't want to import io, for example when using this library on the web.
LogFilter
The LogFilter decides which log events should be shown and which don't.
The default implementation (DevelopmentFilter) shows all logs with level >= Logger.level while in debug mode (i.e., running dart with --enable-asserts). In release mode all logs are omitted.
You can create your own LogFilter like this:
class MyFilter extends LogFilter { @override bool shouldLog(LogEvent event) { return true; } }
This will show all logs even in release mode. (NOT a good idea)
LogPrinter
The LogPrinter creates and formats the output, which is then sent to the LogOutput.
You can implement your own LogPrinter. This gives you maximum flexibility.
A very basic printer could look like this:
class MyPrinter extends LogPrinter { @override List log(LogEvent event) { return [event.message]; } }
If you created a cool LogPrinter which might be helpful to others, feel free to open a pull request. :)
Colors
Please note that in some cases ANSI escape sequences do not work under macOS. These escape sequences are used to colorize the output. This seems to be related to a Flutter bug that affects iOS builds: https://github.com/flutter/flutter/issues/64491
However, if you are using a JetBrains IDE (Android Studio, IntelliJ, etc.) you can make use of the Grep Console Plugin and the PrefixPrinter decorator to achieve colored logs for any logger:
var logger = Logger( printer: PrefixPrinter(PrettyPrinter(colors: false)) );
LogOutput
LogOutput sends the log lines to the desired destination.
The default implementation (ConsoleOutput) send every line to the system console.
class ConsoleOutput extends LogOutput { @override void output(OutputEvent event) { for (var line in event.lines) { print(line); } } }
Possible future LogOutputs could send to a file, firebase or to Logcat. Feel free to open pull requests.