avatarsimbu

Summary

The web content discusses the implementation of logging in a Flutter application using the popular logger package, detailing the process of setting up a logging system and considering future enhancements.

Abstract

The article outlines the author's approach to integrating a logging system into their Flutter application to monitor network requests and key actions. The author chose the logger package based on its popularity on pub.dev and Flutter Gems. The implementation focuses on console logging for the moment, with the intention to extend or switch out the logging method in the future. The logging functionality has been added to a shared core repository, simbucore, which includes a provider for injecting logging capabilities where needed. A PrettyPrintLogWriter class is introduced to print colorful log statements to the console, leveraging the logger package's PrettyPrinter. The author acknowledges the potential for evolving the logging system, mentioning the possibility of using services like Crashlytics for logging unhandled exceptions.

Opinions

  • The author values the importance of logging in application development for monitoring and debugging purposes.
  • The logger package is favored for its popularity and presumed reliability within the Flutter community.
  • The initial implementation is intentionally straightforward, focusing on console output, with a clear understanding that more sophisticated logging strategies will be necessary as the application grows.
  • The author emphasizes the flexibility of their logging implementation, which allows for multiple logging strategies through the use of an abstract class and provider pattern.
  • There is an openness to adopting third-party services like Crashlytics for advanced logging needs, such as capturing unhandled exceptions.
  • The article concludes with a recommendation for an AI service, suggesting it as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating the author's endorsement of this service.
Join Medium to view all my articles.

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.

Popular logging package on pub.dev
Popular on Flutter Gems too.

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");
Console output

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

Links

Flutter
Development
Programming
Mobile
Mobile App Development
Recommended from ReadMedium