colored_logger 1.0.1
colored_logger: ^1.0.1 copied to clipboard
A Flutter package for colored terminal logs (VS Code, Android Studio, IntelliJ) with Ansi escape codes.
Colored Logger #
A simple yet powerful colored logging utility for Dart and Flutter applications that enhances console output with ANSI colors.
Features #
- Color-coded log levels: Easily distinguish between different types of logs (info, success, warning, error)
- Custom logging: Create your own colored log messages with custom prefixes
- ANSI color support: Includes a variety of ANSI color codes for terminal output
- Lightweight: Minimal dependencies, just import and use
- Easy to use: Simple static methods for quick implementation
Installation #
Add the package to your pubspec.yaml file:
dependencies:
colored_logger: ^0.0.1
Then run:
flutter pub get
Or with Dart:
dart pub get
Usage #
Basic Usage #
import 'package:colored_logger/colored_logger.dart';
void main() {
// Basic usage with predefined log levels
ColoredLogger.info('This is an info message');
ColoredLogger.success('Operation completed successfully');
ColoredLogger.warning('This is a warning message');
ColoredLogger.error('An error occurred');
}
Custom Logging #
import 'package:colored_logger/colored_logger.dart';
import 'package:colored_logger/ansi_code.dart';
void main() {
// Custom colored message with a specific color code
ColoredLogger.custom('Custom message with color code',
colorCode: 'magenta',
prefix: '[CUSTOM] ');
// Custom colored message with ANSI codes
ColoredLogger.custom('Custom message with ANSI codes',
ansiCode: [AnsiCode.bold, AnsiCode.cyan],
prefix: '[STYLED] ');
}
API Documentation #
ColoredLogger Class #
The ColoredLogger class provides static methods for logging messages with different colors and styles.
Basic Logging Methods
ColoredLogger.info(String message, {String prefix = '[INFO] '})- Logs an info message in blueColoredLogger.success(String message, {String prefix = '[SUCCESS] '})- Logs a success message in greenColoredLogger.warning(String message, {String prefix = '[WARNING] '})- Logs a warning message in yellowColoredLogger.error(String message, {String prefix = '[ERROR] '})- Logs an error message in red
Custom Logging
ColoredLogger.custom(
String message, {
String prefix = '',
String colorCode = 'normal',
List<String>? ansiCode,
})
Parameters:
message: The message to logprefix: Optional prefix to add before the message (default: empty string)colorCode: Color name to use (e.g., 'red', 'green', 'blue')ansiCode: List of ANSI codes to apply (takes precedence over colorCode if provided)
AnsiCode Class #
The AnsiCode class provides ANSI escape codes for terminal text styling.
Text Styles
AnsiCode.normal- Reset to normal styleAnsiCode.bold- Bold textAnsiCode.dim- Dimmed textAnsiCode.italic- Italic textAnsiCode.underline- Underlined text
Foreground Colors
AnsiCode.black- Black textAnsiCode.red- Red textAnsiCode.green- Green textAnsiCode.yellow- Yellow textAnsiCode.blue- Blue textAnsiCode.magenta- Magenta textAnsiCode.cyan- Cyan textAnsiCode.white- White text
Bright Foreground Colors
AnsiCode.brightRed- Bright red textAnsiCode.brightGreen- Bright green textAnsiCode.brightYellow- Bright yellow textAnsiCode.brightBlue- Bright blue textAnsiCode.brightMagenta- Bright magenta textAnsiCode.brightCyan- Bright cyan textAnsiCode.brightWhite- Bright white text
Background Colors
AnsiCode.bgBlack- Black backgroundAnsiCode.bgRed- Red backgroundAnsiCode.bgGreen- Green backgroundAnsiCode.bgYellow- Yellow backgroundAnsiCode.bgBlue- Blue backgroundAnsiCode.bgMagenta- Magenta backgroundAnsiCode.bgCyan- Cyan backgroundAnsiCode.bgWhite- White background
Utility Methods
AnsiCode.getColorByCode(String color)- Returns the common ANSI code for a given color name
Utility Functions #
colorizeText
The package provides a utility function to directly colorize text with ANSI codes:
String colorizeText(
String text, {
List<String>? ansiCode,
String continueAnsi = AnsiCode.normal,
}
)
Parameters:
text: The text to colorizeansiCode: Optional list of ANSI codes to applycontinueAnsi: Continue ANSI code to apply after the text (default:AnsiCode.normal)
Example usage:
import 'package:colored_logger/colored_logger.dart';
import 'package:colored_logger/ansi_code.dart';
// Colorize text with a single ANSI code
String greenText = colorizeText('This text is green', ansiCode: [AnsiCode.green]);
print(greenText);
// Colorize text with multiple ANSI codes
String boldCyanText = colorizeText(
'This text is bold and cyan',
ansiCode: [AnsiCode.bold, AnsiCode.cyan]
);
print(boldCyanText);
// Colorize multiline text (each line gets colored separately)
String multilineText = colorizeText(
'Line 1\nLine 2\nLine 3',
ansiCode: [AnsiCode.yellow]
);
print(multilineText);
This function properly handles multiline text by applying the color codes to each line individually.
Example #
See the example for a complete working example.
Additional Information #
- Repository: GitHub
- Homepage: venhdev.me
- Issues: Please file issues on the GitHub repository
- Contributions: Contributions are welcome! Feel free to submit a pull request
License #
This project is licensed under the MIT License - see the LICENSE file for details.