top of page
Writer's pictureDon Peter

Logging in Flutter


Logging in Flutter

Logging plays a vital role in debugging, monitoring, and analyzing the behavior of your Flutter application. Choosing the right logging method and plugin can significantly improve your development workflow and overall code quality.


This blog post explores different logging options available in Flutter, their advantages and disadvantages for each.


Native Logging Methods


print() and debugPrint():

These are the most basic logging methods provided by Flutter and Dart. They offer simple syntax and are easily accessible. However, they lack log levels, filtering options, and other features essential for comprehensive logging.


print('This is a simple log message'); 

debugPrint('This message is only visible in debug mode'); 

To prevent the potential loss of log lines due to Android's log line discarding mechanism when outputting an excessive amount of logs at once, it is advisable to utilize debugPrint() from Flutter's foundation library. This function acts as a wrapper around the standard print method, implementing throttling to ensure that the log output remains at a level that avoids being discarded by Android's kernel.


dart:developer:

This package provides more advanced logging features than print(). It allows logging messages with more granularity.


import 'dart:convert';
import 'dart:developer';

class User {
  // Define your custom object properties and methods here.
}

void main() {
  // Create an instance of your custom object.
  var user = User();
  
  // Log a message with additional application data using the error parameter.
  log(
    'This is getting logged',
    name: 'some.id',
    error: jsonEncode(user),
  );
}

The log function is used to log a message. The name parameter is used to specify the logging category, and the error parameter is employed to pass additional application data. In this case, the jsonEncode function is used to encode the custom object as a JSON string before passing it to the error parameter.


This approach allows you to view the JSON-encoded data as a structured object when examining the log entries in tools like DevTools.


Advantages

  • Easy to implement and understand.

  • Built-in with Flutter and Dart.

  • Suitable for simple logging needs.

Disadvantages

  • Lack of filtering options and log levels.

  • Not ideal for complex applications with extensive logging needs.


Third party Logging Package


Logger:

Logger is a popular package that offers a powerful and flexible logging API. It provides various log levels, custom filters, and different output destinations.



var logger = Logger(
  filter: null,
  printer: PrettyPrinter(), 
  output: null, 
);
  
logger.i('Starting the application'); 
logger.w('A warning message', error: error); 

Link to logger plugin: https://pub.dev/packages/logger


Advantages:

  • More features and flexibility than native methods.

  • Can be extended to have support with external services and platforms.

  • Customizable output and filtering options.

Disadvantages:

  • May require additional setup and dependencies.

  • Can be more complex to use.


Choosing the Right Logging Method

The best logging method for your project depends on your specific needs and requirements. Here are some factors to consider:

  • Project Size and Complexity: Simple projects with minimal logging needs might benefit from native methods like print() or dart:developer. For larger and more complex applications, consider using a dedicated logging package like Logger.

  • Performance Considerations: For performance-critical applications, lightweight packages should be used.


By understanding the advantages and disadvantages of each option and considering your specific requirements, you can make an informed decision and ensure your application is well-equipped for success.

Comments


Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

Monitor & Improve Performance of your Mobile App

 

Detect memory leaks, abnormal memory usages, crashes, API / Network call issues, frame rate issues, ANR, App Hangs, Exceptions and Errors, and much more.

Explore Finotes

bottom of page