
Flutter — Logging with logger
Time to add some logging, I want to be able to review network requests and key actions that take place, to give some feedback and validation now that the interactions between the application and backend are getting more complicated.
I used the popularity on pub.dev and Flutter Gems to choose the logger package.


All I want at the moment is to log to console, we can switch out or extend later, quick article no messing…
Ta Da
Now we can add logging statements to our projects:
ref.read(logWriterProvider).debug("Debug message");
ref.read(logWriterProvider).info("Information message");
ref.read(logWriterProvider).warn("Warning message");
ref.read(logWriterProvider).error("Error message");
XP
Implementation
I added the logging functionality to
https://gitlab.com/simbu-mobile/simbucore
It has a provider to wire-up and inject logging where needed.
final logWriterProvider = Provider<LogWriter>(
(ref) => PrettyPrintLogWriter(),
);The provider uses an abstract class to support multiple implementations starting with one that prints to the console using the logger package:
import 'package:simbucore/app/logging/service/log_writer.dart';
import 'package:logger/logger.dart';
// Prints pretty colourful log statements to the console
class PrettyPrintLogWriter implements LogWriter {
late Logger logger;
PrettyPrintLogWriter() {
logger = Logger(
printer: PrettyPrinter(methodCount: 0),
);
}
@override
void debug(message) {
logger.d(message);
}
@override
void error(message, StackTrace? stackTrace) {
logger.e(message, stackTrace);
}
@override
void fatal(message, StackTrace? stackTrace) {
logger.wtf(message, stackTrace);
}
@override
void info(message) {
logger.i(message);
}
@override
void warn(message) {
logger.w(message);
}
}That will do for now, we can evolve the underlying services when we have further logging requirements.
Back Burner
Logging of unhandled exceptions, I may be offload it to a service like Crashlytics.
Sound & Vision






