File indexing completed on 2024-11-24 05:18:58

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_Log
0010  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0011  * @copyright   Akira Ohgaki
0012  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0013  * @link        https://github.com/akiraohgaki/flooer
0014  */
0015 
0016 /**
0017  * Usage
0018  *
0019  * $log = new Flooer_Log();
0020  * $log->setFile('./errors.log');
0021  * $log->setMail('email@address');
0022  * $log->log('Message', LOG_ERR);
0023  */
0024 
0025 /**
0026  * Error logger class
0027  *
0028  * @category    Flooer
0029  * @package     Flooer_Log
0030  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0031  */
0032 class Flooer_Log
0033 {
0034 
0035     /**
0036      * Configuration options
0037      *
0038      * @var     array
0039      */
0040     protected $_config = array(
0041         'format' => '%timestamp% %priorityName% (%priority%): %message%',
0042         'file' => null,
0043         'fileMaxLength' => 1024,
0044         'mail' => null,
0045         'mailAdditionalHeaders' => null,
0046         'mailFilter' => LOG_ERR
0047     );
0048 
0049     /**
0050      * Priorities
0051      *
0052      * @var     array
0053      */
0054     protected $_priorities = array(
0055         LOG_EMERG => 'EMERG',
0056         LOG_ALERT => 'ALERT',
0057         LOG_CRIT => 'CRIT',
0058         LOG_ERR => 'ERR',
0059         LOG_WARNING => 'WARN',
0060         LOG_NOTICE => 'NOTICE',
0061         LOG_INFO => 'INFO',
0062         LOG_DEBUG => 'DEBUG'
0063     );
0064 
0065     /**
0066      * Error log
0067      *
0068      * @var     array
0069      */
0070     protected $_errorLog = array();
0071 
0072     /**
0073      * Constructor
0074      *
0075      * @param   array $config
0076      * @return  void
0077      */
0078     public function __construct(array $config = null)
0079     {
0080         if ($config) {
0081             $this->_config = $config + $this->_config;
0082         }
0083     }
0084 
0085     /**
0086      * Write a log and/or send an email notification
0087      *
0088      * @param   string $message
0089      * @param   int $priority Available values: 0-7 (LOG_EMERG-LOG_DEBUG)
0090      * @return  bool
0091      */
0092     public function log($message, $priority = null)
0093     {
0094         if ($priority === null) {
0095             $priority = LOG_NOTICE;
0096         }
0097         if (isset($this->_priorities[$priority])) {
0098             $log = $this->_config['format'];
0099             $log = str_replace('%timestamp%', date('c'), $log);
0100             $log = str_replace('%priorityName%', $this->_priorities[$priority], $log);
0101             $log = str_replace('%priority%', $priority, $log);
0102             $log = str_replace('%message%', $message, $log);
0103             $log .= "\n";
0104             $this->_errorLog[] = $log;
0105             $bool = false;
0106             if ($this->_config['file']
0107                 && is_dir(dirname($this->_config['file']))
0108             ) {
0109                 ini_set('log_errors', 1);
0110                 ini_set('log_errors_max_len', $this->_config['fileMaxLength']);
0111                 //ini_set('error_log', $this->_config['file']);
0112                 $bool = error_log($log, 3, $this->_config['file']);
0113             }
0114             if ($this->_config['mail']
0115                 && $priority <= $this->_config['mailFilter']
0116             ) {
0117                 $bool = mail(
0118                     $this->_config['mail'],
0119                     __CLASS__ . ' message',
0120                     wordwrap($log, 70),
0121                     $this->_config['mailAdditionalHeaders']
0122                 );
0123             }
0124             return $bool;
0125         }
0126         return false;
0127     }
0128 
0129     /**
0130      * Set a log format
0131      *
0132      * @param   string $format
0133      * @return  void
0134      */
0135     public function setFormat($format)
0136     {
0137         $this->_config['format'] = $format;
0138     }
0139 
0140     /**
0141      * Get a log format
0142      *
0143      * @return  string
0144      */
0145     public function getFormat()
0146     {
0147         return $this->_config['format'];
0148     }
0149 
0150     /**
0151      * Set the path of a log file
0152      *
0153      * @param   string $path
0154      * @return  void
0155      */
0156     public function setFile($path)
0157     {
0158         $this->_config['file'] = $path;
0159     }
0160 
0161     /**
0162      * Get the path of a log file
0163      *
0164      * @return  string
0165      */
0166     public function getFile()
0167     {
0168         return $this->_config['file'];
0169     }
0170 
0171     /**
0172      * Set the max length of a log file
0173      *
0174      * @param   int $maxLength
0175      * @return  void
0176      */
0177     public function setFileMaxLength($maxLength)
0178     {
0179         $this->_config['fileMaxLength'] = $maxLength;
0180     }
0181 
0182     /**
0183      * Get the max length of a log file
0184      *
0185      * @return  int
0186      */
0187     public function getFileMaxLength()
0188     {
0189         return $this->_config['fileMaxLength'];
0190     }
0191 
0192     /**
0193      * Set an email address
0194      *
0195      * @param   string $mail
0196      * @return  void
0197      */
0198     public function setMail($mail)
0199     {
0200         $this->_config['mail'] = $mail;
0201     }
0202 
0203     /**
0204      * Get an email address
0205      *
0206      * @return  string
0207      */
0208     public function getMail()
0209     {
0210         return $this->_config['mail'];
0211     }
0212 
0213     /**
0214      * Set the additional headers of an email
0215      *
0216      * @param   string $additionalHeaders
0217      * @return  void
0218      */
0219     public function setMailAdditionalHeaders($additionalHeaders)
0220     {
0221         $this->_config['mailAdditionalHeaders'] = $additionalHeaders;
0222     }
0223 
0224     /**
0225      * Get the additional headers of an email
0226      *
0227      * @return  string
0228      */
0229     public function getMailAdditionalHeaders()
0230     {
0231         return $this->_config['mailAdditionalHeaders'];
0232     }
0233 
0234     /**
0235      * Set the level of an email notification
0236      *
0237      * @param   int $level Available values: 0-7 (LOG_EMERG-LOG_DEBUG)
0238      * @return  void
0239      */
0240     public function setMailFilter($level)
0241     {
0242         $this->_config['mailFilter'] = $level;
0243     }
0244 
0245     /**
0246      * Get the level of an email notification
0247      *
0248      * @return  int
0249      */
0250     public function getMailFilter()
0251     {
0252         return $this->_config['mailFilter'];
0253     }
0254 
0255     /**
0256      * Get an error log
0257      *
0258      * @return  array
0259      */
0260     public function getErrorLog()
0261     {
0262         return $this->_errorLog;
0263     }
0264 
0265 }