--- DBI.pm.orig 2008-04-16 18:50:13.000000000 -0500
+++ DBI.pm 2008-04-16 19:44:31.000000000 -0500
@@ -2,7 +2,7 @@
use strict;
use vars qw($VERSION);
-$VERSION = 0.02;
+$VERSION = 0.03;
use Log::Dispatch 2.00;
use base qw(Log::Dispatch::Output);
@@ -34,13 +34,19 @@
}
$self->{table} = $params{table} || 'log';
+ $self->{statement_columns} = $params{statement_columns} || 'level, message';
+ $self->{statement_values} = $params{statement_values} || '?, ?';
+ if (exists $params{append_to_execute_args}) {
+ $self->{append_to_execute_args} = $params{append_to_execute_args};
+ }
+ $self->{execute_level_as_number} = $params{execute_level_as_number} : 0;
$self->{sth} = $self->create_statement;
}
sub create_statement {
my $self = shift;
return $self->{dbh}->prepare(<<"SQL");
-INSERT INTO $self->{table} (level, message) VALUES (?, ?)
+INSERT INTO $self->{table} ($self->{statement_columns}) VALUES ($self->{statement_columns})
SQL
;
}
@@ -48,7 +54,26 @@
sub log_message {
my $self = shift;
my %params = @_;
- $self->{sth}->execute(@params{qw(level message)});
+ my @execute = $self->{execute_level_as_number} ? ( $self->_level_as_number($params{level}) , $params{message}) : (@params{qw(level message)});
+
+ my @append;
+ if (exists $self->{append_to_execute_args}) {
+ if ( ref $self->{append_to_execute_args} eq 'CODE' ) {
+ @append = $self->{append_to_execute_args}->();
+ }
+ elsif ( ref $self->{append_to_execute_args} eq 'ARRAY' ) {
+ @append = @{ $self->{append_to_execute_args} };
+ }
+ elsif ( my $cr = $self->{append_to_execute_args}->can('get_log_dispatch_dbi_append_to_execute_args') ) {
+ @append = $cr->( $self->{append_to_execute_args} );
+ }
+ else {
+ @append = ($self->{append_to_execute_args});
+ }
+ push @execute, @append if @append;
+ }
+
+ $self->{sth}->execute( @execute );
}
sub DESTROY {
@@ -122,6 +147,41 @@
Table name for logging. default is B<log>.
+=item -- statement_columns
+
+SQL list of columns to insert into. Default is 'level, message'
+
+
+=item -- statement_values
+
+SQL list of items for VALUES() including placeholders. Default is '?, ?'
+
+=item -- append_to_execute_args
+
+If increasing the number of SQL placeholders with 'statement_values' you will probably also want to pass more to execute() than 'level' and 'message'
+
+You can do that with 'append_to_execute_args'
+
+It can be an array ref of items, a code ref that returns an array of items (empty array results in no addtional values), an object with a method called 'get_log_dispatch_dbi_append_to_execute_args' that returns an array of items(empty array results in no addtional values), or a single item (undef becomes NULL usually)
+
+Default is [] if you do not specify it.
+
+Be carefull as it's mere existance regardless of if the value is undef or '' will get an addition argument to execute!
+
+=item -- execute_level_as_number
+
+If set to true, DBI's execute() will get the level in numeric form instead of textual.
+
+For example instead of 'debug' it will insert '0', 'info' will be 1, etc
+
+This is useful to facilitate the use of simple math in queries like this: 'WHERE level >= 2 AND < 6'
+
+Default is 0 so as not to change the default behavior.
+
+In L<TABLE SCHEMA> below you'd change the 'level' column like so:
+
+ level int(1) NOT NULL,
+
=item log_message
inherited from Log::Dispatch::Output.
@@ -151,6 +211,36 @@
Log::Dispatch::DBI and override C<create_statement> and C<log_message>
method.
+=head1 Advanced SCHEMA Example
+
+Maybe something like this for MySQL.
+
+ CREATE TABLE log (
+ id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ datetime datetime,
+ level int(1) NOT NULL,
+ message text NOT NULL,
+ );
+
+Then in the object:
+
+ $log->add(
+ Log::Dispatch::DBI->new(
+ ...
+ 'statement_columns' => 'id, when, level, message',
+ 'statement_values' => 'NULL, NOW(), ?, ?',
+ 'execute_level_as_number' => 1,
+ )
+ );
+
+ ...
+
+ $log->log(level => 'info', message => 'too bad');
+
+will execute the following SQL:
+
+ INSERT INTO log (id, when, level, message) VALUES(NULL, NOW(), 1, 'too bad');
+
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>