Dart| Flutter: How to add logging to an application with configuration and examples

This post explains how to add logging into dart and flutter applications.

Logging is used to log different types of messages such as information, error, and debug messages, Helps developers to debug errors and issues.

Currently, there are two types of popular libraries in Dart

  • Dart logging
  • Dart logger

You can add these loggers in Dart and Flutter applications

We are going to use the logging library in dart applications.

How to use logging in Dart applications with examples?

First, add logging dependency in pubspec.yml

name: dartapp
description: >-
   dart example application.
version: 1.0.0

  sdk: '>=2.10.0 <3.0.0'
  logging: any
    intl: any

Next, Install the dependency using the below command dart pub install to install dependency in dart flutter pub install to install dependency in a flutter

Once dependency is installed, follow the steps

  • import the logging package into your code

Then in your dart code, import the below package

import 'package:logging/logging.dart';

Next, First, initialize an object of the Logger class, and pass a unique string to it.

final log = Logger('LoggingExample');

logging does not print anything until you configured Levels and onRecord listen.

listen method should log different custom messages

LEVEL.INFO is a default logging that logs information messages, It does not log debug messages.

LEVEL.ALL: it prints all log messages

  Logger.root.level = Level.ALL; // defaults to Level.INFO
  Logger.root.onRecord.listen((record) {
    print('${record.level.name}: ${record.time}: ${record.message}');

Next, log messages using different methods.

  log.info("log info method");

Let’s see different examples of enabling different LEVEL.

Dart logging LEVEL.ALL example

import 'package:logging/logging.dart';

final log = Logger('LoggingExample');

void main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    print('${record.level.name}: ${record.time}: ${record.message}');

  // log message with different methods
  log.shout("log shout method");
  log.severe("log severe method");
  log.warning("log warning method");
  log.info("log info method");
  log.config("log config method");
  log.fine("log fine method");
  log.finer("log finer method");
  log.finest("log finest method");

Output: It logs all types of messages that contain different levels

SHOUT: 2022-07-03 09:51:00.894768: log shout method
SEVERE: 2022-07-03 09:51:00.894768: log severe method
WARNING: 2022-07-03 09:51:00.894768: log warning method
INFO: 2022-07-03 09:51:00.894768: log info method
CONFIG: 2022-07-03 09:51:00.904131: log config method
FINE: 2022-07-03 09:51:00.904131: log fine method
FINER: 2022-07-03 09:51:00.904131: log finer method
FINEST: 2022-07-03 09:51:00.904131: log finest method

next, Change the Logging level to OFF

  Logger.root.level = Level.OFF;

Output: It does not print any log messages.

next, Change the Logging level to INFO

  Logger.root.level = Level.INFO;

It prints information messages below. Output:

SHOUT: 2022-07-03 09:53:48.817704: log shout method
SEVERE: 2022-07-03 09:53:48.823688: log severe method
WARNING: 2022-07-03 09:53:48.823688: log warning method
INFO: 2022-07-03 09:53:48.824687: log info method

next, Change the Logging level to CONFIG

  Logger.root.level = Level.CONFIG;


SHOUT: 2022-07-03 09:54:31.165708: log shout method
SEVERE: 2022-07-03 09:54:31.174165: log severe method
WARNING: 2022-07-03 09:54:31.174165: log warning method
INFO: 2022-07-03 09:54:31.174165: log info method
CONFIG: 2022-07-03 09:54:31.174165: log config method

with the above different levels configured, It displays different log messages.

Dart logging levels

Currently, It supports the following Levels.

Level.OFF: Logging is disabled Level.SHOUT: log shout messages Level.SEVERE: log severe errors Level.WARNING: log warnings Level.INFO: log useful message Level.CONFIG: log configuration message Level.FINE: log success message with a FINE level Level.FINER: log success message with FINER level Level.FINEST: log success message with FINEST level

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.