colored_logger 1.1.0
colored_logger: ^1.1.0 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 name
ColoredLogger.custom('Custom message with color name',
colorName: '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 colorName = 'normal',
List<String>? ansiCode,
})
Parameters:
message
: The message to logprefix
: Optional prefix to add before the message (default: empty string)colorName
: Color name to use (e.g., 'red', 'green', 'blue')ansiCode
: List of ANSI codes to apply (takes precedence over colorName 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.getColorByName(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,
List<String> forwardTo = const [AnsiCode.normal],
})
Parameters:
text
: The text to colorizeansiCode
: Optional list of ANSI codes to apply to the textforwardTo
: List of ANSI codes to apply after the text (default:[AnsiCode.normal]
). Used to forward styling to the next line or reset to 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);
// Preserve color after text (for continued styling)
String preservedColorText = colorizeText(
'This begins a colored section',
ansiCode: [AnsiCode.red],
forwardTo: [AnsiCode.red] // Continue with red text after this
);
print(preservedColorText + ' and this continues in the same color');
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.