start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Log.pm
CommitLineData
fc7ec1d9 1package Catalyst::Log;
2
0fc2d522 3use Class::C3;
e5ecd5bc 4use Moose;
f63c03e4 5use Data::Dump;
9b2bc37b 6
a6433a05 7our %LEVELS = ();
8
3cdcf968 9has level => (is => 'rw');
5fb12dbb 10has _body => (is => 'rw');
3cdcf968 11has abort => (is => 'rw');
12
2f9e6321 13{
a6433a05 14 my @levels = qw[ debug info warn error fatal ];
846772b7 15
16 for ( my $i = 0 ; $i < @levels ; $i++ ) {
17
18 my $name = $levels[$i];
19 my $level = 1 << $i;
20
a6433a05 21 $LEVELS{$name} = $level;
22
23 no strict 'refs';
24
846772b7 25 *{$name} = sub {
26 my $self = shift;
27
3cdcf968 28 if ( $self->level & $level ) {
846772b7 29 $self->_log( $name, @_ );
30 }
31 };
32
33 *{"is_$name"} = sub {
34 my $self = shift;
3cdcf968 35 return $self->level & $level;
846772b7 36 };
37 }
a6433a05 38}
39
0fc2d522 40sub new {
a6433a05 41 my $class = shift;
0fc2d522 42 my $self = $class->next::method;
a6433a05 43 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
44 return $self;
0fc2d522 45}
a6433a05 46
47sub levels {
48 my ( $self, @levels ) = @_;
49 $self->level(0);
50 $self->enable(@levels);
51}
52
53sub enable {
54 my ( $self, @levels ) = @_;
3cdcf968 55 my $level = $self->level;
56 for(map { $LEVELS{$_} } @levels){
57 $level |= $_;
58 }
59 $self->level($level);
a6433a05 60}
846772b7 61
a6433a05 62sub disable {
63 my ( $self, @levels ) = @_;
3cdcf968 64 my $level = $self->level;
65 for(map { $LEVELS{$_} } @levels){
66 $level &= ~$_;
67 }
68 $self->level($level);
846772b7 69}
70
a6433a05 71sub _dump {
846772b7 72 my $self = shift;
f63c03e4 73 $self->info( Data::Dump::dump(@_) );
846772b7 74}
75
76sub _log {
77 my $self = shift;
78 my $level = shift;
846772b7 79 my $message = join( "\n", @_ );
1cf0345b 80 $message .= "\n" unless $message =~ /\n$/;
3cdcf968 81 my $body = $self->_body;
82 $body .= sprintf( "[%s] %s", $level, $message );
83 $self->_body($body);
4be535b1 84}
85
86sub _flush {
b5ecfcf0 87 my $self = shift;
3cdcf968 88 if ( $self->abort || !$self->_body ) {
b5ecfcf0 89 $self->abort(undef);
90 }
91 else {
3cdcf968 92 $self->_send_to_log( $self->_body );
4be535b1 93 }
3cdcf968 94 $self->_body(undef);
846772b7 95}
96
adee716c 97sub _send_to_log {
98 my $self = shift;
99 print STDERR @_;
100}
101
6680c772 102no Moose;
103__PACKAGE__->meta->make_immutable();
104
846772b7 1051;
106
107__END__
fc7ec1d9 108
109=head1 NAME
110
111Catalyst::Log - Catalyst Log Class
112
113=head1 SYNOPSIS
114
82d2fcbe 115 $log = $c->log;
846772b7 116 $log->debug($message);
117 $log->info($message);
118 $log->warn($message);
119 $log->error($message);
120 $log->fatal($message);
846772b7 121
f4dbb0d9 122 if ( $log->is_debug ) {
846772b7 123 # expensive debugging
124 }
82d2fcbe 125
a6433a05 126
fc7ec1d9 127See L<Catalyst>.
128
129=head1 DESCRIPTION
130
adee716c 131This module provides the default, simple logging functionality for Catalyst.
132If you want something different set C<< $c->log >> in your application module,
133e.g.:
82d2fcbe 134
135 $c->log( MyLogger->new );
136
137Your logging object is expected to provide the interface described here.
397be447 138Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
139
140If you want to be able to log arbitrary warnings, you can do something along
141the lines of
142
143 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
144
145however this is (a) global, (b) hairy and (c) may have unexpected side effects.
146Don't say we didn't warn you.
82d2fcbe 147
a6433a05 148=head1 LOG LEVELS
149
b5ecfcf0 150=head2 debug
a6433a05 151
152 $log->is_debug;
153 $log->debug($message);
154
b5ecfcf0 155=head2 info
a6433a05 156
157 $log->is_info;
158 $log->info($message);
159
b5ecfcf0 160=head2 warn
a6433a05 161
162 $log->is_warn;
163 $log->warn($message);
164
b5ecfcf0 165=head2 error
a6433a05 166
167 $log->is_error;
168 $log->error($message);
169
b5ecfcf0 170=head2 fatal
a6433a05 171
172 $log->is_fatal;
173 $log->fatal($message);
174
b22c6668 175=head1 METHODS
fc7ec1d9 176
b5ecfcf0 177=head2 new
a6433a05 178
26e73131 179Constructor. Defaults to enable all levels unless levels are provided in
a6433a05 180arguments.
181
182 $log = Catalyst::Log->new;
26e73131 183 $log = Catalyst::Log->new( 'warn', 'error' );
a6433a05 184
3cdcf968 185=head2 level
186
187Contains a bitmask of the currently set log levels.
188
b5ecfcf0 189=head2 levels
a6433a05 190
191Set log levels
b22c6668 192
a6433a05 193 $log->levels( 'warn', 'error', 'fatal' );
fc7ec1d9 194
b5ecfcf0 195=head2 enable
fc7ec1d9 196
a6433a05 197Enable log levels
fc7ec1d9 198
a6433a05 199 $log->enable( 'warn', 'error' );
fc7ec1d9 200
b5ecfcf0 201=head2 disable
fc7ec1d9 202
a6433a05 203Disable log levels
fc7ec1d9 204
a6433a05 205 $log->disable( 'warn', 'error' );
fc7ec1d9 206
b5ecfcf0 207=head2 is_debug
a2f2cde9 208
b5ecfcf0 209=head2 is_error
a2f2cde9 210
b5ecfcf0 211=head2 is_fatal
a2f2cde9 212
b5ecfcf0 213=head2 is_info
a2f2cde9 214
b5ecfcf0 215=head2 is_warn
49ba64d7 216
217Is the log level active?
218
b5ecfcf0 219=head2 abort
4be535b1 220
ac5c933b 221Should Catalyst emit logs for this request? Will be reset at the end of
222each request.
4be535b1 223
224*NOTE* This method is not compatible with other log apis, so if you plan
225to use Log4Perl or another logger, you should call it like this:
226
227 $c->log->abort(1) if $c->log->can('abort');
228
adee716c 229=head2 _send_to_log
230
231 $log->_send_to_log( @messages );
232
233This protected method is what actually sends the log information to STDERR.
234You may subclass this module and override this method to get finer control
235over the log output.
236
3cdcf968 237=head2 meta
238
fc7ec1d9 239=head1 SEE ALSO
240
241L<Catalyst>.
242
243=head1 AUTHOR
244
245Sebastian Riedel, C<sri@cpan.org>
61b1e958 246Marcus Ramberg, C<mramberg@cpan.org>
a6433a05 247Christian Hansen, C<ch@ngmedia.com>
fc7ec1d9 248
249=head1 COPYRIGHT
250
a6433a05 251This program is free software, you can redistribute it and/or modify
61b1e958 252it under the same terms as Perl itself.
fc7ec1d9 253
254=cut
255
e5ecd5bc 256__PACKAGE__->meta->make_immutable;
257
fc7ec1d9 2581;