flutter Logger

2,125 阅读1分钟

Logger

小,易于使用和可扩展的日志打印漂亮的日志。灵感来自Android的logger

开始使用

只需创建一个Logger实例并开始打印日志:

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

除了字符串消息,您还可以传递其他对象,如List、Map或Set。

输出日志会时,除了会打印"Logger is working!"这一句日志外,还会打印出日志是在哪一个类,哪一行。

可以输出不同级别的日志:

logger.v("Verbose log");

logger.d("Debug log");

logger.i("Info log");

logger.w("Warning log");

logger.e("Error log");

logger.wtf("What a terrible failure log");

要仅显示特定的日志级别,您可以设置:

Logger.level = Level.warning;

上面的设置这隐藏了所有的verbosedebuginfo日志事件。

设置

当创建一个logger,你可以自定义一些选项:

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)
);

如果你使用PrettyPrinter,有更多的选项:

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
    printTime: false // Should each log print contain a timestamp
  ),
);

自动检测

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

LogFilter决定哪些日志事件应该显示,哪些不应该显示。

The default implementation (DevelopmentFilter) shows all logs with level >= Logger.level while in debug mode. In release mode all logs are omitted.

更多使用,请查看说明

github.com/leisim/logg…

logger_flutter扩展

LogConsoleOnShake(
  child: Container() // Your widgets
),

摇动手机即可显示日志输出控制台。