Colored Logger
A simple yet powerful colored logging utility for Dart and Flutter applications that enhances console output with ANSI colors and styles.
Features
- Color-coded log levels: Easily distinguish between different types of logs (info, success, warning, error)
- ANSI color support: Includes a variety of ANSI color codes for terminal output
- RGB & 256 color support: Create millions of colors with true color RGB and 8-bit color support
- Predefined styles: Ready-to-use semantic styles, colors, text decorations
Installation
Add the package to your pubspec.yaml
file:
dependencies:
colored_logger: ^1.2.3
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';
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',
ansiCodes: [AnsiCode.bold, AnsiCode.cyan],
prefix: '[STYLED] ');
// Using predefined styles from AnsiColors
ColoredLogger.custom('Error style message',
ansiCodes: AnsiColors.error,
prefix: '[ERROR] ');
}
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>? ansiCodes,
})
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')ansiCodes
: List of ANSI codes to apply (takes precedence over colorName if provided)colored
: If false, the message will not be colored (default: true)
AnsiCode Class
Deprecated: The AnsiCode
class is deprecated and will be removed in a future release (v2.0.0). Use the Ansi
class instead.
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 textAnsiCode.blink
- Blinking text (not supported in all terminals)AnsiCode.reverse
- Reverse/invert foreground and background colorsAnsiCode.hidden
- Hidden/invisible textAnsiCode.strikethrough
- Strikethrough 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
Advanced Color Methods
AnsiCode.fg256(int color)
- Returns a foreground color using 8-bit color (256 colors)AnsiCode.bg256(int color)
- Returns a background color using 8-bit color (256 colors)AnsiCode.fgRGB(int r, int g, int b)
- Returns a foreground color using RGB valuesAnsiCode.bgRGB(int r, int g, int b)
- Returns a background color using RGB values
AnsiColors Class
Deprecated: The AnsiColors
class is deprecated and will be removed in a future release (v2.0.0). Use the Ansi
class instead.
The AnsiColors
class provides enhanced styling options and predefined color combinations for terminal output.
Text Decorations
AnsiColors.blink
- Blink text (not supported in all terminals)AnsiColors.rapidBlink
- Rapid blink text (not widely supported)AnsiColors.reverse
- Reverse/invert foreground and background colorsAnsiColors.hidden
- Hidden text (not widely supported)AnsiColors.strikethrough
- Strikethrough textAnsiColors.doubleUnderline
- Double underline (not widely supported)AnsiColors.framed
- Framed text (not widely supported)AnsiColors.encircled
- Encircled text (not widely supported)AnsiColors.overlined
- Overlined text (not widely supported)
Advanced Color Methods
AnsiColors.fg256(int color)
- Returns a foreground color using 8-bit color (256 colors)AnsiColors.bg256(int color)
- Returns a background color using 8-bit color (256 colors)AnsiColors.fgRGB(int r, int g, int b)
- Returns a foreground color using RGB valuesAnsiColors.bgRGB(int r, int g, int b)
- Returns a background color using RGB values
Predefined Styles
AnsiColors.error
- Bold red text (for error messages)AnsiColors.success
- Bold green text (for success messages)AnsiColors.warning
- Bold yellow text (for warning messages)AnsiColors.info
- Bold blue text (for info messages)AnsiColors.debug
- Cyan text (for debug messages)AnsiColors.highlight
- Black text on yellow backgroundAnsiColors.alert
- White text on red backgroundAnsiColors.notice
- Black text on cyan backgroundAnsiColors.primary
- Bold bright blue textAnsiColors.secondary
- Bright magenta textAnsiColors.tertiary
- Bright cyan textAnsiColors.muted
- Dim white textAnsiColors.emphasis
- Bold italic textAnsiColors.link
- Underlined cyan textAnsiColors.code
- Bright white text on bright black backgroundAnsiColors.quote
- Italic green textAnsiColors.header
- Bold underlined bright white textAnsiColors.subheader
- Underlined bright white text
Named Colors
AnsiColors.coral
- Coral color (RGB: 255, 127, 80)AnsiColors.tomato
- Tomato color (RGB: 255, 99, 71)AnsiColors.orangeRed
- Orange red color (RGB: 255, 69, 0)AnsiColors.gold
- Gold color (RGB: 255, 215, 0)AnsiColors.limeGreen
- Lime green color (RGB: 50, 205, 50)AnsiColors.forestGreen
- Forest green color (RGB: 34, 139, 34)AnsiColors.teal
- Teal color (RGB: 0, 128, 128)AnsiColors.deepSkyBlue
- Deep sky blue color (RGB: 0, 191, 255)AnsiColors.royalBlue
- Royal blue color (RGB: 65, 105, 225)AnsiColors.navyBlue
- Navy blue color (RGB: 0, 0, 128)AnsiColors.purple
- Purple color (RGB: 128, 0, 128)AnsiColors.violet
- Violet color (RGB: 238, 130, 238)AnsiColors.hotPink
- Hot pink color (RGB: 255, 105, 180)AnsiColors.deepPink
- Deep pink color (RGB: 255, 20, 147)AnsiColors.chocolate
- Chocolate color (RGB: 210, 105, 30)AnsiColors.sienna
- Sienna color (RGB: 160, 82, 45)AnsiColors.silver
- Silver color (RGB: 192, 192, 192)AnsiColors.gray
- Gray color (RGB: 128, 128, 128)AnsiColors.slateGray
- Slate gray color (RGB: 112, 128, 144)
Utility Functions
colorize
The package provides a new utility function to directly colorize text with ANSI codes using the Ansi
class:
String colorize(
dynamic input,
Ansi ansi, {
String prefix = '',
String suffix = '',
bool colored = true,
})
Parameters:
input
: The text to colorize.ansi
: TheAnsi
code to apply to the text.prefix
: An optional prefix to add before the text.suffix
: An optional suffix to add after the text.colored
: Iffalse
, the text will not be colored.
Example usage:
import 'package:colored_logger/colored_logger.dart';
// Colorize text with a single ANSI code
String greenText = colorize('This text is green', Ansi.green);
print(greenText);
// Colorize text with multiple ANSI codes
String boldCyanText = colorize(
'This text is bold and cyan',
Ansi.bold.combine(Ansi.cyan)
);
print(boldCyanText);
// Using RGB colors
String rgbText = colorize(
'This text uses RGB color',
Ansi.fgRgb(255, 100, 50)
);
print(rgbText);
// Using predefined styles from Ansi
String errorText = colorize(
'This is an error message',
Ansi.red.bold
);
print(errorText);
// Using prefix and suffix
String prefixedText = colorize(
'Important message',
Ansi.red.bold,
prefix: '[ALERT] ',
suffix: ' !'
);
print(prefixedText);
Deprecated: The colorizeText
function is deprecated and will be removed in a future release (v2.0.0). Use colorize
instead.
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.