flutter_http_logger 1.0.2 copy "flutter_http_logger: ^1.0.2" to clipboard
flutter_http_logger: ^1.0.2 copied to clipboard

A Flutter package to log HTTP requests and responses, making it easier to debug and monitor network traffic in real-time.

flutter_http_logger #

flutter_http_logger is a simple Flutter package that enables logging of HTTP requests and responses. This package allows developers to view logs in a web browser using a provided URL. It is designed to create a local server from real devices (not emulators) to facilitate HTTP log viewing.

Features #

  • Log HTTP requests and responses in real-time.
  • View logs on a web browser using a provided URL.
  • Supports different HTTP methods: GET, POST, and POST_FILE.
  • Simple initialization and usage.

Getting Started #

To start using flutter_http_logger, add it to your pubspec.yaml file:

dependencies:
  flutter_http_logger: 1.0.2

Then, import the package:

import 'package:flutter_http_logger/flutter_http_logger.dart';

Usage #

Initializing the Logger #

To initialize the logger and start the local server, use the HttpLog.startServer(context) function in your app:

HttpLog.startServer(context);

Stopping the Logger #

To stop the logger and close the local server, use the HttpLog.endServer() function in your app:

HttpLog.endServer();

Sending Logs #

Whenever you make an HTTP request, log the request and response details using the HttpLog.sendLog() function. Here are examples for different HTTP methods:

Logging a POST Request

HttpLog.sendLog(
  method: "POST",
  url: baseUrl + api,
  header: _header,
  request: dataInJson,
  statusCode: response.statusCode,
  response: response.body,
  duration: endTime.difference(startTime).inMilliseconds,
);

Logging a GET Request

HttpLog.sendLog(
  method: "GET",
  url: baseUrl + api,
  header: _header,
  statusCode: response.statusCode,
  response: response.body,
  duration: endTime.difference(startTime).inMilliseconds,
);

Logging a POST_FILE Request

HttpLog.sendLog(
  method: "POST_FILE",
  url: baseUrl + api,
  header: _header,
  request: data,
  statusCode: response.statusCode,
  response: response.body,
  duration: endTime.difference(startTime).inMilliseconds,
);

Viewing Logs #

After starting the server, visit the provided URL (displayed in the logs or your app console) in a web browser to view real-time logs of your HTTP requests and responses.

Example #

Here is a simple example of how to integrate flutter_http_logger with an HTTP client in a Flutter app:

import 'package:flutter_http_logger/flutter_http_logger.dart';

class _MyWidgetState extends State<MyWidget> {
  @override
  void initState() {
    super.initState();
    // Initialize the HTTP Logger
    HttpLog.startServer(context);

    _makeHttpRequest();
  }

  @override
  void dispose() {
    // End the HTTP Logger
    HttpLog.endServer();
    super.dispose();
  }
}

Future<void> _makeHttpRequest() async {

  // Capture start time
    final startTime = DateTime.now();
final response = await http.post(Uri.parse( baseUrl + api),
    header: _header,
    body: dataInJson);

     // Capture end time
    final endTime = DateTime.now();

    // Log the request and response
  HttpLog.sendLog(
    method: "POST",
    url: baseUrl + api,
    header: _header,
    request: dataInJson,
    statusCode: response.statusCode,
     duration: endTime.difference(startTime).inMilliseconds,
    response: response.body,
  );
}

Setup Instructions (For Android Emulators Only) #

To use flutter_http_logger, follow these steps:

  1. Download the required file from flutter_http_logger_emu.

  2. Open the command prompt or terminal and navigate to the file location.

  3. Run the following command to install dependencies:

    npm install
    
  4. Start the local server by running:

    npm start
    
  5. If you encounter an error stating that npm is not found, install Node.js and set the system path for Node.

  6. Once the setup is complete, every time you run the app, navigate to the file location, open the terminal, and run npm start.

  7. After starting the server, an IP address will be displayed. Open a browser and enter that IP, and you need to input that IP into your Flutter app. You are ready to go!

Contributings #

Contributions are welcome! If you have any issues or feature requests, feel free to open an issue or submit a pull request.

License #

This project is licensed under the MIT License.

4
likes
0
points
28
downloads

Publisher

verified publishershuklashrestha.com.np

Weekly Downloads

A Flutter package to log HTTP requests and responses, making it easier to debug and monitor network traffic in real-time.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, shelf, shelf_web_socket, web_socket_channel

More

Packages that depend on flutter_http_logger