Log::Message(3pm) | User Contributed Perl Documentation | Log::Message(3pm) |
Log::Message - A generic message storing mechanism;
use Log::Message private => 0, config => '/our/cf_file'; my $log = Log::Message->new( private => 1, level => 'log', config => '/my/cf_file', ); $log->store('this is my first message'); $log->store( message => 'message #2', tag => 'MY_TAG', level => 'carp', extra => ['this is an argument to the handler'], ); my @last_five_items = $log->retrieve(5); my @items = $log->retrieve( tag => qr/my_tag/i, message => qr/\d/, remove => 1, ); my @items = $log->final( level => qr/carp/, amount => 2 ); my $first_error = $log->first() # croak with the last error on the stack $log->final->croak; # empty the stack $log->flush();
Log::Message is a generic message storage mechanism. It allows you to store messages on a stack -- either shared or private -- and assign meta-data to it. Some meta-data will automatically be added for you, like a timestamp and a stack trace, but some can be filled in by the user, like a tag by which to identify it or group it, and a level at which to handle the message (for example, log it, or die with it)
Log::Message also provides a powerful way of searching through items by regexes on messages, tags and level.
There are 4 modules of interest when dealing with the Log::Message::* modules:
Configuration can be specified in 4 ways:
You should never need to use the Log::Message::Config module yourself, as this is transparently done by Log::Message, but its manpage does provide an explanation of how you can create a config file.
When using Log::Message, or creating a new Log::Message object, you can supply various options to alter its behaviour. Of course, there are sensible defaults should you choose to omit these options.
Below an explanation of all the options and how they work.
These options will be overridden by any explicit arguments passed.
This means that even though every module may make its own $log object they will still be sharing the same error stack on which they are putting errors and from which they are retrieving.
This can be useful in big projects.
If you choose to create a private object, then the stack will of course be private to this object, but it will still fall back to the shared config should no private config or overriding arguments be provided.
The verbose setting will control whether this module will generate warnings if something improper is passed as input, or merely silently returns undef, at which point Log::Message will generate a warning.
It's best to just leave this at its default value, which is '1'
Tags are useful for searching on or grouping by. For example, you could tag all the messages you want to go to the user as 'USER ERROR' and all those that are only debug information with 'DEBUG'.
At the end of your program, you could then print all the ones tagged 'USER ERROR' to STDOUT, and those marked 'DEBUG' to a log file.
See the Log::Message::Handlers manpage to see what handlers are available by default and what they do, as well as to how to add your own handlers.
The default is to return the newest ones first
This creates a new Log::Message object; The parameters it takes are described in the "Options" section below and let it just be repeated that you can use these options like this:
my $log = Log::Message->new( %options );
as well as during "use" time, like this:
use Log::Message option1 => value, option2 => value
There are but 3 rules to keep in mind:
This will create a new Item object and store it on the stack.
Possible arguments you can give to it are:
The handler will receive them as a normal list
store() will return true upon success and undef upon failure, as well as issue a warning as to why it failed.
This will retrieve all message items matching the criteria specified from the stack.
Here are the criteria you can discriminate on:
In scalar context it will return the first item matching your criteria and in list context, it will return all of them.
If an error occurs while retrieving, a warning will be issued and undef will be returned.
This is a shortcut for retrieving the first item(s) stored on the stack. It will default to only retrieving one if called with no arguments, and will always return results in chronological order.
If you only supply one argument, it is assumed to be the amount you wish returned.
Furthermore, it can take the same arguments as "retrieve" can.
This is a shortcut for retrieving the last item(s) stored on the stack. It will default to only retrieving one if called with no arguments, and will always return results in reverse chronological order.
If you only supply one argument, it is assumed to be the amount you wish returned.
Furthermore, it can take the same arguments as "retrieve" can.
This removes all items from the stack and returns them to the caller
Log::Message::Item, Log::Message::Handlers, Log::Message::Config
This module by Jos Boumans <kane@cpan.org>.
Thanks to Ann Barcomb for her suggestions.
This module is copyright (c) 2002 Jos Boumans <kane@cpan.org>. All rights reserved.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
2022-10-14 | perl v5.34.0 |