Log::MixedColor - Outputs messages in multiple colors
version 1.000
Output log messages in color while emphasizing parts of the message in a different color. Although colour codes witin a message string can be done manually, this module is providing a simplified approach to colour logging hopefully saving time and code (and colour codes can also be inserted manually if required - i.e. they won't be stripped).
use Log::MixedColor; my $log = Log::MixedColor->new; $log->verbose(1); $log->info_msg( "This is a " . $log->quote('general info') . " message." ); $log->debug(1); $log->debug_msg( "This is a " . $log->q('debug') . " message" );
There are four types of messages:
info_msg (or info) - displayed when debug or verbose are turned on
info_msg
info
debug_msg (or dmsg) - displayed when debug is turned on
debug_msg
dmsg
err_msg (or err) - displayed all the time on STDERR
err_msg
err
fatal_msg (or fatal) - displayed all the time on STDERR and will cause the script to exit
fatal_msg
fatal
The debug and verbose methods are intended so that the script utilising this module can pass the command line option values specifying whether to operate the script logging in verbose or debug mode.
debug
verbose
use Getopt::Std; use Log::MixedColor; our( $opt_v, $opt_d ); getopts('vd'); my $log = Log::MixedColor->new( verbose => $opt_v, debug => $opt_d ); $log->info_msg( "This is a " . $log->quote('general info') . " message." ); $log->debug_msg( "This is a " . $log->q('debug') . " message" );
The debug log messages will only display when the script is run with -d and the verbose messages will be display when the script is run with -d or -v.
-d
-v
Create the Log::MixedColor object. The following can be set at creation time (defaults shown):
my $log = Log::MixedColor->new( verbose => 0, debug => 0, fatal_is_fatal => 1 );
which is equivalent to:
my $log = Log::MixedColor->new;
Put the log object in verbose mode.
$log->verbose(1);
Alias for verbose.
Put the log object in debug mode.
$log->debug(1);
Alias for debug.
Quote a portion of the message in a different color to the rest of the message
$log->debug_msg( "This is a ".$log->quote('quoted bit')." inside a message." );
Alternatively, instead of using this method, you could just use the quoting strings directly, e.g.:
$log->debug_msg( "This is a %%quoted bit## inside a message." );
Alias for quote.
quote
Sets the string used to denote the start of the text to be quoted in a different color. Default shown
$log->quote_start( '%%' );
It needs to be different from that specified by quote_end.
quote_end
Sets the string used to denote the end of the text to be quoted in a different color. Default shown.
$log->quote_end( '##' );
It needs to be different from that specified by quote_start.
quote_start
Display a message on STDOUT when the log object is in debug or verbose mode.
STDOUT
$log->info_msg( "This is a " . $log->quote('general') . " message." );
Alias for info_msg.
Display a message on STDOUT when the log object is in debug mode.
$log->debug_msg( "This is a " . $log->quote('low level') . " message." );
Alias for debug_msg.
Display a message on STDERR.
STDERR
$log->err_msg( "This is a " . $log->quote('warning') . " message." );
Alias for err_msg.
Display a message on STDERR and then exit the script.
$log->fatal_err( "This is a ".$log->quote('critical')." message so we have to stop.", 2 );
The optional second argument is the exit code the script will exit with. It defaults to 1.
1
The exit feature can be turned off by setting $log->fatal_is_fatal to false.
$log->fatal_is_fatal
Alias for fatal_err.
fatal_err
Determines whether the fatal_msg method actually causes the script to exit. It will by default.
$log->fatal_is_fatal(0);
Turning it off will make it equivalent to err_msg, but might be helpful when developing a script during which time you may not want it to be fatal, but you do when your script goes into production.
To customise the colors, pass the color strings as recognised by Term::ANSIColor to the following relevant methods or set the equivalent properties as part of new (the default is shown in brackets):
new
info_color (green)
info_color
debug_color (magenta)
debug_color
err_color (red)
err_color
info_quote_color (black on_white)
info_quote_color
debug_quote_color (blue)
debug_quote_color
err_quote_color (yellow)
err_quote_color
The fatal_err method will use the same colours as the err_msg method.
To allow for language variations and individual preferences the prefix before the output message can be customised with the following methods (defaults shown in brackets):
info_prefix (Info:)
info_prefix
debug_prefix (Debug:)
debug_prefix
err_prefix (Error:)
err_prefix
The fatal_err method will use the same prefix as the err_msg method.
Please report any bugs or feature requests in the issues section of GitHub: https://github.com/Q-Technologies/perl-Log-MixedColor. Ideally, submit a Pull Request.
Matthew Mallard <mqtech@cpan.org>
This software is copyright (c) 2019 by Matthew Mallard.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Log::MixedColor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::MixedColor
CPAN shell
perl -MCPAN -e shell install Log::MixedColor
For more information on module installation, please visit the detailed CPAN module installation guide.