1Log::Handler::Output::DUBsIe(r3)Contributed Perl DocumenLtoagt:i:oHnandler::Output::DBI(3)
2
3
4

NAME

6       Log::Handler::Output::DBI - Log messages to a database.
7

SYNOPSIS

9           use Log::Handler::Output::DBI;
10
11           my $db = Log::Handler::Output::DBI->new(
12               # database source
13               database    => "database",
14               driver      => "mysql",
15               host        => "127.0.0.1",
16               port        => 3306,
17
18               # or with "dbname" instead of "database"
19               dbname      => "database",
20               driver      => "Pg",
21               host        => "127.0.0.1",
22               port        => 5432,
23
24               # or with data_source
25               data_source => "dbi:mysql:database=database;host=127.0.0.1;port=3306",
26
27               # Username and password
28               user        => "user",
29               password    => "password",
30
31               # debugging
32               debug       => 1,
33
34               # table, columns and values (as string)
35               table       => "messages",
36               columns     => "level ctime cdate pid hostname progname message",
37               values      => "%level %time %date %pid %hostname %progname %message",
38
39               # table, columns and values (as array reference)
40               table       => "messages",
41               columns     => [ qw/level ctime cdate pid hostname progname message/ ],
42               values      => [ qw/%level %time %date %pid %hostname %progname %message/ ],
43
44               # table, columns and values (your own statement)
45               statement   => "insert into messages (level,ctime,cdate,pid,hostname,progname,message) values (?,?,?,?,?,?,?)",
46               values      => [ qw/%level %time %date %pid %hostname %progname %message/ ],
47
48               # if you like persistent connections and want to re-connect
49               persistent  => 1,
50           );
51
52           my %message = (
53               level       => "ERROR",
54               time        => "10:12:13",
55               date        => "1999-12-12",
56               pid         => $$,
57               hostname    => "localhost",
58               progname    => $0,
59               message     => "an error here"
60           );
61
62           $db->log(\%message);
63

DESCRIPTION

65       With this output you can insert messages into a database table.
66

METHODS

68   new()
69       Call "new()" to create a new Log::Handler::Output::DBI object.
70
71       The following options are possible:
72
73       data_source
74           Set the dsn (data source name).
75
76           You can use this parameter instead of "database", "driver", "host"
77           and "port".
78
79       database or dbname
80           Pass the database name.
81
82       driver
83           Pass the database driver.
84
85       host
86           Pass the hostname where the database is running.
87
88       port
89           Pass the port where the database is listened.
90
91       user
92           Pass the database user for the connect.
93
94       password
95           Pass the users password.
96
97       table and columns
98           With this options you can pass the table name for the insert and
99           the columns.  You can pass the columns as string or as array.
100           Example:
101
102               # the table name
103               table => "messages",
104
105               # columns as string
106               columns => "level, ctime, cdate, pid, hostname, progname, message",
107
108               # columns as array
109               columns => [ qw/level ctime cdate pid hostname progname message/ ],
110
111           The statement would created as follows
112
113               insert into message (level, ctime, cdate, pid, hostname, progname, mtime, message)
114                            values (?,?,?,?,?,?,?)
115
116       statement
117           With this option you can pass your own statement if you don't want
118           to you the options "table" and "columns".
119
120               statement => "insert into message (level, ctime, cdate, pid, hostname, progname, mtime, message)"
121                            ." values (?,?,?,?,?,?,?)"
122
123       values
124           With this option you have to set the values for the insert.
125
126                   values => "%level, %time, %date, %pid, %hostname, %progname, %message",
127
128                   # or
129
130                   values => [ qw/%level %time %date %pid %hostname %progname %message/ ],
131
132           The placeholders are identical with the pattern names that you have
133           to pass with the option "message_pattern" from Log::Handler.
134
135               %L   level
136               %T   time
137               %D   date
138               %P   pid
139               %H   hostname
140               %N   newline
141               %C   caller
142               %p   package
143               %f   filename
144               %l   line
145               %s   subroutine
146               %S   progname
147               %r   runtime
148               %t   mtime
149               %m   message
150
151           Take a look to the documentation of Log::Handler for all possible
152           patterns.
153
154       persistent
155           With this option you can enable or disable a persistent database
156           connection and re-connect if the connection was lost.
157
158           This option is set to 1 on default.
159
160       dbi_params
161           This option is useful if you want to pass arguments to DBI. The
162           default is set to
163
164               {
165                   PrintError => 0,
166                   AutoCommit => 1
167               }
168
169           "PrintError" is deactivated because this would print error messages
170           as warnings to STDERR.
171
172           You can pass your own arguments - and overwrite it - with
173
174               dbi_params => { PrintError => 1, AutoCommit => 0 }
175
176       debug
177           With this option it's possible to enable debugging. The information
178           can be intercepted with $SIG{__WARN__}.
179
180   log()
181       Log a message to the database.
182
183           my $db = Log::Handler::Output::DBI->new(
184               database   => "database",
185               driver     => "mysql",
186               user       => "user",
187               password   => "password",
188               host       => "127.0.0.1",
189               port       => 3306,
190               table      => "messages",
191               columns    => [ qw/level ctime message/ ],
192               values     => [ qw/%level %time %message/ ],
193               persistent => 1,
194           );
195
196           $db->log(
197               message => "your message",
198               level   => "INFO",
199               time    => "2008-10-10 10:12:23",
200           );
201
202       Or you can connect to the database yourself. You should notice that if
203       the database connection lost then the logger can't re-connect to the
204       database and would return an error. Use "dbi_handle" at your own risk.
205
206           my $dbh = DBI->connect(...);
207
208           my $db = Log::Handler::Output::DBI->new(
209               dbi_handle => $dbh,
210               table      => "messages",
211               columns    => [ qw/level ctime message/ ],
212               values     => [ qw/%level %time %message/ ],
213           );
214
215   connect()
216       Connect to the database.
217
218   disconnect()
219       Disconnect from the database.
220
221   validate()
222       Validate a configuration.
223
224   reload()
225       Reload with a new configuration.
226
227   errstr()
228       This function returns the last error message.
229

PREREQUISITES

231           Carp
232           Params::Validate
233           DBI
234           your DBI driver you want to use
235

EXPORTS

237       No exports.
238

REPORT BUGS

240       Please report all bugs to <jschulz.cpan(at)bloonix.de>.
241
242       If you send me a mail then add Log::Handler into the subject.
243

AUTHOR

245       Jonny Schulz <jschulz.cpan(at)bloonix.de>.
246
248       Copyright (C) 2007-2009 by Jonny Schulz. All rights reserved.
249
250       This program is free software; you can redistribute it and/or modify it
251       under the same terms as Perl itself.
252
253
254
255perl v5.28.1                      2013-11-03      Log::Handler::Output::DBI(3)
Impressum