added optional arguments to Catalyst::Log methods
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Log.pm
1 package Catalyst::Log;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5 use Data::Dumper;
6
7 $Data::Dumper::Terse = 1;
8
9 =head1 NAME
10
11 Catalyst::Log - Catalyst Log Class
12
13 =head1 SYNOPSIS
14
15     $log = $c->log;
16     $log->debug($msg, @args);
17     $log->dump($ref);
18     $log->error($msg, @args);
19     $log->info($msg, @args);
20     $log->warn($msg, @args);
21
22 See L<Catalyst>.
23
24 =head1 DESCRIPTION
25
26 This module provides the default, simple logging functionality for Catalyst.
27 If you want something different set C<$c->log> in your application module, e.g.:
28
29     $c->log( MyLogger->new );
30
31 Your logging object is expected to provide the interface described here.
32
33
34 =head1 METHODS
35
36 =over 4
37
38 =item $log->debug($msg, @args)
39
40 Logs a debugging message.
41
42 =cut
43
44 sub debug { _format( 'debug', splice(@_, 1) ) }
45
46 =item $log->dump($ref)
47
48 Logs a formatted dump of a variable passed by reference (uses C<Data::Dumper>).
49
50 =cut
51
52 sub dump { _format( 'dump', Dumper( $_[1] ) ) }
53
54 =item $log->error($msg, @args)
55
56 Logs an error message.
57
58 =cut
59
60 sub error { _format( 'error', splice(@_, 1) ) }
61
62 =item $log->info($msg, @args)
63
64 Logs an informational message.
65
66 =cut
67
68 sub info { _format( 'info', splice(@_, 1) ) }
69
70 =item $log->warn($msg, @args)
71
72 Logs a warning message.
73
74 =cut
75
76 sub warn { _format( 'warn', splice(@_, 1) ) }
77
78 sub _format {
79     if (@_ > 2) {
80         printf STDERR '[' . localtime(time) . "] [catalyst] [$_[0]] $_[1]\n", splice(@_, 2);
81     }
82     else {
83         print STDERR '[' . localtime(time) . "] [catalyst] [$_[0]] $_[1]\n";
84     }
85 }
86
87 =back
88
89 =head1 SEE ALSO
90
91 L<Catalyst>.
92
93 =head1 AUTHOR
94
95 Sebastian Riedel, C<sri@cpan.org>
96
97 =head1 COPYRIGHT
98
99 This program is free software, you can redistribute it and/or modify it under
100 the same terms as Perl itself.
101
102 =cut
103
104 1;