*/
class Ethna_Logger extends Ethna_AppManager
{
+ // {{{ properties
/**#@+
* @access private
*/
var $log_stack = array();
/**#@-*/
+ // }}}
+ // {{{ Ethna_Logger
/**
* Ethna_Logger¥¯¥é¥¹¤Î¥³¥ó¥¹¥È¥é¥¯¥¿
*
}
}
+ $config_log = $config->get('log');
+
// ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£
- $this->facility = $this->_parseLogFacility($config->get('log_facility'));
+ if (is_array($config_log)) {
+ $this->facility = array_keys($config_log);
+ } else {
+ $this->facility = $this->_parseLogFacility($config->get('log_facility'));
+ }
foreach ($this->facility as $f) {
// ¥í¥°¥ì¥Ù¥ë
- $level = $this->_parseLogLevel($config->get("log_level_$f"));
- if (is_null($level)) {
- $level = $this->_parseLogLevel($config->get("log_level"));
+ if (isset($config_log[$f]['level'])) {
+ $this->level[$f] = $this->_parseLogLevel($config_log[$f]['level']);
+ } else if (($level = $config->get("log_level_$f")) !== null) {
+ $this->level[$f] = $this->_parseLogLevel($level);
+ } else if (($level = $config->get("log_level")) !== null) {
+ $this->level[$f] = $this->_parseLogLevel($level);
+ } else {
+ $this->level[$f] = LOG_WARNING;
}
- if (is_null($level)) {
- $level = LOG_WARNING;
+
+ // ¥á¥Ã¥»¡¼¥¸¥Õ¥£¥ë¥¿(filter_do)
+ if (isset($config_log[$f]['filter_do'])) {
+ $this->message_filter_do[$f] = $config_log[$f]['filter_do'];
+ } else if (($filter = $config->get("log_filter_do_$f")) !== null) {
+ $this->message_filter_do[$f] = $filter;
+ } else if (($filter = $config->get("log_filter_do")) !== null) {
+ $this->message_filter_do[$f] = $filter;
+ } else {
+ $this->message_filter_do[$f] = '';
}
- $this->level[$f] = $level;
- // ¥í¥°¥ª¥×¥·¥ç¥ó
- $option = $this->_parseLogOption($config->get("log_option_$f"));
- if (is_null($option)) {
- $option = $this->_parseLogOption($config->get("log_option"));
+ // ¥á¥Ã¥»¡¼¥¸¥Õ¥£¥ë¥¿(filter_ignore)
+ if (isset($config_log[$f]['filter_ignore'])) {
+ $this->message_filter_ignore[$f] = $config_log[$f]['filter_ignore'];
+ } else if (($filter = $config->get("log_filter_ignore_$f")) !== null) {
+ $this->message_filter_ignore[$f] = $filter;
+ } else if (($filter = $config->get("log_filter_ignore")) !== null) {
+ $this->message_filter_ignore[$f] = $filter;
+ } else {
+ $this->message_filter_ignore[$f] = '';
}
- $this->option[$f] = $option;
- // ¥á¥Ã¥»¡¼¥¸¥Õ¥£¥ë¥¿
- $message_filter_do = $config->get("log_filter_do_$f");
- if (is_null($message_filter_do)) {
- $message_filter_do = $config->get("log_filter_do");
+ // ¤½¤Î¤¿¥ª¥×¥·¥ç¥ó (unset¤Ï¤»¤º¤Ë¤½¤Î¤Þ¤ÞÅϤ¹)
+ if (isset($config_log[$f])) {
+ $this->option[$f] = $config_log[$f];
+ } else {
+ $this->option[$f] = array();
}
- $this->message_filter_do[$f] = $message_filter_do;
- $message_filter_ignore = $config->get("log_filter_ignore_$f");
- if (is_null($message_filter_ignore)) {
- $message_filter_ignore = $config->get("log_filter_ignore");
+ // 'option' ¤Ë¤è¤ë¥ª¥×¥·¥ç¥ó»ØÄê (for B.C.)
+ if (isset($config_log[$f]['option'])) {
+ $option = $this->_parseLogOption($config_log[$f]['option']);
+ } else if (($option = $config->get("log_option_$f")) !== null) {
+ $option = $this->_parseLogOption($option);
+ } else if (($option = $config->get("log_option")) !== null) {
+ $option = $this->_parseLogOption($option);
+ }
+ if ($option !== null) {
+ $this->option[$f] = array_merge($this->option[$f], $option);
}
- $this->message_filter_ignore[$f] = $message_filter_ignore;
}
// ¥¢¥é¡¼¥È¥ª¥×¥·¥ç¥ó
- $this->alert_level = $this->_parseLogLevel($config->get('log_alert_level'));
- $this->alert_mailaddress = preg_split('/\s*,\s*/', $config->get('log_alert_mailaddress'));
+ $this->alert_level =
+ $this->_parseLogLevel($config->get('log_alert_level'));
+ $this->alert_mailaddress
+ = preg_split('/\s*,\s*/', $config->get('log_alert_mailaddress'));
}
+ // }}}
+ // {{{ getLogFacility
/**
* ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£¤ò¼èÆÀ¤¹¤ë
*
* @access public
- * @return mixed ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£(¥Õ¥¡¥·¥ê¥Æ¥£¤¬1¤Ä°Ê²¼¤Ê¤éscalar¡¢2¤Ä°Ê¾å¤Ê¤éÇÛÎó¤òÊÖ¤¹ for B.C.)
+ * @return mixed ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£(¥Õ¥¡¥·¥ê¥Æ¥£¤¬1¤Ä°Ê²¼¤Ê¤éscalar¡¢
+ * 2¤Ä°Ê¾å¤Ê¤éÇÛÎó¤òÊÖ¤¹ for B.C.)
*/
function getLogFacility()
{
}
return $this->facility;
}
+ // }}}
+ // {{{ errorLevelToLogLevel
/**
* PHP¥¨¥é¡¼¥ì¥Ù¥ë¤ò¥í¥°¥ì¥Ù¥ë¤ËÊÑ´¹¤¹¤ë
*
}
return array($level, $code);
}
+ // }}}
+ // {{{ begin
/**
* ¥í¥°½ÐÎϤò³«»Ï¤¹¤ë
*
$this->writer[$f] =& $this->_getLogWriter($this->option[$f], $f);
if (Ethna::isError($this->writer[$f])) {
// use default
- $this->writer[$f] =& $this->_getLogWriter($this->option[$f], "default");
+ $this->writer[$f] =& $this->_getLogWriter($this->option[$f],
+ "default");
}
}
}
}
}
+ // }}}
+ // {{{ log
/**
* ¥í¥°¤ò½ÐÎϤ¹¤ë
*
// ¥í¥°¥á¥Ã¥»¡¼¥¸¥Õ¥£¥ë¥¿(¥ì¥Ù¥ë¥Õ¥£¥ë¥¿¤ËÍ¥À褹¤ë)
$r = $this->_evalMessageMask($this->message_filter_do[$key], $message);
if (is_null($r)) {
- $r = $this->_evalMessageMask($this->message_filter_ignore[$key], $message);
+ $r = $this->_evalMessageMask($this->message_filter_ignore[$key],
+ $message);
if ($r) {
continue;
}
}
}
}
+ // }}}
+ // {{{ end
/**
* ¥í¥°½ÐÎϤò½ªÎ»¤¹¤ë
*
$this->is_begin = false;
}
+ // }}}
+ // {{{ _getLogWriter
/**
* LogWriter¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë
*
}
$plugin_manager =& $this->controller->getPlugin();
- $plugin_object = $plugin_manager->getPlugin('Logwriter', ucfirst(strtolower($plugin)));
+ $plugin_object = $plugin_manager->getPlugin('Logwriter',
+ ucfirst(strtolower($plugin)));
if (Ethna::isError($plugin_object)) {
return $plugin_object;
}
return $plugin_object;
}
+ // }}}
- /**
- * ¥í¥°¥ª¥×¥·¥ç¥ó(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
- *
- * @access private
- * @param string $string ¥í¥°¥ª¥×¥·¥ç¥ó(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)
- * @return array ²òÀϤµ¤ì¤¿ÀßÄê¥Õ¥¡¥¤¥ëÃÍ(¥¢¥é¡¼¥ÈÄÌÃΥ᡼¥ë¥¢¥É¥ì¥¹, ¥¢¥é¡¼¥ÈÂÐ¾Ý¥í¥°¥ì¥Ù¥ë, ¥í¥°¥ª¥×¥·¥ç¥ó)
- */
- function _parseLogOption($string)
- {
- if (is_null($string)) {
- return null;
- }
-
- $option = array();
- $elts = preg_split('/\s*,\s*/', $string);
- foreach ($elts as $elt) {
- if (preg_match('/^(.*?)\s*:\s*(.*)/', $elt, $match)) {
- $option[$match[1]] = $match[2];
- } else {
- $option[$elt] = true;
- }
- }
-
- return $option;
- }
-
+ // {{{ _alert
/**
* ¥¢¥é¡¼¥È¥á¡¼¥ë¤òÁ÷¿®¤¹¤ë
*
* @access protected
* @param string $message ¥í¥°¥á¥Ã¥»¡¼¥¸
* @return int 0:Àµ¾ï½ªÎ»
+ * @deprecated
*/
function _alert($message)
{
$header = "Mime-Version: 1.0\n";
$header .= "Content-Type: text/plain; charset=ISO-2022-JP\n";
$header .= "X-Alert: " . $this->controller->getAppId();
- $subject = sprintf("[%s] alert (%s%s)\n", $this->controller->getAppId(), substr($message, 0, 12), strlen($message) > 12 ? "..." : "");
+ $subject = sprintf("[%s] alert (%s%s)\n",
+ $this->controller->getAppId(),
+ substr($message, 0, 12),
+ strlen($message) > 12 ? "..." : "");
// ËÜʸ
$mail = sprintf("--- [log message] ---\n%s\n\n", $message);
if (function_exists("debug_backtrace")) {
$bt = debug_backtrace();
- $mail .= sprintf("--- [backtrace] ---\n%s\n", Ethna_Util::FormatBacktrace($bt));
+ $mail .= sprintf("--- [backtrace] ---\n%s\n",
+ Ethna_Util::FormatBacktrace($bt));
}
foreach ($this->alert_mailaddress as $mailaddress) {
- mail($mailaddress, $subject, mb_convert_encoding($mail, "ISO-2022-JP"), $header);
+ mail($mailaddress,
+ $subject,
+ mb_convert_encoding($mail, "ISO-2022-JP"),
+ $header);
}
set_error_handler("ethna_error_handler");
return 0;
}
+ // }}}
+ // {{{ _evalMessageMask
/**
* ¥í¥°¥á¥Ã¥»¡¼¥¸¤Î¥Þ¥¹¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦
*
return null;
}
+ // }}}
+ // {{{ _evalLevelMask
/**
* ¥í¥°¥ì¥Ù¥ë¤Î¥Þ¥¹¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦
*
}
// ÃΤé¤Ê¤¤¥ì¥Ù¥ë¤Ê¤é½ÐÎϤ·¤Ê¤¤
- if (isset($log_level_table[$src]) == false || isset($log_level_table[$dst]) == false) {
+ if (isset($log_level_table[$src]) == false
+ || isset($log_level_table[$dst]) == false) {
return true;
}
return true;
}
+ // }}}
+
+ // {{{ _parseLogOption
+ /**
+ * ¥í¥°¥ª¥×¥·¥ç¥ó(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
+ *
+ * @access private
+ * @param mixed $option ¥í¥°¥ª¥×¥·¥ç¥ó(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)
+ * @return array ²òÀϤµ¤ì¤¿ÀßÄê¥Õ¥¡¥¤¥ëÃÍ(¥¢¥é¡¼¥ÈÄÌÃΥ᡼¥ë¥¢¥É¥ì¥¹,
+ * ¥¢¥é¡¼¥ÈÂÐ¾Ý¥í¥°¥ì¥Ù¥ë, ¥í¥°¥ª¥×¥·¥ç¥ó)
+ */
+ function _parseLogOption($option)
+ {
+ if (is_null($option)) {
+ return null;
+ } else if (is_array($option)) {
+ return $option;
+ }
+
+ $ret = array();
+ $elts = preg_split('/\s*,\s*/', $option);
+ foreach ($elts as $elt) {
+ if (preg_match('/^(.*?)\s*:\s*(.*)/', $elt, $match)) {
+ $ret[$match[1]] = $match[2];
+ } else {
+ $ret[$elt] = true;
+ }
+ }
+
+ return $ret;
+ }
+ // }}}
+ // {{{ _parseLogFacility
/**
* ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
*
$facility_list = preg_split('/\s*,\s*/', $facility, -1, PREG_SPLIT_NO_EMPTY);
return $facility_list;
}
+ // }}}
+ // {{{ _parseLogLevel
/**
* ¥í¥°¥ì¥Ù¥ë(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
*
return constant($constant_name);
}
+ // }}}
}
// }}}
?>
--- /dev/null
+<?php
+/**
+ * Ethna_Logger_Test.php
+ */
+
+/**
+ * Ethna_Logger¥¯¥é¥¹¤Î¥Æ¥¹¥È¥±¡¼¥¹
+ * (Logwriter¤Ç¤Ï¤Ê¤¯¡¢Logwriter¤Î¥Þ¥Í¡¼¥¸¥ã¤È¤·¤Æ¤ÎLogger¤Î¥Æ¥¹¥È)
+ *
+ * @access public
+ */
+class Ethna_Logger_Test extends Ethna_UnitTestBase
+{
+ function setUp()
+ {
+ // Config¥¯¥é¥¹¤òEthna_Logger_Test_Config¤ËÀßÄê
+ $this->ctl->class['config'] = 'Ethna_Logger_Test_Config';
+ $this->ctl->getConfig();
+ }
+
+ function tearDown()
+ {
+ // do nothing.
+ }
+
+ function _resetLoggerSetting($config)
+ {
+ unset($this->ctl->class_factory->object['logger']);
+ $config_obj =& $this->ctl->class_factory->object['config'];
+ $config_obj->config = $config;
+ }
+
+ /**
+ * old style log setting.
+ */
+ function test_parseSetting_Compatible()
+ {
+ $config = array(
+ 'log_facility' => 'echo',
+ 'log_level' => 'warning',
+ 'log_option' => 'pid,function,pos',
+ );
+ $this->_resetLoggerSetting($config);
+ $this->logger =& $this->ctl->getLogger();
+
+ // facility
+ $facility = $this->logger->getLogFacility();
+ $this->assertEqual($facility, 'echo'); // not array, but string (for B.C.)
+
+ // level
+ $level_echo = $this->logger->level['echo'];
+ $this->assertEqual($level_echo, LOG_WARNING);
+
+ // option
+ $option_echo = $this->logger->option['echo'];
+ $this->assertEqual($option_echo['pid'], true);
+ $this->assertEqual($option_echo['function'], true);
+ $this->assertEqual($option_echo['pos'], true);
+ }
+
+ /**
+ * structured style log setting.
+ */
+ function test_parseSetting_Structured()
+ {
+ $config = array(
+ 'log' => array(
+ 'echo' => array(
+ 'level' => 'warning',
+ ),
+ 'file' => array(
+ 'level' => 'notice',
+ 'file' => '/var/log/Ethna.log',
+ 'mode' => 0666,
+ ),
+ 'alertmail' => array(
+ 'level' => 'err',
+ 'mailaddress' => 'alert@ml.example.jp',
+ ),
+ ),
+ 'log_option' => 'pid,function,pos',
+ );
+ $this->_resetLoggerSetting($config);
+ $this->logger =& $this->ctl->getLogger();
+
+ // facility
+ $facility = $this->logger->getLogFacility();
+ $this->assertEqual($facility, array('echo', 'file', 'alertmail'));
+
+ // level
+ $level_echo = $this->logger->level['echo'];
+ $this->assertEqual($level_echo, LOG_WARNING);
+ $level_file = $this->logger->level['file'];
+ $this->assertEqual($level_file, LOG_NOTICE);
+ $level_alertmail = $this->logger->level['alertmail'];
+ $this->assertEqual($level_alertmail, LOG_ERR);
+
+ // option
+ $option_echo = $this->logger->option['echo'];
+ $this->assertEqual($option_echo['pid'], true);
+ $this->assertEqual($option_echo['function'], true);
+ $this->assertEqual($option_echo['pos'], true);
+
+ $option_file = $this->logger->option['file'];
+ $this->assertEqual($option_file['pid'], true);
+ $this->assertEqual($option_file['function'], true);
+ $this->assertEqual($option_file['pos'], true);
+ $this->assertEqual($option_file['file'], '/var/log/Ethna.log');
+ $this->assertEqual($option_file['mode'], 0666);
+
+ $option_alertmail = $this->logger->option['alertmail'];
+ $this->assertEqual($option_alertmail['pid'], true);
+ $this->assertEqual($option_alertmail['function'], true);
+ $this->assertEqual($option_alertmail['pos'], true);
+ $this->assertEqual($option_alertmail['mailaddress'], 'alert@ml.example.jp');
+ }
+
+ /**
+ * @todo log level filter, begin(), log(), end()
+ */
+ //function test_etcetc()
+ //{
+ // // not implemented yet.
+ //}
+}
+
+class Ethna_Logger_Test_Config extends Ethna_Config
+{
+ function Ethna_Logger_Test_Config()
+ {
+ // do nothing.
+ }
+}
+?>