You Log to Your DB, Where Do You Log When Your DB

2019-04-13 01:50发布

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!

2条回答
你好瞎i
2楼-- · 2019-04-13 02:24

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-04-13 02:27

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);
查看更多
登录 后发表回答