Flutter log exceptions and errors using the logger package

When building Flutter applications, effective logging can be highly beneficial. Logging helps you identify and resolve issues more easily during development. Instead of implementing logging from scratch, we can use the logger package. The package serves as a helpful tool, simplifying logging tasks, including errors. This post will guide you through adding the logger package to your Flutter project and teach you different ways to use it in different situations.

Installing

To get started, we need to install the logger package into our project. The installation process is simple. Just execute the following command: flutter pub add logger.

Once the command is executed, make sure to check your pubspec.yaml file for the added dependencies. You should see the logger package included in the dependencies section, like this:

dependencies:
  flutter:
    sdk: flutter
  logger: ^2.0.1

Implementation

With the logger package, you can use the default logger or make your own. Let us start with the default logger:

Default Logger

The default logger provides a quick and simple way to begin logging in your application. Here is how to set up and use it:

import 'package:logger/logger.dart';

void main() {
  Logger logger = Logger();

  logger.i('I created a default logger instance');
}

In this code snippet, we imported the logger package and created a logger variable that is an instance of the Logger class. Now we can call the i function on the logger variable to log an informational message as demonstrated in the GIF below:

Modified Logger

For more customization and control over your logs, you can pass properties to the Logger instance. This allows you to adjust the filtering, log level, output, and formatting:

import 'package:logger/logger.dart';

void main() {
  Logger modifiedLogger = Logger(
    filter: DevelopmentFilter(),
    level: Level.all,
    output: ConsoleOutput(),
    printer: PrettyPrinter(methodCount: 10),
  );

  modifiedLogger.i('I created a custom logger instance');
}

In this code snippet, we added all available properties to our Logger instance.

  • filter: DevelopmentFilter(): This filter ensures that logs are only shown during development. You can change this for production if needed.
  • level: Level.all: The logger displays logs of all levels. If you use only level.info for example, the logger will only show informational messages.
  • output: ConsoleOutput(): The logs appear in the console. You can choose other options, like saving logs to a file using the FileOutput class.
  • printer: PrettyPrinter(methodCount: 10): The PrettyPrinter formats the logs. Although PrettyPrinter has many settings, here we only use methodCount to show up to 10 methods in the log’s call stack.”

Regular Logging Functions

The functions below accept text as input and are used to display regular logs. Each function has a different color for the message.

import 'package:logger/logger.dart';

void main() {
  Logger logger = Logger();

  logger.t("Trace log");
  logger.d("Debug log");
  logger.i("Info log");
  logger.w("Warning log");
}

In this code, we have used the four available functions (t, d, i, and w) to show different types of regular log messages.

The functions for error-related logs accept an error, and the f function also takes a stack trace.

import 'package:logger/logger.dart';

void main() {
  Logger logger = Logger();
  
  try {
    throw 'Something went wrong';
  } catch (error, stackTrace) {
    logger.e("Error log", error: error);
    logger.f("Fatal log", error: error, stackTrace: stackTrace);
  }
}

In this code snippet, we have used the two available functions (e and f) to display log messages related to errors.

When Should You Use Loggers?

You should use loggers whenever you want to keep track of what is happening in your code. They are like a diary for your application that helps you in different ways:

  1. Understanding: Loggers show how your application works, especially when things are not going well.
  2. Fixing Issues: When your application has problems, loggers can show you where they are.
  3. Building and Testing: Loggers help you see what your application is doing while you are making and testing it.
  4. Checking Speed: You can use loggers to see how fast your app runs and where it slows down.
  5. Finding Clues: Loggers are like hints that help you find and fix problems in your code.
  6. Following Steps: They show the path your application takes, helping you see how different parts connect.

In short, loggers help you understand, fix, and improve your application by keeping a record of what is going on.

Conclusion

Using the logger package in your Flutter application development can make your coding experience better. It helps you understand and fix problems in your application, which makes your work easier and your application better. Think of loggers as friendly guides that show you how your code works, whether you are testing, fixing issues, or making things faster. So, try out the logger package to learn more about your application and improve your coding.

Tijn van den Eijnde
Tijn van den Eijnde
Articles: 90

Leave a Reply

Your email address will not be published. Required fields are marked *