This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix typos (spelling errors) in cpan/Log-Message/*.
[perl5.git] / cpan / Log-Message / lib / Log / Message / Item.pm
CommitLineData
e3f7a951
RGS
1package Log::Message::Item;
2
3use strict;
4use Params::Check qw[check];
5use Log::Message::Handlers;
6
7### for the messages to store ###
8use Carp ();
9
10BEGIN {
11 use vars qw[$AUTOLOAD $VERSION];
12
13 $VERSION = $Log::Message::VERSION;
14}
15
16### create a new item.
17### note that only an id (position on the stack), message and a reference
18### to its parent are required. all the other things it can fill in itself
19sub new {
20 my $class = shift;
21 my %hash = @_;
22
23 my $tmpl = {
24 when => { no_override => 1, default => scalar localtime },
25 id => { required => 1 },
26 message => { required => 1 },
27 parent => { required => 1 },
28 level => { default => '' }, # default may be conf dependant
29 tag => { default => '' }, # default may be conf dependant
30 longmess => { default => _clean(Carp::longmess()) },
31 shortmess => { default => _clean(Carp::shortmess())},
32 };
33
34 my $args = check($tmpl, \%hash) or return undef;
35
36 return bless $args, $class;
37}
38
39sub _clean { map { s/\s*//; chomp; $_ } shift; }
40
41sub remove {
42 my $item = shift;
43 my $self = $item->parent;
44
45 return splice( @{$self->{STACK}}, $item->id, 1, undef );
46}
47
48sub AUTOLOAD {
49 my $self = $_[0];
50
51 $AUTOLOAD =~ s/.+:://;
52
53 return $self->{$AUTOLOAD} if exists $self->{$AUTOLOAD};
54
55 local $Carp::CarpLevel = $Carp::CarpLevel + 3;
56
57 { no strict 'refs';
58 return *{"Log::Message::Handlers::${AUTOLOAD}"}->(@_);
59 }
60}
61
62sub DESTROY { 1 }
63
641;
65
66__END__
67
68=pod
69
70=head1 NAME
71
72Log::Message::Item - Message objects for Log::Message
73
74=head1 SYNOPSIS
75
76 # Implicitly used by Log::Message to create Log::Message::Item objects
77
78 print "this is the message's id: ", $item->id;
79
80 print "this is the message stored: ", $item->message;
81
82 print "this is when it happened: ", $item->when;
83
84 print "the message was tagged: ", $item->tag;
85
86 print "this was the severity level: ", $item->level;
87
88 $item->remove; # delete the item from the stack it was on
89
90 # Besides these methods, you can also call the handlers on
96ee1e35 91 # the object specifically.
e3f7a951
RGS
92 # See the Log::Message::Handlers manpage for documentation on what
93 # handlers are available by default and how to add your own
94
95
96=head1 DESCRIPTION
97
98Log::Message::Item is a class that generates generic Log items.
99These items are stored on a Log::Message stack, so see the Log::Message
100manpage about details how to retrieve them.
101
102You should probably not create new items by yourself, but use the
103storing mechanism provided by Log::Message.
104
105However, the accessors and handlers are of interest if you want to do
106fine tuning of how your messages are handled.
107
108The accessors and methods are described below, the handlers are
109documented in the Log::Message::Handlers manpage.
110
111=head1 Methods and Accessors
112
113=head2 remove
114
115Calling remove will remove the object from the stack it was on, so it
116will not show up any more in subsequent fetches of messages.
117
118You can still call accessors and handlers on it however, to handle it
119as you will.
120
121=head2 id
122
123Returns the internal ID of the item. This may be useful for comparing
124since the ID is incremented each time a new item is created.
125Therefore, an item with ID 4 must have been logged before an item with
126ID 9.
127
128=head2 when
129
130Returns the timestamp of when the message was logged
131
132=head2 message
133
134The actual message that was stored
135
136=head2 level
137
138The severity type of this message, as well as the name of the handler
139that was called upon storing it.
140
141=head2 tag
142
143Returns the identification tag that was put on the message.
144
145=head2 shortmess
146
147Returns the equivalent of a C<Carp::shortmess> for this item.
148See the C<Carp> manpage for details.
149
150=head2 longmess
151
152Returns the equivalent of a C<Carp::longmess> for this item, which
153is essentially a stack trace.
154See the C<Carp> manpage for details.
155
156=head2 parent
157
158Returns a reference to the Log::Message object that stored this item.
159This is useful if you want to have access to the full stack in a
160handler.
161
162=head1 SEE ALSO
163
164L<Log::Message>, L<Log::Message::Handlers>, L<Log::Message::Config>
165
166=head1 AUTHOR
167
168This module by
169Jos Boumans E<lt>kane@cpan.orgE<gt>.
170
171=head1 Acknowledgements
172
173Thanks to Ann Barcomb for her suggestions.
174
175=head1 COPYRIGHT
176
177This module is
178copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.
179All rights reserved.
180
181This library is free software;
182you may redistribute and/or modify it under the same
183terms as Perl itself.
184
185=cut
186
187# Local variables:
188# c-indentation-style: bsd
189# c-basic-offset: 4
190# indent-tabs-mode: nil
191# End:
192# vim: expandtab shiftwidth=4: