Source for file jLog.class.php

Documentation is available at jLog.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage core
  5. @author     Laurent Jouanneau
  6. @contributor F. Fernandez, Hadrien Lanneau
  7. @copyright  2006-2010 Laurent Jouanneau, 2007 F. Fernandez, 2011 Hadrien Lanneau
  8. @link       http://www.jelix.org
  9. @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11.  
  12. /**
  13.  * interface for log message. A component which want to log
  14.  * a message can use an object implementing this interface.
  15.  * Classes that implements it are responsible to format
  16.  * the message. Formatting a message depends on its type.
  17.  */
  18. interface jILogMessage {
  19.     /**
  20.      * return the category of the message
  21.      * @return string category name
  22.      */
  23.     public function getCategory();
  24.  
  25.     /**
  26.      * @return string the message
  27.      */
  28.     public function getMessage();
  29.  
  30.     /**
  31.      * return the full message, formated for simple text output (it can contain informations
  32.      * other than the message itself)
  33.      * @return string the message
  34.      */
  35.     public function getFormatedMessage();
  36. }
  37.  
  38. require(JELIX_LIB_CORE_PATH.'log/jLogMessage.class.php');
  39. require(JELIX_LIB_CORE_PATH.'log/jLogErrorMessage.class.php');
  40.  
  41.  
  42. /**
  43.  * interface for loggers
  44.  */
  45. interface jILogger {
  46.     /**
  47.      * @param jILogMessage $message the message to log
  48.      */
  49.     function logMessage($message);
  50.  
  51.     /**
  52.      * output messages to the given response
  53.      * @param jResponse $response 
  54.      */
  55.     function output($response);
  56. }
  57.  
  58. require(JELIX_LIB_CORE_PATH.'log/jFileLogger.class.php');
  59.  
  60. /**
  61.  * utility class to log some message into a file into yourapp/var/log
  62.  * @package    jelix
  63.  * @subpackage utils
  64.  * @static
  65.  */
  66. class jLog {
  67.  
  68.     protected static $loggers array();
  69.  
  70.     /**
  71.      * all messages, when the memory logger is used
  72.      * @var array  array of jILogMessage
  73.      */
  74.     protected static $allMessages array();
  75.  
  76.     /**
  77.      * messages count of each categories, for the memory logger
  78.      */
  79.     protected static $messagesCount array();
  80.  
  81.     /**
  82.      * private constructor. static class
  83.      */
  84.     private function __construct(){}
  85.  
  86.     /**
  87.     * log a dump of a php value (object or else) into the given category
  88.     * @param mixed $obj the value to dump
  89.     * @param string $label a label
  90.     * @param string $category the message category
  91.     */
  92.     public static function dump($obj$label=''$category='default'){
  93.         $message new jLogDumpMessage($obj$label$category);
  94.         self::_dispatchLog($message);
  95.     }
  96.  
  97.     /**
  98.     * log a message into the given category.
  99.     * Warning: since it is called by error handler, it should not trigger errors!
  100.     * and should take care of case were an error could appear
  101.     * @param mixed $message 
  102.     * @param string $category the log type
  103.     */
  104.     public static function log ($message$category='default'{
  105.         if (!is_object($message|| $message instanceof jILogMessage)
  106.             $message new jLogMessage($message$category);
  107.         self::_dispatchLog($message);
  108.     }
  109.  
  110.     /**
  111.     * log an exception into the given category.
  112.     * @param Exception $exception 
  113.     * @param string $category the log type
  114.     */
  115.     public static function logEx ($exception$category='default'{
  116.         $message new jLogErrorMessage($category,
  117.                                         $exception->getCode()$exception->getMessage(),
  118.                                         $exception->getFile()$exception->getLine(),
  119.                                         $exception->getTrace());
  120.         self::_dispatchLog($message);
  121.     }
  122.  
  123.     protected static function _dispatchLog($message{
  124.         global $gJConfig;
  125.         $category $message->getCategory();
  126.         if (!isset($gJConfig->logger[$category])
  127.             || strpos($category'option_'=== 0// option_* ar not some type of log messages
  128.             $category 'default';
  129.         }
  130.  
  131.         $all $gJConfig->logger['_all'];
  132.         $loggers preg_split('/[\s,]+/'$gJConfig->logger[$category]);
  133.  
  134.         if ($all != ''{
  135.             $allLoggers preg_split('/[\s,]+/'$all);
  136.             self::_log($message$allLoggers);
  137.             $loggers array_diff($loggers$allLoggers);
  138.         }
  139.  
  140.         self::_log($message$loggers);
  141.     }
  142.  
  143.     protected static function _log($message$loggers{
  144.  
  145.         // let's inject the message in all loggers
  146.         foreach($loggers as $loggername{
  147.             if ($loggername == '')
  148.                 continue;
  149.             if ($loggername == 'memory'{
  150.                 global $gJConfig;
  151.                 $cat $message->getCategory();
  152.                 if (isset($gJConfig->memorylogger[$cat]))
  153.                     $max intval($gJConfig->memorylogger[$cat]);
  154.                 else {
  155.                     $max intval($gJConfig->memorylogger['default']);
  156.                 }
  157.                 if (!isset(self::$messagesCount[$cat])) {
  158.                     self::$messagesCount[$cat0;
  159.                 }
  160.                 if(++self::$messagesCount[$cat$max{
  161.                     continue;
  162.                 }
  163.                 self::$allMessages[$message;
  164.                 continue;
  165.             }
  166.             if(!isset(self::$loggers[$loggername])) {
  167.                 if ($loggername == 'file')
  168.                     self::$loggers[$loggernamenew jFileLogger();
  169.                 elseif ($loggername == 'syslog'{
  170.                     require(JELIX_LIB_CORE_PATH.'log/jSyslogLogger.class.php');
  171.                     self::$loggers[$loggernamenew jSyslogLogger();
  172.                 }
  173.                 elseif ($loggername == 'mail'{
  174.                     require(JELIX_LIB_CORE_PATH.'log/jMailLogger.class.php');
  175.                     self::$loggers[$loggernamenew jMailLogger();
  176.                 }
  177.                 else {
  178.                     $l jApp::loadPlugin($loggername'logger''.logger.php'$loggername.'Logger');
  179.                     if (is_null($l))
  180.                         continue// yes, silent, because we could be inside an error handler
  181.                     self::$loggers[$loggername$l;
  182.                 }
  183.             }
  184.             self::$loggers[$loggername]->logMessage($message);
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * returns messages stored in memory (if the memory logger is activated)
  190.      * @param string|array$filter if given, category or list of categories
  191.      *                              of messages you want to retrieve
  192.      * @return array  the list of jILogMessage object
  193.      */
  194.     public static function getMessages($filter false{
  195.         if ($filter === false || self::$allMessages === null)
  196.             return self::$allMessages;
  197.         if (is_string ($filter))
  198.             $filter array($filter);
  199.         $list array();
  200.         foreach(self::$allMessages as $msg{
  201.             if (in_array($msg->getCategory()$filter))
  202.                 $list[$msg;
  203.         }
  204.         return $list;
  205.     }
  206.  
  207.     /**
  208.      * @return integer 
  209.      */
  210.     static function getMessagesCount($category{
  211.         if (isset(self::$messagesCount[$category])) {
  212.             return self::$messagesCount[$category];
  213.         }
  214.         return 0;
  215.     }
  216.  
  217.     /**
  218.      * call each loggers so they have the possibility to inject data into the
  219.      * given response
  220.      * @param jResponse $response 
  221.      */
  222.     public static function outputLog($response{
  223.         foreach(self::$loggers as $logger{
  224.             $logger->output($response);
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * indicate if, for the given category, the given logger is activated
  230.      * @param string $logger the logger name
  231.      * @param string $category the category
  232.      * @return boolean true if it is activated
  233.      */
  234.     public static function isPluginActivated($logger$category{
  235.         global $gJConfig;
  236.  
  237.         $loggers preg_split('/[\s,]+/'$gJConfig->logger['_all']);
  238.         if (in_array($logger$loggers))
  239.             return true;
  240.  
  241.         if (!isset($gJConfig->logger[$category]))
  242.             return false;
  243.  
  244.         $loggers preg_split('/[\s,]+/'$gJConfig->logger[$category]);
  245.         return in_array($logger$loggers);
  246.     }
  247.  
  248. }

Documentation generated on Wed, 24 Sep 2014 22:00:57 +0200 by phpDocumentor 1.4.3