Zend Framework -> Zend_Log via application.ini => insert() on a non-object error
I am trying to get logging via the application.ini file going and I am getting stuck with an error.
resources.log.db.writerName = "Db"
resources.log.db.writerParams.db.adapter = "PDO_SQLITE"
resources.log.db.writerParams.db.dbname = ROOT "/data/tmp.db3"
resources.log.d开发者_开发问答b.writerParams.db.table = "logs"
resources.log.db.writerParams.db.columnMap.priority = "priority"
resources.log.db.writerParams.db.columnMap.message = "message"
Fatal error: Call to a member function insert() on a non-object in /var/www/libs/zend/library/Zend/Log/Writer/Db.php on line 137
I submitted a fix here: http://framework.zend.com/issues/browse/ZF-9497
You cannot instantiate a Database Logger from an Ini configuration yet.
Either setup the Logger in your Bootstrap within an _initLog
method or extend the regular Zend_Log class to use the default DB adapter or better, to instantiate, the DB adapter given in the application.ini.
See here:
- Zend_Log in application.ini
And in case you are interested why it doesn't work from application.ini:
The Log Resource Plugin will call the Zend_Log::factory
Method, which in turn will check the Writer you defined and then call this Writer's factory
method to create a new Writer instance. But Zend_Log_Writer_Db
expects the first argument to it's constructor to be a Zend_Db_Adapter
instance, but it will not enforce it.
You cannot supply an instance from the ini file. The writer will check if the _db property is set when you try to write and throw an exception when it's null. But you supplied a string for the property, so _db is not null and it won't throw. Instead, when trying to write your writer will $this->_db->insert()
, causing the error, because _db should be the missing Zend_Db_Adapter
, but is a string, e.g. a non-object.
See the factory() and _write() method for details:
- http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Log/Writer/Db.php
I used this the bootstrap:
protected function _initLog()
{
$options = $this->getOption('resources');
$db = Zend_Registry::get('db');
$options['log']['db']['writerParams']['db'] = $db;
$logOptions = $options['log'];
$logger = Zend_Log::factory($logOptions);
$logger->addPriority('DBLOG', 9);
Zend_Registry::set('logger', $logger);
}
Then you can use the application.ini for the other settings:
resources.log.db.writerName = "Db"
;resources.log.db.writerParams.adapter = <<this must be a database connection, configured in bootstrap>>
resources.log.db.writerParams.table = "users_logs"
resources.log.db.writerParams.columnMap.id = "id"
resources.log.db.writerParams.columnMap.created = "created"
resources.log.db.filterName = "Priority"
resources.log.db.filterParams.priority = 9
resources.log.db.filterParams.operator = "=="
精彩评论