Re: Sqlite wrapper for Log package
| From: | Bertrand Mansion | Date: | Tue, 06 Jan 2004 12:09:23 +0000 |
| Subject: | Re: Sqlite wrapper for Log package | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-24809@lists.php.net to get a copy of this message | ||
<jon@php.net> wrote :
> On Sun, Jan 04, 2004 at 07:35:38PM +0100, Bertrand Mansion wrote:
>
>>>> I have made a concrete Log class for the Log package that logs to an sqlite
>>>> database.
>>
>> I've updated the source so that when a table that doesn't exist is used in
>> the log() parameters, it is automatically created by the log class.
>
> I've added a slightly modified version of your code to CVS. I don't
> have Sqlite running right now so please test it for me.
>
> Also, I won't release a new version of the Log package until this new
> handler has proper examples, tests and documentation. If you could
> provide some or all of them, I'd appreciate it (and it would speed the
> next release along). Otherwise, it will have to wait until I find the
> time to write them myself.
>
> Thanks again, Bertrand!
Something like this could do maybe
(I don't have unit test installed so I can't test...)
--TEST--
Log: Sqlite
--FILE--
<?php
require_once 'Log.php';
$db = sqlite_open('log.db', 0666, $error);
$ident = time();
$message = 'logging an event';
$logger =& Log::factory('sqlite', 'log_table', $ident, $db);
$logger->log($message, PEAR_LOG_WARNING);
$q = "SELECT message FROM log_table WHERE ident='$ident'";
$res = sqlite_query($db, $q);
if (sqlite_fetch_single($res) == $message) {
echo 'ok';
}
sqlite_close($db);
--EXPECT--
ok
And for the docs:
The Sqlite Handler
------------------
The Sqlite handler sends log events to an sqlite database using `PHP native
sqlite functions`_.
It is faster than the SQL handler because requests are made directly to
the database without using an abstraction layer. It is also interesting to
note that sqlite database files can be moved, copied, deleted on your system
just like any other files, which makes log management easier. Last but not
least, using a database to log your events allows you to make sql queries to
create reports, statistics, etc.
When using a database and logging a lot of events, it is recommended
to split the database into smaller databases. This is allowed by Sqlite and
you can later use Sqlite
ATTACH_ statement to query your log database
files globally.
If the database does not exist when the log is opened, sqlite will try to
create it automatically. If the log table does not exist, it will also be
automatically created. The table creation uses the following SQL request::
CREATE TABLE log_table (
id INTEGER PRIMARY KEY NOT NULL,
logtime NOT NULL,
ident CHAR(16) NOT NULL,
priority INT NOT NULL,
message
);
Configuration
~~~~~~~~~~~~~
+-------------------+-----------+---------------+--------------------------+
| Parameter | Type | Default | Description |
+===================+===========+===============+==========================+
| `filename` | String | | Path to an sqlite database. |
+-------------------+-----------+---------------+--------------------------+
| `mode` | Integer | 0666 | Mode used to open the database. |
+-------------------+-----------+---------------+--------------------------+
| `persistent` | Boolean | false | Use a persistent connection. |
+-------------------+-----------+---------------+--------------------------+
An already opened database connection can also be passed as parameter
instead of the above configuration. In this case, closing the database
connection is up to the user.
.. _PHP native sqlite functions: http://www.php.net/sqlite
.. _ATTACH: http://www.sqlite.org/lang.html#attach
Examples
~~~~~~~~
Using a configuration to create a new database connection::
$conf = array('filename' => 'log.db', 'mode' => 0666,
'persistent' => true);
$logger =& Log::factory('sqlite', 'log_table', 'ident', $conf);
$logger->log('logging an event', PEAR_LOG_WARNING);
Using an existing connection::
$db = sqlite_open('log.db', 0666, $error);
$logger =& Log::factory('sqlite', 'log_table', 'ident', $db);
$logger->log('logging an event', PEAR_LOG_WARNING);
sqlite_close($db);
I don't have reST, phpdocumentor or docbook installed neither...
Bertrand