log_tools 1.0.1 copy "log_tools: ^1.0.1" to clipboard
log_tools: ^1.0.1 copied to clipboard

A Flutter logging package that provides detailed and human-readable logging for complex data structures, including maps and lists. It integrates with Dio interceptors to log network requests, response [...]

log_package #

A customizable Flutter package with built-in support for Dio interceptors, enabling detailed logging for network requests, responses, and errors. The package is designed to assist developers in monitoring their API interactions effectively with options to control the verbosity of logs.

Features #

  • Log HTTP requests and responses using Dio interceptors.
  • Toggle between logging different stages: request, response, and errors.
  • Customize which parts of the request or response are logged (headers, data, etc.).
  • Pretty-print JSON data for easy readability.
  • Optionally log requests and responses as cURL commands.
  • Colored logs for better distinction between request types and errors.
  • General-purpose logging with LogTools.info() to format and log complex lists and maps.

Usage #

1. Pretty-printing Complex Data with LogTools.info() #

The LogTools.info() function allows you to print human-readable and formatted logs of any data in your app, making it especially useful for complex lists or maps.

LogTools.info(
  title: LogToolsInfoTitle(title: 'User Data', value: 'List of Users'),
  body: [
    LogToolsInfoBody(title: 'User 1', value: {'name': 'Alice', 'age': 30}),
    LogToolsInfoBody(title: 'User 2', value: {'name': 'Bob', 'age': 25}),
  ],
);

This will output:

[Pretty-Printed Log Output]

2. Adding the Dio Interceptor to Log Network Requests #

You can use the LogToolsDioInterceptor to log all network traffic through Dio, with options to customize the verbosity of the logs.

import 'package:dio/dio.dart';
import 'package:log_tools/log_tools.dart';

final dio = Dio();
dio.interceptors.add(LogToolsDioInterceptor(
  showRequest: true,
  showRequestCurl: true,
  showRequestData: true,
  showResponse: true,
  showResponseCurl: false,
  showResponseData: true,
  showError: true,
  showErrorData: true,
));

3. Logging Options #

LogTools.info():

  • LogToolsInfoTitle: The title and optional value displayed in the log header.
  • LogToolsInfoBody: A list of key-value pairs (maps, lists, etc.) to be pretty-printed.

LogToolsDioInterceptor:

  • showRequest: Logs HTTP requests.
  • showRequestCurl: Logs the request as a cURL command.
  • showRequestData: Logs request body data.
  • showResponse: Logs HTTP responses.
  • showResponseCurl: Logs the response as a cURL command.
  • showResponseData: Logs response body data.
  • showError: Logs any errors encountered during requests.
  • showErrorData: Logs the data related to errors.

Installation #

Add the following to your pubspec.yaml:

dependencies:
  log_tools: ^1.0.0

Then, run flutter pub get to install the package.

Example Output #

Request Dio Interceptor Log:

[Log Dio Interceptor Request]

Response Dio Interceptor Log:

[Log Dio Interceptor Response]

Contribution #

Contributions are welcome! Feel free to open issues, submit pull requests, or suggest new features.


Happy logging with beautifully formatted data!

1
likes
0
points
25
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter logging package that provides detailed and human-readable logging for complex data structures, including maps and lists. It integrates with Dio interceptors to log network requests, responses, and errors in a customizable and easily readable format, while also offering general-purpose logging throughout the app.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

dio, flutter

More

Packages that depend on log_tools