搜尋套件、儲存庫與 APP 分類

套件詳情v2.8.0

logger

小巧、易用且可擴展的記錄器,可印出美觀的日誌。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
359萬
喜歡數
3730
Pub 分數
150

安裝套件

flutter pub add logger

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

日誌記錄套件是為Flutter應用程式提供的輕量級、可自訂的日誌解決方案。它提供乾淨、易讀且美觀格式化的日誌,支援多種日誌層級、結構化輸出以及容易整合。專為除錯與監控設計,幫助開發者在所有平台有效追蹤應用行為。此套件具備可擴充性,允許自訂格式化程式與輸出目的地。其極小的佔用空間與簡單的API使其非常適合開發與生產環境使用。

適用情境

  • 除錯應用流程
  • 監控執行時間行為
  • 追蹤錯誤和警告
  • 生產環境應用的記錄
  • 與 CI/CD 流水線整合

主要特色

  • 格式優美的日誌
  • 可擴充的格式化系統
  • 多個日誌等級
  • 跨平台支援
  • 簡單 API
  • 自定輸出目的地

適合對象

  • 尋求簡單記錄日誌的開發者
  • 需要結構化日誌的團隊
  • 需要調試可見性的應用
  • 具有日誌記錄需求的生產環境
  • 跨平台 Flutter 項目

使用建議

  • 可能需要為生產環境進行設定
  • 詳細輸出可能會影響效能,若未妥善管理
  • 內建篩選選項有限
  • 不適用於複雜的日誌儲存或分析

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Logger

pub package CI Last Commits Pull Requests Code size License

Small, easy to use and extensible logger which prints beautiful logs.
Inspired by logger for Android.

Show some ❤️ and star the repo to support the project

Resources:

Getting Started

Just create an instance of Logger and start logging:

var logger = Logger();

logger.d("Logger is working!");

Instead of a string message, you can also pass other objects like List, Map or Set.

Output

Documentation

Log level

You can log with different levels:

logger.t("Trace log");

logger.d("Debug log");

logger.i("Info log");

logger.w("Warning log");

logger.e("Error log", error: 'Test Error');

logger.f("What a fatal log", error: error, stackTrace: stackTrace);

To show only specific log levels, you can set:

Logger.level = Level.warning;

This hides all trace, debug and info log events.

Options

When creating a logger, you can pass some options:

var logger = Logger(
  filter: null, // Use the default LogFilter (-> only log in debug mode)
  printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
  output: null, // Use the default LogOutput (-> send everything to console)
);

If you use the PrettyPrinter, there are more options:

var logger = Logger(
  printer: PrettyPrinter(
      methodCount: 2, // Number of method calls to be displayed
      errorMethodCount: 8, // Number of method calls if stacktrace is provided
      lineLength: 120, // Width of the output
      colors: true, // Colorful log messages
      printEmojis: true, // Print an emoji for each log message
      // Should each log print contain a timestamp
      dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
  ),
);
Auto detecting

With the io package you can auto detect the lineLength and colors arguments. Assuming you have imported the io package with import 'dart:io' as io; you can auto detect colors with io.stdout.supportsAnsiEscapes and lineLength with io.stdout.terminalColumns.

You should probably do this unless there's a good reason you don't want to import io, for example when using this library on the web.

LogFilter

The LogFilter decides which log events should be shown and which don't.
The default implementation (DevelopmentFilter) shows all logs with level >= Logger.level while in debug mode (i.e., running dart with --enable-asserts). In release mode all logs are omitted.

You can create your own LogFilter like this:

class MyFilter extends LogFilter {
  @override
  bool shouldLog(LogEvent event) {
    return true;
  }
}

This will show all logs even in release mode. (NOT a good idea)

LogPrinter

The LogPrinter creates and formats the output, which is then sent to the LogOutput.
You can implement your own LogPrinter. This gives you maximum flexibility.

A very basic printer could look like this:

class MyPrinter extends LogPrinter {
  @override
  List<String> log(LogEvent event) {
    return [event.message];
  }
}

If you created a cool LogPrinter which might be helpful to others, feel free to open a pull request. :)

Colors

Please note that in some cases ANSI escape sequences do not work under macOS. These escape sequences are used to colorize the output. This seems to be related to a Flutter bug that affects iOS builds: https://github.com/flutter/flutter/issues/64491

However, if you are using a JetBrains IDE (Android Studio, IntelliJ, etc.) you can make use of the Grep Console Plugin and the PrefixPrinter decorator to achieve colored logs for any logger:

var logger = Logger(
    printer: PrefixPrinter(PrettyPrinter(colors: false))
);

LogOutput

LogOutput sends the log lines to the desired destination.
The default implementation (ConsoleOutput) send every line to the system console.

class ConsoleOutput extends LogOutput {
  @override
  void output(OutputEvent event) {
    for (var line in event.lines) {
      print(line);
    }
  }
}

Other provided LogOutputs are:

  • FileOutput/AdvancedFileOutput
  • StreamOutput

Possible future LogOutputs could send to Firebase or to Logcat. Feel free to open pull requests.

Acknowledgments

This package was originally created by Simon Choi, with further development by Harm Aarts, greatly enhancing its functionality over time.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案