You are currently viewing Flutter Logging Example

Flutter Logging Example

  • Post author:
  • Post category:Log
  • Post comments:0 Comments

A fundamental part of software development is logging text. Logs are extremely vital in being able to debug your software. Without Logs you cannot easily pinpoint where your project is crashing. Moreover Logging helps in improving software quality through printing of warnings and suggestions.

In this tutorial we will look at some simple logging examples using various packages and solutions.

(a). Logging using Logger

Logger is a Small, easy to use and extensible logger which prints beautiful logs.

It is inspired by logger for Android.

Here is Logger demo screenshot:

Flutter Logger
Flutter Logger

Step 1: Install Logger

For dart, execute the following command in the terminal:

 $ dart pub add logger

For Flutter declare Logger as a flutter dependency in the pubspec.yaml:

dependencies:
  logger: ^1.1.0

Step 2: Write code

Import Logger first:

import 'package:logger/logger.dart';

Then instantiate the Logger as follows:

var logger = Logger();

Then simply log out as follows:

logger.d("Logger is working!");

Not only can you pass a string , you can also pass complex data types like List, Map and Set.

Here is how you set the log level:

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

You can pass options to Logger:

var logger = Logger(
  filter: null, // Use the default LogFilter (-> only log in debug mode)
  printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
  output: null, // Use the default LogOutput (-> send everything to console)
);

You can log using pretty print:

var logger = Logger(
  printer: PrettyPrinter(
    methodCount: 2, // number of method calls to be displayed
    errorMethodCount: 8, // number of method calls if stacktrace is provided
    lineLength: 120, // width of the output
    colors: true, // Colorful log messages
    printEmojis: true, // Print an emoji for each log message
    printTime: false // Should each log print contain a timestamp
  ),
);

Full Example

Here is a full Logger Example for Dart:

main.dart

import 'package:logger/logger.dart';
var logger = Logger(
  printer: PrettyPrinter(),
);
var loggerNoStack = Logger(
  printer: PrettyPrinter(methodCount: 0),
);
void main() {
  print(
      'Run with either <code>dart example/main.dart or dart --enable-asserts example/main.dart.');
  demo();
}
void demo() {
  logger.d('Log message with 2 methods');
  loggerNoStack.i('Info message');
  loggerNoStack.w('Just a warning!');
  logger.e('Error! Something bad happened', 'Test Error');
  loggerNoStack.v({'key': 5, 'value': 'something'});
  Logger(printer: SimplePrinter(colors: true)).v('boom');
}

Reference

Here are the reference links:

Number Link
1. Download Example
2. Documentation
3. Repository
4. Follow package author

Leave a Reply