Log::Log4perl::Appender::ScreenColoredLevels3pm
Colorize messages according to level
Usage
use Log::Log4perl qw(:easy);Log::Log4perl->init(\ <<'EOT');log4perl.category = DEBUG, Screenlog4perl.appender.Screen = \Log::Log4perl::Appender::ScreenColoredLevelslog4perl.appender.Screen.layout = \Log::Log4perl::Layout::PatternLayoutlog4perl.appender.Screen.layout.ConversionPattern = \%d %F{1} %L> %m %nEOT# Appears blackDEBUG "Debug Message";# Appears greenINFO "Info Message";# Appears blueWARN "Warn Message";# Appears magentaERROR "Error Message";# Appears redFATAL "Fatal Message";
Overview
This appender acts like Log::Log4perl::Appender::Screen, except that it colorizes its output, based on the priority of the message sent.
You can configure the colors and attributes used for the different levels, by specifying them in your configuration:
log4perl.appender.Screen.color.TRACE=cyanlog4perl.appender.Screen.color.DEBUG=bold blue
You can also specify nothing, to indicate that level should not have coloring applied, which means the text will be whatever the default color for your terminal is. This is the default for debug messages.
log4perl.appender.Screen.color.DEBUG=
You can use any attribute supported by Term::ANSIColor as a configuration option.
log4perl.appender.Screen.color.FATAL=\bold underline blink red on_white
The commonly used colors and attributes are:
attributes
|
BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK |
|---|---|
colors
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE |
background colors
|
ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, ON_WHITE |
See Term::ANSIColor for a complete list, and information on which are supported by various common terminal emulators.
The default values for these options are:
Trace
|
Yellow |
|---|---|
Debug
|
None (whatever the terminal default is) |
Info
|
Green |
Warn
|
Blue |
Error
|
Magenta |
Fatal
|
Red |
The constructor new() takes an optional parameter "stderr", if set to a true value, the appender will log all levels to STDERR. If "stderr" is set to a false value, it will log all levels to STDOUT. Otherwise, "stderr" may be set to a hash, with a key for each "log4p_level" and a truthy value to dynamically use stderr. The default setting for "stderr" is 1, so all messages will be logged to STDERR by default.
# All messages/levels to STDERRmy $app = Log::Log4perl::Appender::Screen->new(stderr => 1,);# Only ERROR and FATAL to STDERR (case-sensitive)my $app = Log::Log4perl::Appender::Screen->new(stderr => { ERROR => 1, FATAL => 1},);
The constructor can also take an optional parameter "color", whose value is a hashref of color configuration options, any levels that are not included in the hashref will be set to their default values.
Using ScreenColoredLevels on Windows
Note that if you're using this appender on Windows, you need to fetch Win32::Console::ANSI from CPAN and add
use Win32::Console::ANSI;
to your script.
Releases
12 releases since Debian 3.1 (Sarge); the text changed 9 times.