add different kinds of logging
[p5sagit/Log-Contextual.git] / lib / Log / Contextual.pm
CommitLineData
0daa11f3 1package Log::Contextual;
2
2033c911 3use 5.006;
4
5$VERSION = '1.000';
6
7require Exporter;
8
9BEGIN { @ISA = qw(Exporter) }
10
11@EXPORT = qw(set_logger log_debug with_logger);
12
7cec609c 13our $Get_Logger;
14
15sub set_logger {
16 $Get_Logger = $_[0];
17}
18
6dc6632a 19sub log_trace (&) {
20 my $log = $Get_Logger->();
21 $log->trace($_[0]->())
22 if $log->is_trace;
23}
24
7cec609c 25sub log_debug (&) {
26 my $log = $Get_Logger->();
27 $log->debug($_[0]->())
28 if $log->is_debug;
29}
30
6dc6632a 31sub log_info (&) {
32 my $log = $Get_Logger->();
33 $log->info($_[0]->())
34 if $log->is_info;
35}
36
37sub log_warn (&) {
38 my $log = $Get_Logger->();
39 $log->warn($_[0]->())
40 if $log->is_warn;
41}
42
43sub log_error (&) {
44 my $log = $Get_Logger->();
45 $log->error($_[0]->())
46 if $log->is_error;
47}
48
49sub log_fatal (&) {
50 my $log = $Get_Logger->();
51 $log->fatal($_[0]->())
52 if $log->is_fatal;
53}
54
9a8549d9 55sub with_logger (&$) {
56 local $Get_Logger = $_[1];
57 $_[0]->();
7cec609c 58}
59
0daa11f3 601;
0a3750e2 61
62__END__
63
64.:12:44:33:. <@mst> we have a $Get_Logger global that contains a subref
65.:12:45:11:. <@mst> sub log_debug (&) { my $log = $Get_Logger->(); if ($log->is_debug) {
66 $log->debug($_[0]->()} } }
67.:13:01:22:. >>@mst<< frew: the other part is we'll need a set_logger function that's global
68.:13:01:26:. >>@mst<< frew: plus a with_logger function
69.:13:01:33:. >>@mst<< frew: that uses local()
70.:13:01:38:. <@mst> that should be enough to make a start
71.:13:01:48:. <@frew> so with_logger is what gives us context?
72.:13:01:57:. <@mst> right
73.:13:02:09:. <@mst> with_logger { $logger }, sub { <run code> };
74.:13:02:29:. <@mst> sub with_logger { local $Get_Logger = $_[0]; $_[1]->() }
75.:13:03:05:. <@mst> amazing how simple this stuff is once you get the paradigm
76.:13:03:13:. <@mst> also consider
77.:13:04:17:. <@mst> package Catalyst::Plugin::LogContextual; use Moose::Role; around
78 handle_request => sub { my ($orig, $self) = (shift, shift); my @args = @_;
79 with_logger { $self->log } sub { $self->$orig(@args) } };
80.:13:03:43:. <@frew> so why is $G_L a subref instead of just a ref? to allow for lazy
81 instantiation or what?
82.:13:06:37:. <@mst> it does the caller introspection there IIRC
83.:13:09:56:. <@mst> I've spent like a year thinking about how to do logging sanely
84.:13:10:17:. <@mst> having it turn out to be this bloody trivial to implement amuses me
85.:13:10:43:. <@frew> mst: I guess that's why thinking about it for a year is worth it :-)
86.:13:12:01:. <@mst> there's a couple other things we'll want, I suspect
87.:13:12:18:. <@mst> like a concept of depth and a category system separate from the logger
88.:13:12:24:. <@mst> but those can be handled later atop this API
89.:13:13:35:. <@frew> so like, logging from the model, logging from the controller, logging from
90 the DBIDS part of the model vs the DBIC part of the model ?
91.:13:14:13:. <@mst> that sort of thing
92.:13:14:20:. <@mst> how much of that we can delegate to the logger I dunno yet
93