搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目