パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.5

pretty_http_logger

Pretty Http Logger は、Dart HTTP ライブラリ用のミドルウェアログライブラリで、ネットワーク呼び出しを美しく、読みやすい形式でログに記録し、ネットワーク呼び出しのインターセプトもサポートしています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
4730
いいね数
20
Pub ポイント
150

パッケージをインストール

flutter pub add pretty_http_logger

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Pretty Http Logger は、HTTP ネットワークリクエストとレスポンスのログをクリーンで読みやすい形式で提供する Dart パッケージです。Dart の HTTP クライアントとシームレスに統合されており、開発者が API とのやり取りを簡単にモニタリングおよびデバッグできるようにします。ネットワーク呼び出しをインターセプトする機能を備えており、開発中にリクエストやレスポンスの詳細な検査や変更が可能です。Android、iOS、Web、macOS、Windows、Linux といったすべての主要プラットフォームをサポートしているため、クロスプラットフォームアプリで透明なネットワークログが必要な場合に最適です。

用途

  • APIの問題のデバッグ
  • ネットワークパフォーマンスのモニタリング
  • リクエスト/レスポンスデータの確認
  • API統合のテスト
  • 開発ワークフローの向上
  • 本番環境でのログイン

主な機能

  • 整形されたHTTPログ
  • リクエスト/レスポンスのインターセプト
  • クロスプラットフォーム互換性
  • http パッケージとの簡単な統合
  • カスタマイズ可能なログフォーマット
  • 同期および非同期呼び出しの両方をサポートしています

適している対象

  • クロスプラットフォームアプリを開発する開発者
  • 詳細なネットワークデバッグを必要とするチーム
  • APIを使用するフロントエンドエンジニア
  • テスト担当者がAPIの動作を検証しています
  • 透過的なネットワークモニタリングを必要とするアプリケーション

注意事項

  • 機密データをフィルタリングせずに本番環境で使用しないでください
  • 大量のデータをログに記録するとパフォーマンスに影響する可能性があります
  • ログが散らかるのを避けるために適切な設定が必要です
  • 高頻度のログ記録シナリオには適していません
  • セキュリティのためにリリースビルドで無効にする必要があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

pretty_http_logger

A middleware logger library for Dart http library.

Getting Started

pretty_http_logger is a module that lets you log request and response for Dart's http package.

Installing

Include this library in your package.

pretty_http_logger: any
Importing
import 'package:pretty_http_logger/pretty_http_logger.dart';
Using pretty_http_logger

Create an object of HttpWithMiddleware by using the build factory constructor.

The build constructor takes in a list of middlewares that are built already by pretty_http_logger.

(You can also build your own middleware for pretty_http_logger. Check out Build your own middleware)
HttpWithMiddleware http = HttpWithMiddleware.build(middlewares: [
    HttpLogger(logLevel: LogLevel.BODY),
]);

That is it! Now go ahead and use this http object as you would normally do.

//Simple POST request
var response = await http.post('https://jsonplaceholder.typicode.com/posts/',
    body: {"testing", "1234"});

//Simple GET request
var response = await http.get('https://jsonplaceholder.typicode.com/posts/');

/// Simple Multipart Request
final request = MultipartRequest('POST', Uri.parse('YOUR_URL'));
request.fields['category_id'] = '1';
request.headers.addAll({'Authorization': 'YOUR_AUTH_TOKEN'});
final file = await MultipartFile.fromPath(
"images[0]",
"image_picker_632B42C0-11AF-4075-90F8-A7FCD48A7FC0-27133-0000014E3B2E5959.png",
contentType: MediaType("image", "jpg"),
);
request.files.add(file);
final response = await http.multipart(request);
Request Timeout

With pretty_http_logger you can also specify the timeout of requests. So if you want a request to be timeout in 30 seconds:

HttpWithMiddleware http = HttpWithMiddleware.build(
  requestTimeout: Duration(seconds: 30),
  middlewares: [
    HttpLogger(logLevel: LogLevel.BODY),
]);

HttpWithMiddleware supports all the functions that http provides.

http.get(...);
http.post(...);
http.put(...);
http.delete(...);
http.head(...);
http.patch(...);
http.read(...);
http.readBytes(...);
Using a Client

If you want to use a http.Client in order to keep the connection alive with the server, use HttpClientWithMiddleware.

HttpClientWithMiddleware httpClient = HttpClientWithMiddleware.build(middlewares: [
    HttpLogger(logLevel: LogLevel.BODY),
]);

var response = await httpClient.post('https://jsonplaceholder.typicode.com/posts/',
    body: {"testing", "1234"});

var response = await httpClient.get('https://jsonplaceholder.typicode.com/posts/');

//Don't forget to close the client once done.
httpClient.close();
Building your own middleware

Building your own middleware with pretty_http_logger is very easy, whether you want to create a package for pretty_http_logger or you want to build a middleware solely for your own project.

Once you have the necessary imports, all you have to do is extend the MiddlewareContract class which will give you access to 3 functions.

interceptRequest(RequestData) is the method called before any request is made. interceptResponse(ResponseData) is the method called after the response from request is received. interceptError(error) is the method called when an exception occurs.

You can then @override all the required functions you need to add middleware to.

Example (A simple logger that logs data in all requests):

class Logger extends MiddlewareContract {
  @override
  void interceptRequest(RequestData data) {
    print("Method: ${data.method}");
    print("Url: ${data.url}");
    print("Body: ${data.body}");
  }

  @override
  void interceptResponse(ResponseData data) {
    print("Status Code: ${data.statusCode}");
    print("Method: ${data.method}");
    print("Url: ${data.url}");
    print("Body: ${data.body}");
    print("Headers: ${data.headers}");
  }
  @override
  void interceptError(err) {
    print("Error: $err");
  }
}

You can also modify the RequestData before the request is made and every ResponseData after every response is received. For Example, if you want to wrap your data in a particular structure before sending, or you want every request header to have Content-Type set to application/json.

class Logger extends MiddlewareContract {
  @override
  void interceptRequest(RequestData data) {
    //Adding content type to every request
    data.headers["Content-Type"] = "application/json";
    
    data.body = jsonEncode({
      uniqueId: "some unique id",
      data: data.body,
    });
  }

  @override
  void interceptResponse(ResponseData data) {
    //Unwrapping response from a structure
    data.body = jsonDecode(data.body)["data"];
  }
  @override
  void interceptError(err) {
    print("Error: $err");
  }
}
Package pretty_http_logger is inspired by
  • pretty_http_logger(https://github.com/mithunadhikari40/pretty_http_logger) : Easy request and response logging.
  • pretty_http_logger is inspired by http_logger(https://github.com/gurleensethi/http_logger)
  • pretty_http_logger is inspired by http_middleware(https://github.com/TEDConsulting/http_middleware)
  • pretty_http_logger is inspired by pretty_dio_logger(https://github.com/Milad-Akarie/pretty_dio_logger)
  • pretty_http_logger is a combination of packages mentioned above and uses the code provided by these libraries and keeps everything up to date

If you find any issues or suggestions pretty_http_logger, open an issue or raise a PR, I will be happy to see.

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト