[Zend Framework] Zend_Log: 出力フォーマットを変更する

前回出力したログには、エラーコントローラから渡されているはずの3つめの引数が出力されていませんでした。

application/controllers/ErrorController.php
$log->log($this->view->message, $priority, $errors->exception);
$log->log('Request Parameters', $priority, $errors->request->getParams());

出力できるようにフォーマットを変更してみます。

出力フォーマットを変更する

stackoverflow などで %info% や %module% %controller% %action% で望みの値がとれることは分かりましたが、なぜとれるのかスッキリ理解できていません。
tab とかどう表現するんでしょうか。

application/configs/application.ini
; +-----+
; | Log |
; +-----+
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 7
resources.log.stream.formatterName = "Simple"
resources.log.stream.formatterParams.format = "%timestamp% %priorityName% (%priority%): %message% %info% on %module%/%controller%/%action%" PHP_EOL

出力結果

以下、どんなログが入ったのかサンプルです。
存在しないクラスをロードした場合。

data/logs/application.log
2013-07-30T11:44:37+09:00 CRIT (2): Application error exception 'Zend_Exception' with message 'File "nonexistantclass.php" does not exist or class "nonexistantclass" was not found in the file' in /usr/local/zend/share/ZendFramework/library/Zend/Loader.php:87
Stack trace:
#0 /Users/***/projects/sample/application/controllers/IndexController.php(29): Zend_Loader::loadClass('nonexistantclas...')
#1 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Action.php(516): IndexController->indexAction()
#2 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('indexAction')
#3 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#4 /usr/local/zend/share/ZendFramework/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#5 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#6 /Users/***/projects/sample/public/index.php(26): Zend_Application->run()
#7 {main} on %module%/%controller%/%action%
2013-07-30T11:44:37+09:00 CRIT (2): Request Parameters %info% on default/index/index

DBエラーの場合。

data/logs/application.log
2013-07-30T15:16:17+09:00 CRIT (2): Application error exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'uid' in 'where clause'' in /usr/local/zend/share/ZendFramework/library/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /usr/local/zend/share/ZendFramework/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /usr/local/zend/share/ZendFramework/library/Zend/Db/Statement.php(303): Zend_Db_Statement_Pdo->_execute(Array)
#2 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Select), Array)
#4 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Abstract.php(737): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Select), Array)
#5 /Users/***/projects/sample/application/controllers/NewsController.php(25): Zend_Db_Adapter_Abstract->fetchAll(Object(Zend_Db_Select))
#6 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Action.php(516): NewsController->indexAction()
#7 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('indexAction')
#8 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 /usr/local/zend/share/ZendFramework/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#10 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#11 /Users/***/projects/sample/public/index.php(26): Zend_Application->run()
#12 {main}
Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'uid' in 'where clause'' in /usr/local/zend/share/ZendFramework/library/Zend/Db/Statement/Pdo.php:234
Stack trace:
#0 /usr/local/zend/share/ZendFramework/library/Zend/Db/Statement.php(303): Zend_Db_Statement_Pdo->_execute(Array)
#1 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#2 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Select), Array)
#3 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Abstract.php(737): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Select), Array)
#4 /Users/***/projects/sample/application/controllers/NewsController.php(25): Zend_Db_Adapter_Abstract->fetchAll(Object(Zend_Db_Select))
#5 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Action.php(516): NewsController->indexAction()
#6 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('indexAction')
#7 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 /usr/local/zend/share/ZendFramework/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#9 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#10 /Users/***/projects/sample/public/index.php(26): Zend_Application->run()
#11 {main} on %module%/%controller%/%action%
2013-07-30T15:16:17+09:00 CRIT (2): Request Parameters %info% on default/news/index

logger に渡す値を変更する

ErrorController に手を入れて、欲しい値を取得、logger に渡すようです。