How to Setup Logger in Flutter

Alperen Yalçn
4 min readMar 16, 2022

I am not going to ask you ‘have you ever used print()’ because the answer is obvious but if you are still using print then this article might convince you to stop using print statement.

In this article i am going to demonstrate three packages from pub.dev and why you should use them.

What is Logging

A log file is a file that records either event that occur in the operating system or in software and logging is act of keeping the logs.

Benefits

You might see a lot of unrelevant information on debug console while debugging flutter apps and if you use print it will slow down your coding and debugging performance because finding what you need can be stressful from thousand of lines or if you need to save logs to file logging packages can help you with these.

Note: Before diving to the packages, dont forget to run ‘dart pub get’ command after changing your pubspec.yaml file.

Packages

1 - logging

This one is the most popular logging package(according to pub.dev) which created by the Dart Team.

As alway start with adding package to pubspec.yaml file.

dependencies:
logging: ^1.0.2

First you need to set the root level and in this package there are 7 types of logs level but there is a hierarchy between them which means it matters which root level and log level you use. All messages at or above the current level are sent to the onRecord stream. Available levels and logging methods are listed below.

Log Levels — Log Methods

For example you set the root level as a Level.WARNING and wanted to use log.fine(). In this case you wont see your log in the debug console because FINE level is lower than WARNING level in hierarchy.

After defining the root level you also need to create the onRecord stream. This stream handle the events fired from logging methods.

I called the onRecord stream inside initState method of initial page and it will handle logs from other pages but you can also create an initializer function and call onRecord inside the initializer.

Now you are ready to start logging. Create a variable from Logger and call one of the logging method.

final log = Logger(‘ExampleLogger’);log.fine(“SHOUTTİNG”);

2 - logger

İf you only want some fancy log message in debug console without any effort i can recommend this package.

First add logger package to your pubspec.yaml file

dependencies:
logger: ^1.1.0

Now open the file you want to log and create a Logger variable.

There a 6 level of log in this package and you can also pass list, set, map objects instead of string.

logger.v(“Verbose log”);logger.d(“Debug log”);logger.i(“Info log”);logger.w(“Warning log”);logger.e(“Error log”);logger.wtf(“What a terrible failure log”);

And their outputs look like below.

output of logs

3 -my_logger

This package provides functionality for log data sets into database and fetch it by different filters . If sounds good i listed some of the functionalities.

  • Save logs into database
  • Export logs into file
  • Fetch or delete logs easily
  • Log filtering support

Lets start with adding this package to dependencies.

dependencies:
my_logger: ^1.0.0

Log files are exported on storage directory so you need an extra setup.

There are 7 level logs exist in this package.

    MyLogger.trace("My trace log"); 

MyLogger.debug("My debug log");

MyLogger.info("My info log");

MyLogger.warning("My warning log");

MyLogger.error("My error log");

MyLogger.severe("My severe log");

MyLogger.fatal("My fatal log");

And their outputs look like below.

output of logs

If you want to export your logs you can use the export method provided by this package.

File fileExport = await MyLogger.logs.export(
fileName: "export-all-logs",
exportType: FileType.TXT,
filter: LogFilter.last24Hours(),
);

Also if you wonder other methods you can check this page for more details.

--

--