Log::Handler::Plugin::DBI - A plugin for Log::Handler using Log::Hander::Output::DBI
Firstly, use your config file to connect to the db and create the 'log' table.
Then:
package My::App; use strict; use warnings; use Config::Plugin::Tiny; # For config_tiny(). use File::Spec; use Log::Handler::Plugin::DBI; # For configure_logger() and log(). # ------------------------------------------------ sub marine { my($self) = @_; my($config) = $self -> config_tiny(File::Spec -> catfile('some', 'dir', 'config.tiny.ini') ); $self -> configure_logger($$config{logger}); $self -> log(debug => 'Hi from sub marine()'); } # End of marine. # -------------------------------------------------- 1;
t/config.logger.ini is used in the test file t/test.t, and also ships with the CGI::Snapp::Demo::Four distro.
When you 'use' this module (as in the Synopsis), it automatically imports into your class several methods. See "Methods" for details.
This module is available as a Unix-style distro (*.tgz).
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing distros.
Install Log::Handler::Plugin::DBI as you would for any Perl module:
Perl
Run:
cpanm Log::Handler::Plugin::DBI
or run:
sudo cpan Log::Handler::Plugin::DBI
or unpack the distro, and then either:
perl Build.PL ./Build ./Build test sudo ./Build install
or:
perl Makefile.PL make (or dmake or nmake) make test make install
This module does not have, and does not need, a constructor.
Configures the internal log object with these parameters from $hashref:
A typical $string might be 'dbi:SQLite:dbname=/tmp/logger.test.sqlite'.
Supply your database server username, or leave empty for databases such as SQLite.
Supply your database server password, or leave empty for databases such as SQLite.
Supply your log table name, or let it default to 'log'.
Logs $message at $level.
See Log::Handler#LOG-LEVELS and Log::Handler::Levels.
Returns the internal log object, for those cases when you want to pass it to some other class.
See the CGI::Snapp::Demo::Four distro, which contains httpd/cgi-bin/cgi.snapp.demo.four.cgi, which uses CGI::Snapp::Demo::Four::Wrapper.
The latter passes this log object to CGI::Snapp's logger() method, to demonstrate logging a CGI script's method call sequence to a database table.
In your sub-class of CGI::Snapp for example, or anywhere else you want to log to a database.
See Log::Handler::Plugin::DBI::CreateTable for help creating a 'log' table.
For sample code, study CGI::Snapp::Demo::Four.
See the "FAQ" in Log::Handler::Plugin::DBI::CreateTable. In pseudo-code:
id primary key + (db_vendor-dependent stuff) level varchar(255) not null, message not null + (db_vendor eq 'ORACLE' ? 'long' : 'text') timestamp timestamp not null default current_timestamp + (db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '')
Also, if you're using MySQL, you might want to set the engine=innodb option.
See scripts/create.table.pl and scripts/drop.table.pl for an easy way to do all this.
Yes. See t/test.t, which passes undef as the first parameter to each method, because there is no $self available.
Alternately, if you 'use' this module within any other module, calling $self -> log() will work.
It's used in CGI::Snapp::Demo::Four::Wrapper, which inherits from CGI::Snapp::Demo::Four, which inherits from CGI::Snapp. So, even though CGI::Snapp has its own log() method, the one imported from the current module overrides that one.
Because it's not true that an object of type Log::Handler::Output::DBI (the underlying object here) 'isa' Log::Handler.
It is not needed; it would be for documentation only.
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.
CGI::Application
The following are all part of this set of distros:
CGI::Snapp - A almost back-compat fork of CGI::Application
CGI::Snapp::Demo::One - A template-free demo of CGI::Snapp using just 1 run mode
CGI::Snapp::Demo::Two - A template-free demo of CGI::Snapp using N run modes
CGI::Snapp::Demo::Three - A template-free demo of CGI::Snapp using the forward() method
CGI::Snapp::Demo::Four - A template-free demo of CGI::Snapp using Log::Handler::Plugin::DBI
CGI::Snapp::Demo::Four::Wrapper - A wrapper around CGI::Snapp::Demo::Four, to simplify using Log::Handler::Plugin::DBI
Config::Plugin::Tiny - A plugin which uses Config::Tiny
Config::Plugin::TinyManifold - A plugin which uses Config::Tiny with 1 of N sections
Data::Session - Persistent session data management
Log::Handler::Plugin::DBI::CreateTable - A helper for Log::Hander::Output::DBI to create your 'log' table
The file CHANGES was converted into Changelog.ini by Module::Metadata::Changes.
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
Please read https://metacpan.org/module/CGI::Application::Plugin::Config::Simple#AUTHOR, since a lot of the ideas for this module were copied from CGI::Application::Plugin::Config::Simple.
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=Log::Handler::Plugin::DBI.
Log::Handler::Plugin::DBI was written by Ron Savage <ron@savage.net.au> in 2012.
Home page: http://savage.net.au/index.html.
Australian copyright (c) 2012, Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Artistic License, a copy of which is available at: http://www.opensource.org/licenses/index.html
To install Log::Handler::Plugin::DBI, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install Log::Handler::Plugin::DBI
For more information on module installation, please visit the detailed CPAN module installation guide.