views:

291

answers:

2

I'm working with logging in PHP with Pear, and I get into a standard problem: can I use file-based logging when the DB is not available? I don't mind if it's slow due to concurrency issues, but it cannot fail to work due to multiple, simultaneous hits.

I'm asking this question in general (for other web technologies) and specifically for Pear for PHP.

Thanks!

A: 

You can pass an existing database object into the singleton method of Log - if you don't have a database you can revert to alternative logging methods (or good old user_error())

require_once 'DB.php';
$db = &DB::connect('pgsql://jon@localhost+unix/logs');

$conf['db'] = $db;
$logger = &Log::singleton('sql', 'log_table', 'ident', $conf);
Ken
+4  A: 

Generally, logging to the file system is a good fall back if you can't connect to your database. Simultaneous hits shouldn't be a problem (locks...). If you already have your logs adapted for a database perhaps the easiest way to go would be to use sqlite as a fall back.

Another way would be to email log events in this case, in addition to not loosing them this approach should make you aware of your database problem faster.

Toni Ruža
nice! I like the SqlLite idea AND the email idea.
Yar
I'm not much of a strong OO advocate but this seems like a great case for hiding logging behind an interface and having the interface switch out it's logging provider (email/sqllite/db/file)
Graphain