2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 // {{{ Ethna_Plugin_Logwriter
16 * @author Masaki Fujimoto <fujimoto@php.net>
20 class Ethna_Plugin_Logwriter extends Ethna_Plugin_Abstract
26 /** @var string ログアイデンティティ文字列 */
29 /** @var int ログファシリティ */
32 /** @var int ログオプション */
35 /** @var bool バックトレースが取得可能かどうか */
38 /** @var array ログレベル名テーブル */
39 var $level_name_table = array(
44 LOG_WARNING => 'WARNING',
45 LOG_NOTICE => 'NOTICE',
53 * Logwriterクラスのコンストラクタ
56 * @param string $log_ident ログアイデンティティ文字列(プロセス名等)
57 * @param int $log_facility ログファシリティ
58 * @param string $log_file ログ出力先ファイル名(LOG_FILEオプションが指定されている場合のみ)
59 * @param int $log_option ログオプション(LOG_FILE,LOG_FUNCTION...)
62 function Ethna_Plugin_Logwriter()
71 * @param int $option ログオプション(LOG_FILE,LOG_FUNCTION...)
73 function setOption($option)
75 $this->ident = $option['ident'];
76 $this->facility = $option['facility'];
77 $this->option = $option;
78 $this->have_backtrace = function_exists('debug_backtrace');
94 * @param int $level ログレベル(LOG_DEBUG, LOG_NOTICE...)
95 * @param string $message ログメッセージ(+引数)
97 function log($level, $message)
114 * @return string ログアイデンティティ文字列
125 * @param int $level ログレベル(LOG_DEBUG,LOG_NOTICE...)
126 * @return string ログレベル表示文字列(LOG_DEBUG→"DEBUG")
128 function _getLogLevelName($level)
130 if (isset($this->level_name_table[$level]) == false) {
133 return $this->level_name_table[$level];
137 * ログ出力箇所の情報(関数名/ファイル名等)を取得する
140 * @return array ログ出力箇所の情報
142 function _getBacktrace()
144 $skip_method_list = array(
145 array('ethna', 'raise'),
146 array(null, 'raiseerror'),
147 array(null, 'handleerror'),
148 array('ethna_logger', null),
149 array('ethna_plugin_logwriter', null),
150 array('ethna_error', null),
151 array('ethna_apperror', null),
152 array('ethna_actionerror', null),
153 array('ethna_backend', 'log'),
154 array(null, 'ethna_error_handler'),
155 array(null, 'trigger_error'),
158 if ($this->have_backtrace == false) {
162 $bt = debug_backtrace();
164 while ($i < count($bt)) {
165 if (isset($bt[$i]['class']) == false) {
166 $bt[$i]['class'] = null;
168 if (isset($bt[$i]['file']) == false) {
169 $bt[$i]['file'] = null;
171 if (isset($bt[$i]['line']) == false) {
172 $bt[$i]['line'] = null;
178 foreach ($skip_method_list as $method) {
179 $class = $function = true;
180 if ($method[0] != null) {
181 $class = preg_match("/^$method[0]/i", $bt[$i]['class']);
183 if ($method[1] != null) {
184 $function = preg_match("/^$method[1]/i", $bt[$i]['function']);
186 if ($class && $function) {
199 $c =& Ethna_Controller::getInstance();
200 $basedir = $c->getBasedir();
202 $function = sprintf("%s.%s", isset($bt[$i]['class']) ? $bt[$i]['class'] : 'global', $bt[$i]['function']);
204 $file = $bt[$i]['file'];
205 if (strncmp($file, $basedir, strlen($basedir)) == 0) {
206 $file = substr($file, strlen($basedir));
208 if (strncmp($file, ETHNA_BASE, strlen(ETHNA_BASE)) == 0) {
209 $file = preg_replace('#^/+#', '', substr($file, strlen(ETHNA_BASE)));
211 $line = $bt[$i]['line'];
212 return array('function' => $function, 'pos' => sprintf('%s:%s', $file, $line));