line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Handler::Plugin::DBI; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
74819
|
use strict; |
|
3
|
|
|
|
|
17
|
|
|
3
|
|
|
|
|
86
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
96
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use vars qw(@EXPORT @ISA); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
262
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
@EXPORT = qw/configure_logger log_object log/; |
9
|
|
|
|
|
|
|
@ISA = ('Exporter'); |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
1725
|
use Log::Handler::Output::DBI; |
|
3
|
|
|
|
|
98476
|
|
|
3
|
|
|
|
|
609
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my($object); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '1.04'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# ----------------------------------------------- |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub configure_logger |
20
|
|
|
|
|
|
|
{ |
21
|
1
|
|
|
1
|
1
|
399
|
my($self, $config) = @_; |
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
|
1
|
|
50
|
|
|
22
|
values => [qw/%level %message/], |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} # End of configure_logger. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# ----------------------------------------------- |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub log |
38
|
|
|
|
|
|
|
{ |
39
|
1
|
|
|
1
|
1
|
4
|
my($self, $level, $s) = @_; |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
50
|
|
|
10
|
$object -> log(level => $level, message => $s || ''); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} # End of log. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# ----------------------------------------------- |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub log_object |
48
|
|
|
|
|
|
|
{ |
49
|
1
|
|
|
1
|
1
|
187
|
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 |
98
|
|
|
|
|
|
|
L distro. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 Description |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
When you 'use' this module (as in the Synopsis), it automatically imports into your class |
103
|
|
|
|
|
|
|
several methods. See L for details. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 Distributions |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This module is available as a Unix-style distro (*.tgz). |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
See L |
110
|
|
|
|
|
|
|
for help on unpacking and installing distros. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 Installation |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Install L as you would for any C module: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Run: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
cpanm Log::Handler::Plugin::DBI |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
or run: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sudo cpan Log::Handler::Plugin::DBI |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
or unpack the distro, and then either: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
perl Build.PL |
127
|
|
|
|
|
|
|
./Build |
128
|
|
|
|
|
|
|
./Build test |
129
|
|
|
|
|
|
|
sudo ./Build install |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
or: |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
perl Makefile.PL |
134
|
|
|
|
|
|
|
make (or dmake or nmake) |
135
|
|
|
|
|
|
|
make test |
136
|
|
|
|
|
|
|
make install |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 Constructor and Initialization |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This module does not have, and does not need, a constructor. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 Methods |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 configure_logger($hashref) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Configures the internal log object with these parameters from $hashref: |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over 4 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item o dsn => $string |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
A typical $string might be 'dbi:SQLite:dbname=/tmp/logger.test.sqlite'. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item o username => $string |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Supply your database server username, or leave empty for databases such as SQLite. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item o password => $string |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Supply your database server password, or leave empty for databases such as SQLite. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item o table_name => $string |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Supply your log table name, or let it default to 'log'. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 log($level => $message) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Logs $message at $level. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
See L and L. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 log_object() |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Returns the internal log object, for those cases when you want to pass it to some other class. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
See the L distro, which contains httpd/cgi-bin/cgi.snapp.demo.four.cgi, |
179
|
|
|
|
|
|
|
which uses L. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
The latter passes this log object to L's logger() method, to demonstrate logging a |
182
|
|
|
|
|
|
|
L script's method call sequence to a database table. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 FAQ |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 When would I use this module? |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
In your sub-class of L for example, or anywhere else you want to log to a database. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
See L for help creating a 'log' table. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
For sample code, study L. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 What is the expected structure of the 'log' table? |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
See the L. In pseudo-code: |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
id primary key + (db_vendor-dependent stuff) |
199
|
|
|
|
|
|
|
level varchar(255) not null, |
200
|
|
|
|
|
|
|
message not null + (db_vendor eq 'ORACLE' ? 'long' : 'text') |
201
|
|
|
|
|
|
|
timestamp timestamp not null default current_timestamp + |
202
|
|
|
|
|
|
|
(db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '') |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Also, if you are using MySQL, you might want to set the engine=innodb option. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
See scripts/create.table.pl and scripts/drop.table.pl for an easy way to do all this. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head2 Can this module be used in any module? |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Yes. See t/test.t, which passes undef as the first parameter to each method, because there is no |
211
|
|
|
|
|
|
|
$self available. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Alternately, if you 'use' this module within any other module, calling $self -> log() will |
214
|
|
|
|
|
|
|
work. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
It is used in L, which inherits from L, |
217
|
|
|
|
|
|
|
which inherits from L. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 Why can I not call $log_object -> info($message) etc as with Log::Handler? |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Because it is I true that an object of type L (the underlying |
222
|
|
|
|
|
|
|
object here) 'isa' L. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 Machine-Readable Change Log |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
The file CHANGES was converted into Changelog.ini by L. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 Version Numbers |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 Repository |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
L |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 Support |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Email the author, or log a bug on RT: |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
L. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 Author |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
L was written by Ron Savage Iron@savage.net.auE> in 2012. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Home page: L. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 Copyright |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Australian copyright (c) 2012, Ron Savage. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
All Programs of mine are 'OSI Certified Open Source Software'; |
253
|
|
|
|
|
|
|
you can redistribute them and/or modify them under the terms of |
254
|
|
|
|
|
|
|
The Artistic License, a copy of which is available at: |
255
|
|
|
|
|
|
|
http://www.opensource.org/licenses/index.html |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=cut |