Log::Message(3)
π NAME π
okay so bestie, welcome to Log::Message(3) β A generic message storing mechanism; yes the semicolon is canon and we respect it π
β¨ SYNOPSIS β¨
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();
πΈ DESCRIPTION πΈ
So Log::Message is literally a generic message storage mechanism. It lets you store messages on a stack β either shared or private β and assign meta-data to it. Some meta-data gets auto-added for you, like a timestamp and a stack trace, and some you can fill in yourself, like a tag to identify or group the message, and a level at which to handle it (for example, log it, or die with it).
Log::Message also gives you a powerful way to search through items by regexes on messages, tags and level. obsessed.
π§ Hierarchy π§
There are 4 modules of interest when dealing with the Log::Message::* modules:
Log::Messageβ provides a few methods to manipulate the stack it keeps. It has the option of keeping either a private or a public stack. More on this below.Log::Message::Itemβ these are individual message items, which are objects that contain the user message as well as the meta-data described above. See theLog::Message::Itemmanpage to see how to extract this meta-data and how to work with the Item objects. You should never need to create your own Item objects, but knowing about their methods and accessors is important if you want to write your own handlers. (See below.)Log::Message::Handlersβ these are a collection of handlers that will be called for a level that is used on aLog::Message::Itemobject. For example, if a message is logged with the'carp'level, the'carp'handler fromLog::Message::Handlerswill be called. See theLog::Message::Handlersmanpage for more explanation about how handlers work, which handlers are available and how to create your own.Log::Message::Configβ perLog::Messageobject, there is a configuration required that will fill in defaults if the user did not specify arguments to override them (like for example what tag will be set if none was provided).Log::Message::Confighandles the creation of these configurations.
Configuration can be specified in 4 ways:
- As a configuration file when you
use Log::Message - As arguments when you
use Log::Message - As a configuration file when you create a new
Log::Messageobject. (The config will then only apply to that object if you marked it as private) - As arguments when you create a new
Log::Messageobject.
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.
π Options π
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.
configβ the path to a configuration file to be read. See the manpage ofLog::Message::Configfor the required format.
These options will be overridden by any explicit arguments passed.
privateβ whether to create, by default, private or shared objects. If you choose to create shared objects, allLog::Messageobjects will use the same stack.
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.
verboseβLog::Messagemakes use of another module to validate its arguments, which is calledParams::Check, which is a lightweight, yet powerful input checker and parser. (See theParams::Checkmanpage for details.)
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'.
tagβ the tag to add to messages if none was provided. If neither your config, nor any specific arguments supply a tag, thenLog::Messagewill set it to'NONE'.
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.
levelβ"level"describes what action to take when a message is logged. Just like"tag",Log::Messagewill provide a default (which is'log') if neither your config file, nor any explicit arguments are given to override it.
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.
removeβ this indicates whether or not to automatically remove the messages from the stack when you've retrieved them. The default setting provided byLog::Messageis'0': do not remove.chronoβ this indicates whether messages should always be fetched in chronological order or not. This simply means that you can choose whether, when retrieving items, the item most recently added should be returned first, or the one that had been added most long ago.
The default is to return the newest ones first.
π Methods π
π new
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:
- Provided arguments take precedence over a configuration file.
- Arguments to
newtake precedence over options provided at"use"time - An object marked private will always have an empty stack to begin with
π store
This will create a new Item object and store it on the stack.
Possible arguments you can give to it are:
messageβ this is the only argument that is required. If no other arguments are given, you may even leave off the"message"key. The argument will then automatically be assumed to be the message.tagβ the tag to add to this message. If not provided,Log::Messagewill look in your configuration for one.levelβ the level at which this message should be handled. If not provided,Log::Messagewill look in your configuration for one.extraβ this is an array ref with arguments passed to the handler for this message, when it is called fromstore().
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.
π¦ retrieve
This will retrieve all message items matching the criteria specified from the stack.
Here are the criteria you can discriminate on:
tagβ a regex to which the tag must adhere. For exampleqr/\w/.levelβ a regex to which the level must adhere.messageβ a regex to which the message must adhere.amountβ maximum amount of errors to return.chronoβ return in chronological order, or not?removeβ remove items from the stack upon retrieval?
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.
β first
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.
π last
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.
π§Ό flush
This removes all items from the stack and returns them to the caller.
π SEE ALSO π
Log::Message::Item, Log::Message::Handlers, Log::Message::Config
π AUTHOR π
This module by Jos Boumans <kane@cpan.org>.
π«Ά Acknowledgements π«Ά
Thanks to Ann Barcomb for her suggestions.
π COPYRIGHT π
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.
π okay that's the whole page, bestie β source/version perl v5.42.0, date 2014-07-27, manual Log::Message(3). go forth and log slayfully π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Log-Message 0.08-bp160.1.2 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms