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