OSDN Git Service

- refine log setting notation.
authorichii386 <ichii386@2ef88817-412d-0410-a32c-8029a115e976>
Sun, 26 Nov 2006 11:27:07 +0000 (11:27 +0000)
committerichii386 <ichii386@2ef88817-412d-0410-a32c-8029a115e976>
Sun, 26 Nov 2006 11:27:07 +0000 (11:27 +0000)
- add logger test case. (only test parsing configurations.)

class/Ethna_Logger.php
skel/etc.ini.php
test/Ethna_Config_Test.php
test/Ethna_Logger_Test.php [new file with mode: 0644]

index 554da4b..8315051 100644 (file)
@@ -29,6 +29,7 @@ define('LOG_ECHO', 1 << 17);
  */
 class Ethna_Logger extends Ethna_AppManager
 {
+    // {{{ properties
     /**#@+
      *  @access private
      */
@@ -98,7 +99,9 @@ class Ethna_Logger extends Ethna_AppManager
     var $log_stack = array();
 
     /**#@-*/
+    // }}}
     
+    // {{{ Ethna_Logger
     /**
      *  Ethna_Logger¥¯¥é¥¹¤Î¥³¥ó¥¹¥È¥é¥¯¥¿
      *
@@ -118,51 +121,84 @@ class Ethna_Logger extends Ethna_AppManager
             }
         }
 
+        $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()
     {
@@ -175,7 +211,9 @@ class Ethna_Logger extends Ethna_AppManager
         }
         return $this->facility;
     }
+    // }}}
 
+    // {{{ errorLevelToLogLevel
     /**
      *  PHP¥¨¥é¡¼¥ì¥Ù¥ë¤ò¥í¥°¥ì¥Ù¥ë¤ËÊÑ´¹¤¹¤ë
      *
@@ -199,7 +237,9 @@ class Ethna_Logger extends Ethna_AppManager
         }
         return array($level, $code);
     }
+    // }}}
 
+    // {{{ begin
     /**
      *  ¥í¥°½ÐÎϤò³«»Ï¤¹¤ë
      *
@@ -212,7 +252,8 @@ class Ethna_Logger extends Ethna_AppManager
             $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");
             }
         }
 
@@ -234,7 +275,9 @@ class Ethna_Logger extends Ethna_AppManager
             }
         }
     }
+    // }}}
 
+    // {{{ log
     /**
      *  ¥í¥°¤ò½ÐÎϤ¹¤ë
      *
@@ -258,7 +301,8 @@ class Ethna_Logger extends Ethna_AppManager
             // ¥í¥°¥á¥Ã¥»¡¼¥¸¥Õ¥£¥ë¥¿(¥ì¥Ù¥ë¥Õ¥£¥ë¥¿¤ËÍ¥À褹¤ë)
             $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;
                 }
@@ -285,7 +329,9 @@ class Ethna_Logger extends Ethna_AppManager
             }
         }
     }
+    // }}}
 
+    // {{{ end
     /**
      *  ¥í¥°½ÐÎϤò½ªÎ»¤¹¤ë
      *
@@ -299,7 +345,9 @@ class Ethna_Logger extends Ethna_AppManager
 
         $this->is_begin = false;
     }
+    // }}}
 
+    // {{{ _getLogWriter
     /**
      *  LogWriter¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë
      *
@@ -331,7 +379,8 @@ class Ethna_Logger extends Ethna_AppManager
         }
 
         $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;
         }
@@ -346,39 +395,16 @@ class Ethna_Logger extends Ethna_AppManager
 
         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)
     {
@@ -388,24 +414,33 @@ class Ethna_Logger extends Ethna_AppManager
         $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
     /**
      *  ¥í¥°¥á¥Ã¥»¡¼¥¸¤Î¥Þ¥¹¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦
      *
@@ -424,7 +459,9 @@ class Ethna_Logger extends Ethna_AppManager
 
         return null;
     }
+    // }}}
 
+    // {{{ _evalLevelMask
     /**
      *  ¥í¥°¥ì¥Ù¥ë¤Î¥Þ¥¹¥¯¥Á¥§¥Ã¥¯¤ò¹Ô¤¦
      *
@@ -450,7 +487,8 @@ class Ethna_Logger extends Ethna_AppManager
         }
 
         // ÃΤé¤Ê¤¤¥ì¥Ù¥ë¤Ê¤é½ÐÎϤ·¤Ê¤¤
-        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;
         }
 
@@ -460,7 +498,40 @@ class Ethna_Logger extends Ethna_AppManager
 
         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
     /**
      *  ¥í¥°¥Õ¥¡¥·¥ê¥Æ¥£(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
      *
@@ -473,7 +544,9 @@ class Ethna_Logger extends Ethna_AppManager
         $facility_list = preg_split('/\s*,\s*/', $facility, -1, PREG_SPLIT_NO_EMPTY);
         return $facility_list;
     }
+    // }}}
 
+    // {{{ _parseLogLevel
     /**
      *  ¥í¥°¥ì¥Ù¥ë(ÀßÄê¥Õ¥¡¥¤¥ëÃÍ)¤ò²òÀϤ¹¤ë
      *
@@ -490,6 +563,7 @@ class Ethna_Logger extends Ethna_AppManager
 
         return constant($constant_name);
     }
+    // }}}
 }
 // }}}
 ?>
index aae8626..f77fce9 100644 (file)
@@ -32,17 +32,26 @@ $config = array(
     'log_facility'          => 'echo',
     'log_level'             => 'warning',
     'log_option'            => 'pid,function,pos',
-    'log_alert_level'       => 'crit',
-    'log_alert_mailaddress' => '',
     'log_filter_do'         => '',
     'log_filter_ignore'     => 'Undefined index.*%%.*tpl',
     // sample-2: mulitple facility
-    // 'log_facility'    => 'echo,file',
-    // 'log_level'       => 'warning',
-    // 'log_level_echo'  => 'notice',
-    // 'log_option'      => 'pid,function,pos',
-    // 'log_option_file' => 'dir:/tmp',
-    // ...
+    //'log' => array(
+    //    'echo'  => array(
+    //        'level'         => 'warning',
+    //    ),
+    //    'file'  => array(
+    //        'level'         => 'notice',
+    //        'file'          => '/var/log/{$project_prefix}.log',
+    //        'mode'          => 0666,
+    //    ),
+    //    'alertmail'  => array(
+    //        'level'         => 'err',
+    //        'mailaddress'   => 'alert@ml.example.jp',
+    //    ),
+    //),
+    //'log_option'            => 'pid,function,pos',
+    //'log_filter_do'         => '',
+    //'log_filter_ignore'     => 'Undefined index.*%%.*tpl',
 
     // memcache
     // sample-1: single (or default) memcache
index b53a444..1da8471 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- *  Ethna_Util_Test.php
+ *  Ethna_Config_Test.php
  */
 
 /**
diff --git a/test/Ethna_Logger_Test.php b/test/Ethna_Logger_Test.php
new file mode 100644 (file)
index 0000000..bc458d5
--- /dev/null
@@ -0,0 +1,134 @@
+<?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.
+    }
+}
+?>