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