flex_logger_console

Pub Version License: MIT Flutter Dart

Formatted, colored console output for FlexLogger with ANSI colors and configurable formatters.

Contents

Features

  • Colored output – ANSI colors from log level (or custom FlexLog.color), via RGB
  • Bordered format – Title, timestamp, message with configurable line width and symbol
  • CustomizableConsoleFormatter (lineSymbol, maxLineWidth, enableColors, dateFormat), optional filter, optional LoggerOutput
  • Platform support – IO (print) and Web (console.log) with conditional exports
  • Custom formatters – Implement LogFormatter and pass to ConsoleLoggerProvider

Colors

Level colors are defined in flex_logger (per log type). This package renders them with ANSI RGB via Color.toAnsiPen() (see ColorAnsiPen). Custom logs can pass color when extending FlexLog for consistent console coloring.

Installation

Add as a dependency in your pubspec.yaml:

dependencies:
  flex_logger: ^1.0.0
  flex_logger_console: ^1.0.0

Then run:

flutter pub get

Usage

Basic Setup

import 'package:flex_logger/flex_logger.dart';
import 'package:flex_logger_console/flex_logger_console.dart';

void main() async {
  FlexLogger.instance.configure(
    providers: [ConsoleLoggerProvider()],
  );
  await FlexLogger.instance.initialize();

  final logger = FlexLogger.instance;
  logger.info('Application started');
  logger.debug('Debug information');
  logger.success('Operation completed');
  logger.warning('Potential issue detected');
  logger.error('Error occurred');
  logger.critical('Critical system failure');
}

Custom Configuration

import 'package:flex_logger/flex_logger.dart';
import 'package:flex_logger_console/flex_logger_console.dart';

FlexLogger.instance.configure(
  providers: [
    ConsoleLoggerProvider(
      formatter: ConsoleFormatter(
        enableColors: true,
        maxLineWidth: 100,
        lineSymbol: '─',
        dateFormat: DateFormat('HH:mm:ss.SSS'),
      ),
      filter: MinLevelFilter(FlexLogLevel.warning),
    ),
  ],
);
await FlexLogger.instance.initialize();

Example Output

Default format (title, time, message) with borders and level color:

┌────────────────────────────────────────────────────────────────────────────────
│ [DEBUG] | 13:45:23.456 | Debug message from button
└────────────────────────────────────────────────────────────────────────────────
┌────────────────────────────────────────────────────────────────────────────────
│ [INFO] | 13:45:24.123 | Info message from button
└────────────────────────────────────────────────────────────────────────────────
┌────────────────────────────────────────────────────────────────────────────────
│ [SUCCESS] | 13:45:25.789 | Success message from button
└────────────────────────────────────────────────────────────────────────────────

Running the Example

cd example
flutter run -d macos  # or any other device

Then click the buttons in the app to see colored logs in your terminal.

API Reference

ConsoleLoggerProvider

Provider for console logging. Use with FlexLogger.instance.configure(providers: [...]).

Constructor:

ConsoleLoggerProvider({
  LogFormatter? formatter,   // defaults to ConsoleFormatter()
  LoggerOutput? output,     // defaults to platform-specific (IO or Web)
  LogFilter filter = const AcceptAllFilter(),
})

ConsoleFormatter

Default formatter for console output.

Constructor:

ConsoleFormatter({
  String lineSymbol = '─',
  int maxLineWidth = 110,
  bool enableColors = true,
  DateFormat? dateFormat,
})

Parameters:

  • lineSymbol - Character used for separator lines (default: '─')
  • maxLineWidth - Maximum width of separator lines (default: 110)
  • enableColors - Whether to use ANSI colors in output (default: true)
  • dateFormat - DateFormat instance for formatting timestamps (default: DateFormat('HH:mm:ss.SSS'))
    • Examples:
      • DateFormat('HH:mm:ss.SSS') - Time with milliseconds
      • DateFormat('yyyy-MM-dd HH:mm:ss') - Full date and time
      • DateFormat.Hms() - Hour:minute:second
      • DateFormat('dd/MM/yyyy HH:mm', 'pl_PL') - Custom locale
    • See DateFormat documentation

Use filter (e.g. MinLevelFilter) to control which logs are shown. Use output to redirect output (e.g. to a file or custom sink); type is LoggerOutput (void Function(String message)).

Platform Notes

Web

On web platforms, console output uses console.log() and may not display ANSI colors depending on browser developer tools.

Terminal Support

For best color support, ensure your terminal supports 256 colors:

  • Most modern terminals (iTerm2, Terminal.app, Windows Terminal) support this by default
  • Set TERM=xterm-256color if colors don't appear

License

MIT License - see LICENSE file for details.

Libraries

flex_logger_console
Console logging provider for FlexLogger.