1 package Log::Message;
\r
5 use Params::Check qw[check];
\r
6 use Log::Message::Item;
\r
7 use Log::Message::Config;
\r
8 use Locale::Maketext::Simple Style => 'gettext';
\r
10 local $Params::Check::VERBOSE = 1;
\r
13 use vars qw[$VERSION @ISA $STACK $CONFIG];
\r
25 Log::Message - A generic message storing mechanism;
\r
29 use Log::Message private => 0, config => '/our/cf_file';
\r
31 my $log = Log::Message->new( private => 1,
\r
33 config => '/my/cf_file',
\r
36 $log->store('this is my first message');
\r
38 $log->store( message => 'message #2',
\r
41 extra => ['this is an argument to the handler'],
\r
44 my @last_five_items = $log->retrieve(5);
\r
46 my @items = $log->retrieve( tag => qr/my_tag/i,
\r
51 my @items = $log->final( level => qr/carp/, amount => 2 );
\r
53 my $first_error = $log->first()
\r
55 # croak with the last error on the stack
\r
64 Log::Message is a generic message storage mechanism.
\r
65 It allows you to store messages on a stack -- either shared or private
\r
66 -- and assign meta-data to it.
\r
67 Some meta-data will automatically be added for you, like a timestamp
\r
68 and a stack trace, but some can be filled in by the user, like a tag
\r
69 by which to identify it or group it, and a level at which to handle
\r
70 the message (for example, log it, or die with it)
\r
72 Log::Message also provides a powerful way of searching through items
\r
73 by regexes on messages, tags and level.
\r
77 There are 4 modules of interest when dealing with the Log::Message::*
\r
84 Log::Message provides a few methods to manipulate the stack it keeps.
\r
85 It has the option of keeping either a private or a public stack.
\r
88 =item Log::Message::Item
\r
90 These are individual message items, which are objects that contain
\r
91 the user message as well as the meta-data described above.
\r
92 See the L<Log::Message::Item> manpage to see how to extract this
\r
93 meta-data and how to work with the Item objects.
\r
94 You should never need to create your own Item objects, but knowing
\r
95 about their methods and accessors is important if you want to write
\r
96 your own handlers. (See below)
\r
98 =item Log::Message::Handlers
\r
100 These are a collection of handlers that will be called for a level
\r
101 that is used on a L<Log::Message::Item> object.
\r
102 For example, if a message is logged with the 'carp' level, the 'carp'
\r
103 handler from L<Log::Message::Handlers> will be called.
\r
104 See the L<Log::Message::Handlers> manpage for more explanation about how
\r
105 handlers work, which one are available and how to create your own.
\r
107 =item Log::Message::Config
\r
109 Per Log::Message object, there is a configuration required that will
\r
110 fill in defaults if the user did not specify arguments to override
\r
111 them (like for example what tag will be set if none was provided),
\r
112 L<Log::Message::Config> handles the creation of these configurations.
\r
114 Configuration can be specified in 4 ways:
\r
120 As a configuration file when you C<use Log::Message>
\r
124 As arguments when you C<use Log::Message>
\r
128 As a configuration file when you create a new L<Log::Message> object.
\r
129 (The config will then only apply to that object if you marked it as
\r
134 As arguments when you create a new Log::Message object.
\r
136 You should never need to use the L<Log::Message::Config> module yourself,
\r
137 as this is transparently done by L<Log::Message>, but its manpage does
\r
138 provide an explanation of how you can create a config file.
\r
146 When using Log::Message, or creating a new Log::Message object, you can
\r
147 supply various options to alter its behaviour.
\r
148 Of course, there are sensible defaults should you choose to omit these
\r
151 Below an explanation of all the options and how they work.
\r
157 The path to a configuration file to be read.
\r
158 See the manpage of L<Log::Message::Config> for the required format
\r
160 These options will be overridden by any explicit arguments passed.
\r
164 Whether to create, by default, private or shared objects.
\r
165 If you choose to create shared objects, all Log::Message objects will
\r
166 use the same stack.
\r
168 This means that even though every module may make its own $log object
\r
169 they will still be sharing the same error stack on which they are
\r
170 putting errors and from which they are retrieving.
\r
172 This can be useful in big projects.
\r
174 If you choose to create a private object, then the stack will of
\r
175 course be private to this object, but it will still fall back to the
\r
176 shared config should no private config or overriding arguments be
\r
181 Log::Message makes use of another module to validate its arguments,
\r
182 which is called L<Params::Check>, which is a lightweight, yet
\r
183 powerful input checker and parser. (See the L<Params::Check>
\r
184 manpage for details).
\r
186 The verbose setting will control whether this module will
\r
187 generate warnings if something improper is passed as input, or merely
\r
188 silently returns undef, at which point Log::Message will generate a
\r
191 It's best to just leave this at its default value, which is '1'
\r
195 The tag to add to messages if none was provided. If neither your
\r
196 config, nor any specific arguments supply a tag, then Log::Message will
\r
199 Tags are useful for searching on or grouping by. For example, you
\r
200 could tag all the messages you want to go to the user as 'USER ERROR'
\r
201 and all those that are only debug information with 'DEBUG'.
\r
203 At the end of your program, you could then print all the ones tagged
\r
204 'USER ERROR' to STDOUT, and those marked 'DEBUG' to a log file.
\r
208 C<level> describes what action to take when a message is logged. Just
\r
209 like C<tag>, Log::Message will provide a default (which is 'log') if
\r
210 neither your config file, nor any explicit arguments are given to
\r
213 See the Log::Message::Handlers manpage to see what handlers are
\r
214 available by default and what they do, as well as to how to add your
\r
219 This indicates whether or not to automatically remove the messages
\r
220 from the stack when you've retrieved them.
\r
221 The default setting provided by Log::Message is '0': do not remove.
\r
225 This indicates whether messages should always be fetched in
\r
226 chronological order or not.
\r
227 This simply means that you can choose whether, when retrieving items,
\r
228 the item most recently added should be returned first, or the one that
\r
229 had been added most long ago.
\r
231 The default is to return the newest ones first
\r
243 $CONFIG = new Log::Message::Config( %hash )
\r
244 or die loc(qq[Problem initialising %1], __PACKAGE__);
\r
252 This creates a new Log::Message object; The parameters it takes are
\r
253 described in the C<Options> section below and let it just be repeated
\r
254 that you can use these options like this:
\r
256 my $log = Log::Message->new( %options );
\r
258 as well as during C<use> time, like this:
\r
260 use Log::Message option1 => value, option2 => value
\r
262 There are but 3 rules to keep in mind:
\r
268 Provided arguments take precedence over a configuration file.
\r
272 Arguments to new take precedence over options provided at C<use> time
\r
276 An object marked private will always have an empty stack to begin with
\r
286 my $conf = new Log::Message::Config( %hash, default => $CONFIG ) or return undef;
\r
288 if( $conf->private || $CONFIG->private ) {
\r
290 return _new_stack( $class, config => $conf );
\r
293 my $obj = _new_stack( $class, config => $conf, stack => $STACK );
\r
295 ### if it was an empty stack, this was the first object
\r
296 ### in that case, set the global stack to match it for
\r
297 ### subsequent new, non-private objects
\r
298 $STACK = $obj->{STACK} unless scalar @$STACK;
\r
309 stack => { default => [] },
\r
310 config => { default => bless( {}, 'Log::Message::Config'),
\r
316 my $args = check( $tmpl, \%hash, $CONFIG->verbose ) or (
\r
317 warn(loc(q[Could not create a new stack object: %1],
\r
318 Params::Check->last_error)
\r
324 my %self = map { uc, $args->{$_} } keys %$args;
\r
326 return bless \%self, $class;
\r
333 return defined $self->{CONFIG}->$what()
\r
334 ? $self->{CONFIG}->$what()
\r
335 : defined $CONFIG->$what()
\r
337 : undef; # should never get here
\r
342 This will create a new Item object and store it on the stack.
\r
344 Possible arguments you can give to it are:
\r
350 This is the only argument that is required. If no other arguments
\r
351 are given, you may even leave off the C<message> key. The argument
\r
352 will then automatically be assumed to be the message.
\r
356 The tag to add to this message. If not provided, Log::Message will look
\r
357 in your configuration for one.
\r
361 The level at which this message should be handled. If not provided,
\r
362 Log::Message will look in your configuration for one.
\r
366 This is an array ref with arguments passed to the handler for this
\r
367 message, when it is called from store();
\r
369 The handler will receive them as a normal list
\r
373 store() will return true upon success and undef upon failure, as well
\r
374 as issue a warning as to why it failed.
\r
378 ### should extra be stored in the item object perhaps for later retrieval?
\r
389 tag => { default => $self->_get_conf('tag') },
\r
390 level => { default => $self->_get_conf('level'), },
\r
391 extra => { default => [], strict_type => 1 },
\r
394 ### single arg means just the message
\r
395 ### otherwise, they are named
\r
397 $hash{message} = shift;
\r
402 my $args = check( $tmpl, \%hash ) or (
\r
403 warn( loc(q[Could not store error: %1], Params::Check->last_error) ),
\r
407 my $extra = delete $args->{extra};
\r
408 my $item = Log::Message::Item->new( %$args,
\r
410 id => scalar @{$self->{STACK}}
\r
412 or ( warn( loc(q[Could not create new log item!]) ), return undef );
\r
414 push @{$self->{STACK}}, $item;
\r
416 { no strict 'refs';
\r
418 my $sub = $args->{level};
\r
420 $item->$sub( @$extra );
\r
428 This will retrieve all message items matching the criteria specified
\r
431 Here are the criteria you can discriminate on:
\r
437 A regex to which the tag must adhere. For example C<qr/\w/>.
\r
441 A regex to which the level must adhere.
\r
445 A regex to which the message must adhere.
\r
449 Maximum amount of errors to return
\r
453 Return in chronological order, or not?
\r
457 Remove items from the stack upon retrieval?
\r
461 In scalar context it will return the first item matching your criteria
\r
462 and in list context, it will return all of them.
\r
464 If an error occurs while retrieving, a warning will be issued and
\r
465 undef will be returned.
\r
474 tag => { default => qr/.*/ },
\r
475 level => { default => qr/.*/ },
\r
476 message => { default => qr/.*/ },
\r
477 amount => { default => '' },
\r
478 remove => { default => $self->_get_conf('remove') },
\r
479 chrono => { default => $self->_get_conf('chrono') },
\r
482 ### single arg means just the amount
\r
483 ### otherwise, they are named
\r
485 $hash{amount} = shift;
\r
490 my $args = check( $tmpl, \%hash ) or (
\r
491 warn( loc(q[Could not parse input: %1], Params::Check->last_error) ),
\r
496 grep { $_->tag =~ /$args->{tag}/ ? 1 : 0 }
\r
497 grep { $_->level =~ /$args->{level}/ ? 1 : 0 }
\r
498 grep { $_->message =~ /$args->{message}/ ? 1 : 0 }
\r
501 ? @{$self->{STACK}}
\r
502 : reverse @{$self->{STACK}};
\r
504 my $amount = $args->{amount} || scalar @list;
\r
507 $args->{remove} ? $_->remove : $_
\r
508 } scalar @list > $amount
\r
509 ? splice(@list,0,$amount)
\r
512 return wantarray ? @rv : $rv[0];
\r
517 This is a shortcut for retrieving the first item(s) stored on the
\r
518 stack. It will default to only retrieving one if called with no
\r
519 arguments, and will always return results in chronological order.
\r
521 If you only supply one argument, it is assumed to be the amount you
\r
524 Furthermore, it can take the same arguments as C<retrieve> can.
\r
531 my $amt = @_ == 1 ? shift : 1;
\r
532 return $self->retrieve( amount => $amt, @_, chrono => 1 );
\r
537 This is a shortcut for retrieving the last item(s) stored on the
\r
538 stack. It will default to only retrieving one if called with no
\r
539 arguments, and will always return results in reverse chronological
\r
542 If you only supply one argument, it is assumed to be the amount you
\r
545 Furthermore, it can take the same arguments as C<retrieve> can.
\r
552 my $amt = @_ == 1 ? shift : 1;
\r
553 return $self->retrieve( amount => $amt, @_, chrono => 0 );
\r
558 This removes all items from the stack and returns them to the caller
\r
565 return splice @{$self->{STACK}};
\r
570 L<Log::Message::Item>, L<Log::Message::Handlers>, L<Log::Message::Config>
\r
575 Jos Boumans E<lt>kane@cpan.orgE<gt>.
\r
577 =head1 Acknowledgements
\r
579 Thanks to Ann Barcomb for her suggestions.
\r
584 copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.
\r
585 All rights reserved.
\r
587 This library is free software;
\r
588 you may redistribute and/or modify it under the same
\r
589 terms as Perl itself.
\r
596 # c-indentation-style: bsd
\r
597 # c-basic-offset: 4
\r
598 # indent-tabs-mode: nil
\r
600 # vim: expandtab shiftwidth=4:
\r