printLog function
Prints colored log messages to stdout for IO platforms (Dart VM, Flutter).
This function provides debug logging with color coding for different types of messages. It only outputs messages when debug mode is enabled, making it safe to use in production code.
The function supports various colors for different message types:
- LogColor.red - Errors and failures
- LogColor.green - Success messages
- LogColor.yellow - Warnings
- LogColor.blue - Information and progress
- LogColor.orange - Highlights and important notes
- LogColor.cyan - Debug details
Parameters:
message
: The log message to printdebug
: Whether debug mode is enabled (only prints if true)color
: Color to use for the message (default: reset/white)
Example:
printLog('Starting scraping process...', debug, color: LogColor.blue);
printLog('Data extracted successfully!', debug, color: LogColor.green);
printLog('Network error occurred', debug, color: LogColor.red);
Implementation
void printLog(String message, bool debug, {LogColor color = LogColor.reset}) {
if (!debug) return;
/// ANSI color codes for terminal output
const Map<LogColor, String> colors = {
LogColor.reset: '\x1B[0m',
LogColor.black: '\x1B[30m',
LogColor.red: '\x1B[31m',
LogColor.green: '\x1B[32m',
LogColor.yellow: '\x1B[33m',
LogColor.blue: '\x1B[34m',
LogColor.magenta: '\x1B[35m',
LogColor.cyan: '\x1B[36m',
LogColor.white: '\x1B[37m',
LogColor.orange: '\x1B[38;5;208m',
};
/// Write colored message to stdout with newline and reset color
stdout.write('${colors[color]}$message\n${colors[LogColor.reset]}');
}