[Zend Framework] Zend_Log: 任意の処理でロギングする
application.ini の設定を読み込み、任意の処理でログをとれるようにします。
[markdown]
Zend_Log の設定は以下で済ませたものを使います。
> * [Zend_Log – エラーコントローラで受けたエラーをログ出力する | deadwood](https://www.d-wood.com/blog/2013/07/30_4394.html)
> * [Zend_Log – 複数ライターへの出力、複数ログの出力 | deadwood](https://www.d-wood.com/blog/2013/07/30_4399.html)
> * [Zend_Log – 出力フォーマットを変更する | deadwood](https://www.d-wood.com/blog/2013/07/30_4403.html)
## 設定
try – catch でつかんだエラー処理のログを残したい、という時に使いそうに思います。
> * [Zend_Exception – エラー処理を用意する、エラーコントローラにスローする | deadwood](https://www.d-wood.com/blog/2013/07/30_4390.html)
Bootstrap.php に、application.ini の設定を読み込み Zend_Registry に保存しておくようにしています。
> * [Create a logger in Zend Framework using application.ini | Niklas Tech Blog](http://blog.niklasottosson.com/?p=1158)
> * [ZendFrameworkでのログ](http://bitter-development.blogspot.jp/2011/06/zendframework.html)
“`php:application/Bootstrap.php
protected function _initLogger()
{
$this->bootstrap(“log”);
$logger = $this->getResource(“log”);
Zend_Registry::set(“logger”, $logger);
}
“`
ログを取りたいコントローラです。
message に取得したエラーをつっこんで、priority を数字で渡してみます。
“`php:application/controllersIndexController.php
try {
// Zend_Loader::loadClass() で、存在しないクラスを指定してコールすると
// Zend_Loader で例外がスローされます
Zend_Loader::loadClass(‘nonexistantclass’);
} catch (Zend_Exception $e) {
echo “キャッチした例外: ” . get_class($e) . “
“;
echo “メッセージ: ” . $e->getMessage() . “
“;
//Get the logger handle from the register
$logger = Zend_Registry::get(‘logger’);
//Use the logger
$logger->log(“エラー”, 3, $e);
}
“`
$e を引数で渡すと、%info% に収まってくれました。
## 出力結果
“`:data/logs/application.log
2013-07-30T22:21:09+09:00 ERR (3): エラー 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(32): 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%
“`
## ユーザー定義を追加する
何かの処理に成功したときにログを残す、というようなこともあると思うのですが?
とりあえず想像で作ってみます。
> * [概要 – Zend_Log – Zend Framework](http://framework.zend.com/manual/1.12/ja/zend.log.overview.html)
> * [zend framework – Zend_Log with multiple writers in application.ini – Stack Overflow](http://stackoverflow.com/questions/7335482/zend-log-with-multiple-writers-in-application-ini)
priority 8 を要素として追加しておきます。
“`php:application/Bootstrap.php
protected function _initLogger()
{
$this->bootstrap(“log”);
$logger = $this->getResource(“log”);
$logger->addPriority(‘USERACTION’, 8);
Zend_Registry::set(“logger”, $logger);
}
“`
フィルターレベルも8に下げておきます。
“`php:application/configs/application.ini
resources.log.stream.filterParams.priority = 8
“`
try – catch 内で例外が投げられなかった場合に、Success ログをします。
“`php:application/controllersIndexController.php
try {
// Zend_Loader::loadClass() で、存在しないクラスを指定してコールすると
// Zend_Loader で例外がスローされます
// Zend_Loader::loadClass(‘nonexistantclass’);
//Get the logger handle from the register
$logger = Zend_Registry::get(‘logger’);
//Use the logger
$logger->log(“Success”, 8, $this->getRequest()->getParams());
} catch (Zend_Exception $e) {
echo “キャッチした例外: ” . get_class($e) . “
“;
echo “メッセージ: ” . $e->getMessage() . “
“;
// その他、エラーから復帰するためのコード
}
“`
%module%/%controller%/%action% の値もうまく収まって、ログできました。
“`:data/logs/application.log
2013-07-30T21:40:02+09:00 USERACTION (8): Success %info% on default/index/index
“`
## 補遺
アクションヘルパーで行っている方もいました。
> * [ログ出力用のアクションヘルパー | Inhale n' Exhale](http://h2plus.biz/hiromitsu/entry/32#more-32)
[/markdown]