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
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...)
61 function Ethna_Plugin_Logwriter()
69 * @param int $option ログオプション(LOG_FILE,LOG_FUNCTION...)
71 function setOption($option)
73 $this->ident = $option['ident'];
74 $this->facility = $option['facility'];
75 $this->option = $option;
76 $this->have_backtrace = function_exists('debug_backtrace');
92 * @param int $level ログレベル(LOG_DEBUG, LOG_NOTICE...)
93 * @param string $message ログメッセージ(+引数)
95 function log($level, $message)
112 * @return string ログアイデンティティ文字列
123 * @param int $level ログレベル(LOG_DEBUG,LOG_NOTICE...)
124 * @return string ログレベル表示文字列(LOG_DEBUG→"DEBUG")
126 function _getLogLevelName($level)
128 if (isset($this->level_name_table[$level]) == false) {
131 return $this->level_name_table[$level];
135 * ログ出力箇所の情報(関数名/ファイル名等)を取得する
138 * @return array ログ出力箇所の情報
140 function _getBacktrace()
142 $skip_method_list = array(
143 array('ethna', 'raise'),
144 array(null, 'raiseerror'),
145 array(null, 'handleerror'),
146 array('ethna_logger', null),
147 array('ethna_plugin_logwriter', null),
148 array('ethna_error', null),
149 array('ethna_apperror', null),
150 array('ethna_actionerror', null),
151 array('ethna_backend', 'log'),
152 array(null, 'ethna_error_handler'),
153 array(null, 'trigger_error'),
156 if ($this->have_backtrace == false) {
160 $bt = debug_backtrace();
162 while ($i < count($bt)) {
163 if (isset($bt[$i]['class']) == false) {
164 $bt[$i]['class'] = null;
169 foreach ($skip_method_list as $method) {
170 $class = $function = true;
171 if ($method[0] != null) {
172 $class = preg_match("/^$method[0]/i", $bt[$i]['class']);
174 if ($method[1] != null) {
175 $function = preg_match("/^$method[1]/i", $bt[$i]['function']);
177 if ($class && $function) {
190 $c =& Ethna_Controller::getInstance();
191 $basedir = $c->getBasedir();
193 $function = sprintf("%s.%s", isset($bt[$i]['class']) ? $bt[$i]['class'] : 'global', $bt[$i]['function']);
195 $file = $bt[$i]['file'];
196 if (strncmp($file, $basedir, strlen($basedir)) == 0) {
197 $file = substr($file, strlen($basedir));
199 if (strncmp($file, ETHNA_BASE, strlen(ETHNA_BASE)) == 0) {
200 $file = preg_replace('#^/+#', '', substr($file, strlen(ETHNA_BASE)));
202 $line = $bt[$i]['line'];
203 return array('function' => $function, 'pos' => sprintf('%s:%s', $file, $line));