File Coverage

blib/lib/Log/Handler/Plugin/DBI.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition 2 4 50.0
subroutine 7 7 100.0
pod 3 3 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Log::Handler::Plugin::DBI;
2              
3 2     2   22838 use strict;
  2         4  
  2         75  
4 2     2   9 use warnings;
  2         2  
  2         53  
5              
6 2     2   1848 use Log::Handler::Output::DBI;
  2         70937  
  2         73  
7              
8 2     2   19 use vars qw(@EXPORT @ISA);
  2         3  
  2         608  
9              
10             @EXPORT = (qw/configure_logger log log_object/);
11             @ISA = ('Exporter');
12              
13             my($object);
14              
15             our $VERSION = '1.02';
16              
17             # -----------------------------------------------
18              
19             sub configure_logger
20             {
21 1     1 1 305 my($self, $config) = @_;
22 1   50     22 $object = Log::Handler::Output::DBI -> new
23             (
24             columns => [qw/level message/],
25             data_source => $$config{dsn},
26             password => $$config{password},
27             persistent => 1,
28             table => $$config{table_name} || 'log',
29             user => $$config{username},
30             values => [qw/%level %message/],
31             );
32              
33             } # End of configure_logger.
34              
35             # -----------------------------------------------
36              
37             sub log
38             {
39 1     1 1 3 my($self, $level, $s) = @_;
40              
41 1   50     9 $object -> log(level => $level, message => $s || '');
42              
43             } # End of log.
44              
45             # -----------------------------------------------
46              
47             sub log_object
48             {
49 1     1 1 219 my($self) = @_;
50              
51 1         8 return $object;
52              
53             } # End of log_object.
54              
55             # --------------------------------------------------
56              
57             1;
58              
59             =head1 NAME
60              
61             Log::Handler::Plugin::DBI - A plugin for Log::Handler using Log::Hander::Output::DBI
62              
63             =head1 Synopsis
64              
65             Firstly, use your config file to connect to the db and create the 'log' table.
66              
67             Then:
68              
69             package My::App;
70              
71             use strict;
72             use warnings;
73              
74             use Config::Plugin::Tiny; # For config_tiny().
75              
76             use File::Spec;
77              
78             use Log::Handler::Plugin::DBI; # For configure_logger() and log().
79              
80             # ------------------------------------------------
81              
82             sub marine
83             {
84             my($self) = @_;
85             my($config) = $self -> config_tiny(File::Spec -> catfile('some', 'dir', 'config.tiny.ini') );
86              
87             $self -> configure_logger($$config{logger});
88              
89             $self -> log(debug => 'Hi from sub marine()');
90              
91             } # End of marine.
92              
93             # --------------------------------------------------
94              
95             1;
96              
97             t/config.logger.ini is used in the test file t/test.t, and also ships with the L distro.
98              
99             =head1 Description
100              
101             When you 'use' this module (as in the Synopsis), it automatically imports into your class several methods. See L for details.
102              
103             =head1 Distributions
104              
105             This module is available as a Unix-style distro (*.tgz).
106              
107             See L
108             for help on unpacking and installing distros.
109              
110             =head1 Installation
111              
112             Install L as you would for any C module:
113              
114             Run:
115              
116             cpanm Log::Handler::Plugin::DBI
117              
118             or run:
119              
120             sudo cpan Log::Handler::Plugin::DBI
121              
122             or unpack the distro, and then either:
123              
124             perl Build.PL
125             ./Build
126             ./Build test
127             sudo ./Build install
128              
129             or:
130              
131             perl Makefile.PL
132             make (or dmake or nmake)
133             make test
134             make install
135              
136             =head1 Constructor and Initialization
137              
138             This module does not have, and does not need, a constructor.
139              
140             =head1 Methods
141              
142             =head2 configure_logger($hashref)
143              
144             Configures the internal log object with these parameters from $hashref:
145              
146             =over 4
147              
148             =item o dsn => $string
149              
150             A typical $string might be 'dbi:SQLite:dbname=/tmp/logger.test.sqlite'.
151              
152             =item o username => $string
153              
154             Supply your database server username, or leave empty for databases such as SQLite.
155              
156             =item o password => $string
157              
158             Supply your database server password, or leave empty for databases such as SQLite.
159              
160             =item o table_name => $string
161              
162             Supply your log table name, or let it default to 'log'.
163              
164             =back
165              
166             =head2 log($level => $message)
167              
168             Logs $message at $level.
169              
170             See L and L.
171              
172             =head2 log_object()
173              
174             Returns the internal log object, for those cases when you want to pass it to some other class.
175              
176             See the L distro, which contains httpd/cgi-bin/cgi.snapp.demo.four.cgi, which uses L.
177              
178             The latter passes this log object to L's logger() method, to demonstrate logging a L script's method call sequence to a database table.
179              
180             =head1 FAQ
181              
182             =head2 When would I use this module?
183              
184             In your sub-class of L for example, or anywhere else you want to log to a database.
185              
186             See L for help creating a 'log' table.
187              
188             For sample code, study L.
189              
190             =head2 What is the expected structure of the 'log' table?
191              
192             See the L. In pseudo-code:
193              
194             id primary key + (db_vendor-dependent stuff)
195             level varchar(255) not null,
196             message not null + (db_vendor eq 'ORACLE' ? 'long' : 'text')
197             timestamp timestamp not null default current_timestamp +
198             (db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '')
199              
200             Also, if you're using MySQL, you might want to set the engine=innodb option.
201              
202             See scripts/create.table.pl and scripts/drop.table.pl for an easy way to do all this.
203              
204             =head2 Can this module be used in any module?
205              
206             Yes. See t/test.t, which passes undef as the first parameter to each method, because there is no $self available.
207              
208             Alternately, if you 'use' this module within any other module, calling $self -> log() will work.
209              
210             It's used in L, which inherits from L, which inherits from L. So, even though L has its own
211             log() method, the one imported from the current module overrides that one.
212              
213             =head2 Why can't I call $log_object -> info($message) etc as with Log::Handler?
214              
215             Because it's I true that an object of type L (the underlying object here) 'isa' L.
216              
217             =head2 Why don't you 'use Exporter;'?
218              
219             It is not needed; it would be for documentation only.
220              
221             For the record, Exporter V 5.567 ships with Perl 5.8.0. That's what I had in Build.PL and Makefile.PL until I tested the fact I can omit it.
222              
223             =head1 See Also
224              
225             L
226              
227             The following are all part of this set of distros:
228              
229             L - A almost back-compat fork of CGI::Application
230              
231             =head1 See Also
232              
233             L
234              
235             The following are all part of this set of distros:
236              
237             L - A almost back-compat fork of CGI::Application
238              
239             L - A template-free demo of CGI::Snapp using just 1 run mode
240              
241             L - A template-free demo of CGI::Snapp using N run modes
242              
243             L - A template-free demo of CGI::Snapp using the forward() method
244              
245             L - A template-free demo of CGI::Snapp using Log::Handler::Plugin::DBI
246              
247             L - A wrapper around CGI::Snapp::Demo::Four, to simplify using Log::Handler::Plugin::DBI
248              
249             L - A plugin which uses Config::Tiny
250              
251             L - A plugin which uses Config::Tiny with 1 of N sections
252              
253             L - Persistent session data management
254              
255             L - A plugin for Log::Handler using Log::Hander::Output::DBI
256              
257             L - A helper for Log::Hander::Output::DBI to create your 'log' table
258              
259             =head1 Machine-Readable Change Log
260              
261             The file CHANGES was converted into Changelog.ini by L.
262              
263             =head1 Version Numbers
264              
265             Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
266              
267             =head1 Credits
268              
269             Please read L, since a lot of the ideas for this module were copied from
270             L.
271              
272             =head1 Support
273              
274             Email the author, or log a bug on RT:
275              
276             L.
277              
278             =head1 Author
279              
280             L was written by Ron Savage Iron@savage.net.auE> in 2012.
281              
282             Home page: L.
283              
284             =head1 Copyright
285              
286             Australian copyright (c) 2012, Ron Savage.
287              
288             All Programs of mine are 'OSI Certified Open Source Software';
289             you can redistribute them and/or modify them under the terms of
290             The Artistic License, a copy of which is available at:
291             http://www.opensource.org/licenses/index.html
292              
293             =cut