Appende...edLevels(3)

✨ NAME ✨

Okay bestie, so this is Log::Log4perl::Appender::ScreenColoredLevels β€” it lives in the User Contributed Perl Documentation manual, section 3 πŸŽ€ and it's basically a glow-up appender that colorizes messages according to level. That's the whole vibe. πŸ’…


🌈 SYNOPSIS 🌈

Here's the whole setup, literally copy-paste it:

use Log::Log4perl qw(:easy);

Log::Log4perl->init(\ <<'EOT');
  log4perl.category = DEBUG, Screen
  log4perl.appender.Screen = \
      Log::Log4perl::Appender::ScreenColoredLevels
  log4perl.appender.Screen.layout = \
      Log::Log4perl::Layout::PatternLayout
  log4perl.appender.Screen.layout.ConversionPattern = \
      %d %F{1} %L> %m %n
EOT

  # Appears black
DEBUG "Debug Message";

  # Appears green
INFO  "Info Message";

  # Appears blue
WARN  "Warn Message";

  # Appears magenta
ERROR "Error Message";

  # Appears red
FATAL "Fatal Message";

Look at those comments β€” the colors are already spelled out for you, no cap. πŸ–€πŸ’šπŸ’™πŸ’œβ€οΈ


πŸ¦‹ DESCRIPTION πŸ¦‹

So this appender is lowkey the same as Log::Log4perl::Appender::Screen, except β€” and this is the main character part β€” it colorizes its output, based on the priority of the message sent. It's giving ✨aesthetic logs✨.

You can configure the colors and attributes used for the different levels by specifying them in your configuration:

log4perl.appender.Screen.color.TRACE=cyan
log4perl.appender.Screen.color.DEBUG=bold blue

And here's the cute part β€” 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. Any of them. Go off. πŸ”₯

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. (iykyk πŸ‘€)


The default values for these options are:

  • Trace β€” Yellow
  • Debug β€” None (whatever the terminal default is)
  • Info β€” Green
  • Warn β€” Blue
  • Error β€” Magenta
  • Fatal β€” Red

Honestly? A tasteful palette. The person who picked these ate. πŸ‘‘


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 STDERR
my $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},
);
⚠️ Heads up: that hash key matching is case-sensitive, bestie. ERROR works, error does not. Don't get caught out. πŸ’€

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. That's it. That's the whole fix. πŸͺŸβœ¨


πŸŽ€ LICENSE πŸŽ€

Copyright 2002-2013 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


πŸ‘‘ AUTHOR πŸ‘‘

Please contribute patches to the project on Github:

http://github.com/mschilli/log4perl

Send bug reports or requests for enhancements to the authors via our

MAILING LIST (questions, bug reports, suggestions/patches): log4perl-devel@lists.sourceforge.net

Authors (please contact them via the list above, not directly): Mike Schilli <m@perlmeister.com>, Kevin Goess <cpan@goess.org>

Contributors (in alphabetical order): Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy Grundman, Paul Harrington, Alexander Hartmaier David Hull, Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang.


πŸ’œ That's the whole page, bestie β€” go make your terminal pretty. This edition was rendered with perl v5.42.0 and dated 2022-10-22. Slay. πŸ’…βœ¨
πŸ“¦ where this page comes from