Skip to main content

Flutter logger — The complete crash course

Flutter logger — The complete crash course

We all know the problem: We want to debug print our app, but the messages in the chat are just a mess. They are not formatted, it’s hard to get an overview…. it just isn’t fun. That’s why I will introduce you to the logger package, which solves exactly this problem. Let’s get started!

Happy reading!

Note: Logger could look like this:

Installation

The first thing we have to do after creating our app is to add logger. To do so, we will use the command dart pub add logger. You should be familiar with those commands, otherwise, I recommend you to look up the basics of Flutter again.

There is also a package called logger_flutter, which you can use if you use Flutter. If you install this, you can shake your phone and it will show you the console on the phone. You can install it with flutter pub add logger_flutter. The console on your phone could look like this:

Basics

First, you have to create a logger variable. If you use packages like Riverpod, you can store them in a provider to access them in the whole app (Learn Riverpod here)

And here we already have many options for how our logger will look like:

Now we want to show something in our console. There are multiple logger levels, which I will list here:

Deep Dive & Advanced Usage

Let’s talk about LogFilter, LogPrinter and LogOutput. They are used to customize your experience with logger even more:

LogFilter

The LogFilter decides which log events should be shown and which don’t. It’s by default set toDevelopmentFilter, which shows all logs with level >= Logger.level while in debug mode, but in release mode, all logs will be omitted. You can create your won LogFilter of course. To do so, you have to create a class that extends LogFilter and create a function called shouldLog.

LogPrinter

LogPrinter creates and formats the output, which is then sent to the LogOutput (We will talk about LogOutput in the next chapter). Of course, you can implement your own LogPrinter:

Note by the author of this package:

If you created a cool *LogPrinter* which might be helpful to others, feel free to open a pull request. :)

(Source: https://pub.dev/packages/logger#logprinter)

LogOutput

LogOutput is responsible for sending the log lines to the desired destination. Its default implementation ConsoleOutput sends every line to the system console. It’s possible, that in the future the LogOutput could send a file to firebase or Logcat. The author says, that you should “feel free to open a pull request” (Source: https://pub.dev/packages/logger#logoutput).

Further reading & Conclusion

In this article, you have learned the basics of the console manager “logger”. You have seen how easy it is to use and how beautiful makes your console output.

You can unfold the whole power of logger if you use packages like Freezed, Isar, or Flutter Hooks. If you want to learn these additions, I have entire tutorials about them. Check them out here.

In the following few articles, I will introduce more somewhat complicated packages and explain them. If you don’t want to miss this, I recommend you to follow me. I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

Thanks for reading, have a nice day!

NOTICE: This article is based on the documentation of the logger package. All the information provided here is from this site and a big part of the source code is taken from the documentation. Logger Documentation Source: https://pub.dev/packages/logger. The package is licensed under MIT License by Simon Leier.