NAME

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

SYNOPSIS

use Log::Handler::Output::DBI;

my $log = Log::Handler::Output::DBI->new(
    # database connection
    database   => 'database',
    driver     => 'mysql',
    user       => 'user',
    password   => 'password',
    host       => '127.0.0.1',
    port       => 3306,

    # debugging
    debug      => 1,

    # table, columns and values (as string)
    table      => 'messages',
    columns    => 'level, ctime, cdate, pid, hostname, caller, progname, mtime, message',
    values     => '%level, %time, %date, %pid, %hostname, %caller, %progname, %mtime, %message',

    # table, columns and values (as array reference)
    table      => 'messages',
    columns    => [ qw/level ctime cdate pid hostname caller progname mtime message/ ],
    values     => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],

    # table, columns and values (your own statement)
    statement  => 'insert into messages (level,ctime,cdate,pid,hostname,caller,progname,mtime,message) values (?,?,?,?,?,?,?,?,?)',
    values     => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],

    # if you like persistent connections and want to re-connect
    persistent => 1,
    reconnect  => 1,
);

$db_output->log(\%message);

DESCRIPTION

With this output you can insert messages to a database table.

METHODS

new()

Call new() to create a new Log::Handler::Output::DBI object.

The following options are possible:

database

Pass the database name.

driver

Pass the database driver.

user

Pass the database user for the connect.

password

Pass the users password.

host

Pass the hostname where the database is running.

port

Pass the port where the database is listened.

table and columns

With this options you can pass the table name for the insert and the columns. You can pass the columns as string or as array. Example:

# the table name
table => 'messages',

# columns as string
columns => 'level, ctime, cdate, pid, hostname, caller, progname, mtime, message',

# columns as array
columns => [ qw/level ctime cdate pid hostname caller progname mtime message/ ],

The statement would created as follows

insert into message (level, ctime, cdate, pid, hostname, caller, progname, mtime, message)
             values (?,?,?,?,?,?,?,?,?)
statement

With this option you can pass your own statement if you don't want to you the options table and columns.

statement => 'insert into message (level, ctime, cdate, pid, hostname, caller, progname, mtime, message)'
             .' values (?,?,?,?,?,?,?,?,?)'
values

With this option you have to set the values for the insert.

values => '%level, %time, %date, %pid, %hostname, %caller, %progname, %mtime, %message',

# or

values => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],

The placeholders are identical with the pattern names that you have to pass with the option message_pattern.

%L   level
%T   time
%D   date
%P   pid
%H   hostname
%N   newline
%C   caller
%p   progname
%t   mtime
%m   message

Take a look to the documentation of Log::Handler for all possible patterns.

persistent and reconnect

With this option you can enable or disable a persistent database connection and re-connect if the connection was lost.

If you use persistent connections then a ping is send to the database before each insert. If the ping fails then a re-connect is executed.

Both options are set to 1 on default.

dbi_params

This option is useful if you want to pass arguments to DBI. The default is set to

{
    PrintError => 0,
    AutoCommit => 1
}

PrintError is deactivated because this would print error messages as warnings to STDERR.

You can pass your own arguments - and overwrite it - with

dbi_params => { PrintError => 1, AutoCommit => 0 }
debug

With this option it's possible to enable debugging. The informations can be intercepted with $SIG{__WARN__}.

log()

errstr()

This function returns the last error message.

PREREQUISITES

Carp
Params::Validate
DBI
your DBI driver you want to use

EXPORTS

No exports.

REPORT BUGS

Please report all bugs to <jschulz.cpan(at)bloonix.de>.

AUTHOR

Jonny Schulz <jschulz.cpan(at)bloonix.de>.

QUESTIONS

Do you have any questions or ideas?

MAIL: <jschulz.cpan(at)bloonix.de>

If you send me a mail then add Log::Handler into the subject.

COPYRIGHT

Copyright (C) 2007 by Jonny Schulz. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.