flex_logger_console 1.0.4
flex_logger_console: ^1.0.4 copied to clipboard
Console logging provider for FlexLogger - provides formatted console output with colors and log levels
flex_logger_console #
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
- Customizable –
ConsoleFormatter(lineSymbol, maxLineWidth, enableColors, dateFormat), optional filter, optionalLoggerOutput - Platform support – IO (print) and Web (console.log) with conditional exports
- Custom formatters – Implement
LogFormatterand pass toConsoleLoggerProvider
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-DateFormatinstance for formatting timestamps (default:DateFormat('HH:mm:ss.SSS'))- Examples:
DateFormat('HH:mm:ss.SSS')- Time with millisecondsDateFormat('yyyy-MM-dd HH:mm:ss')- Full date and timeDateFormat.Hms()- Hour:minute:secondDateFormat('dd/MM/yyyy HH:mm', 'pl_PL')- Custom locale
- See DateFormat documentation
- Examples:
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-256colorif colors don't appear
License #
MIT License - see LICENSE file for details.