Understanding logging service in flutter framework

In this post, we are learning about console logging in mobile app development.it is very important because sometimes you want to fix bugs using print(), this logger package helps you understand your messages in the console easily

we are using logger package to understand how logging service works.

Setup

adding the dependency in pubspec.yaml

logger: ^1.0.0

Import the package

import 'package:logger/logger.dart';

create a new instance of logger service

final logger = Logger();

You can pass some options with in logger constructor.

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

Log a message at level [Level.verbose]

logger.d('Log message with 2 methods');

Log a message at level [Level.debug].

logger.v("Verbose log");

Log a message at level [Level.info].

logger.i("Info log");

Log a message at level [Level.warning].

logger.w("Warning log");

Log a message at level [Level.error].

 logger.e("Error log");

Log a message at level [Level.wtf].

logger.wtf("What a terrible failure log");

example output of logger service

Set Logger type

void main() {
  Logger.level = Level.verbose;
  runApp(MyApp());
}

Logging with PrettyPrinter

logger constructor accepts parameter called 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
  ),

Additional resources

https://pub.dev/packages/logger

Conclusion

In this post we leaned about logging so that’s it Thank you.

Leave a Comment