Dart:
Logging

How to:

Dart includes a simple logging mechanism through the dart:developer library. For more sophisticated logging needs, programmers often turn to third-party libraries like logger and log4dart.

Using dart:developer

This is suitable for basic logging, especially during development:

import 'dart:developer';

void main() {
  log('This is a debug log message.');
}

Output:

This is a debug log message.

Using the logger package

For a more comprehensive solution, the logger package offers various levels of logging (e.g., info, warning, error) and can be formatted in a more readable manner.

First, add the logger dependency in your pubspec.yaml file:

dependencies:
  logger: ^1.0.0

Then, use it as follows:

import 'package:logger/logger.dart';

var logger = Logger();

void main() {
  logger.d("This is a debug message");
  logger.w("This is a warning message");
  logger.e("This is an error message");
}

Sample output might look like this, with each message type being formatted differently for easy identification:

💬 This is a debug message
⚠️ This is a warning message
❗️ This is an error message

Using the log4dart package

For applications requiring configuration-based logging (similar to Log4j), log4dart offers a familiar approach. It’s especially handy for large scale applications.

Ensure you include log4dart in your pubspec.yaml:

dependencies:
  log4dart: ^2.0.0

A simple usage example:

import 'package:log4dart/log4dart.dart';

void main() {
  final logger = LoggerFactory.getLogger("MyApp");
  logger.debug("Debugging MyApp");
  logger.info("Informational message");
}

Output:

DEBUG: Debugging MyApp
INFO: Informational message

Each of these methods provides a different level of flexibility and complexity, from simple debugging messages to comprehensive, configurable logging suited to the needs of complex applications.