rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

Log.php (10007B)


      1 <?php
      2 /**
      3  * Slim - a micro PHP 5 framework
      4  *
      5  * @author      Josh Lockhart <info@slimframework.com>
      6  * @copyright   2011 Josh Lockhart
      7  * @link        http://www.slimframework.com
      8  * @license     http://www.slimframework.com/license
      9  * @version     2.4.2
     10  * @package     Slim
     11  *
     12  * MIT LICENSE
     13  *
     14  * Permission is hereby granted, free of charge, to any person obtaining
     15  * a copy of this software and associated documentation files (the
     16  * "Software"), to deal in the Software without restriction, including
     17  * without limitation the rights to use, copy, modify, merge, publish,
     18  * distribute, sublicense, and/or sell copies of the Software, and to
     19  * permit persons to whom the Software is furnished to do so, subject to
     20  * the following conditions:
     21  *
     22  * The above copyright notice and this permission notice shall be
     23  * included in all copies or substantial portions of the Software.
     24  *
     25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     29  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     30  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     31  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     32  */
     33 namespace Slim;
     34 
     35 /**
     36  * Log
     37  *
     38  * This is the primary logger for a Slim application. You may provide
     39  * a Log Writer in conjunction with this Log to write to various output
     40  * destinations (e.g. a file). This class provides this interface:
     41  *
     42  * debug( mixed $object, array $context )
     43  * info( mixed $object, array $context )
     44  * notice( mixed $object, array $context )
     45  * warning( mixed $object, array $context )
     46  * error( mixed $object, array $context )
     47  * critical( mixed $object, array $context )
     48  * alert( mixed $object, array $context )
     49  * emergency( mixed $object, array $context )
     50  * log( mixed $level, mixed $object, array $context )
     51  *
     52  * This class assumes only that your Log Writer has a public `write()` method
     53  * that accepts any object as its one and only argument. The Log Writer
     54  * class may write or send its argument anywhere: a file, STDERR,
     55  * a remote web API, etc. The possibilities are endless.
     56  *
     57  * @package Slim
     58  * @author  Josh Lockhart
     59  * @since   1.0.0
     60  */
     61 class Log
     62 {
     63     const EMERGENCY = 1;
     64     const ALERT     = 2;
     65     const CRITICAL  = 3;
     66     const FATAL     = 3; //DEPRECATED replace with CRITICAL
     67     const ERROR     = 4;
     68     const WARN      = 5;
     69     const NOTICE    = 6;
     70     const INFO      = 7;
     71     const DEBUG     = 8;
     72 
     73     /**
     74      * @var array
     75      */
     76     protected static $levels = array(
     77         self::EMERGENCY => 'EMERGENCY',
     78         self::ALERT     => 'ALERT',
     79         self::CRITICAL  => 'CRITICAL',
     80         self::ERROR     => 'ERROR',
     81         self::WARN      => 'WARNING',
     82         self::NOTICE    => 'NOTICE',
     83         self::INFO      => 'INFO',
     84         self::DEBUG     => 'DEBUG'
     85     );
     86 
     87     /**
     88      * @var mixed
     89      */
     90     protected $writer;
     91 
     92     /**
     93      * @var bool
     94      */
     95     protected $enabled;
     96 
     97     /**
     98      * @var int
     99      */
    100     protected $level;
    101 
    102     /**
    103      * Constructor
    104      * @param  mixed $writer
    105      */
    106     public function __construct($writer)
    107     {
    108         $this->writer = $writer;
    109         $this->enabled = true;
    110         $this->level = self::DEBUG;
    111     }
    112 
    113     /**
    114      * Is logging enabled?
    115      * @return bool
    116      */
    117     public function getEnabled()
    118     {
    119         return $this->enabled;
    120     }
    121 
    122     /**
    123      * Enable or disable logging
    124      * @param  bool $enabled
    125      */
    126     public function setEnabled($enabled)
    127     {
    128         if ($enabled) {
    129             $this->enabled = true;
    130         } else {
    131             $this->enabled = false;
    132         }
    133     }
    134 
    135     /**
    136      * Set level
    137      * @param  int                          $level
    138      * @throws \InvalidArgumentException    If invalid log level specified
    139      */
    140     public function setLevel($level)
    141     {
    142         if (!isset(self::$levels[$level])) {
    143             throw new \InvalidArgumentException('Invalid log level');
    144         }
    145         $this->level = $level;
    146     }
    147 
    148     /**
    149      * Get level
    150      * @return int
    151      */
    152     public function getLevel()
    153     {
    154         return $this->level;
    155     }
    156 
    157     /**
    158      * Set writer
    159      * @param  mixed $writer
    160      */
    161     public function setWriter($writer)
    162     {
    163         $this->writer = $writer;
    164     }
    165 
    166     /**
    167      * Get writer
    168      * @return mixed
    169      */
    170     public function getWriter()
    171     {
    172         return $this->writer;
    173     }
    174 
    175     /**
    176      * Is logging enabled?
    177      * @return bool
    178      */
    179     public function isEnabled()
    180     {
    181         return $this->enabled;
    182     }
    183 
    184     /**
    185      * Log debug message
    186      * @param  mixed       $object
    187      * @param  array       $context
    188      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    189      */
    190     public function debug($object, $context = array())
    191     {
    192         return $this->log(self::DEBUG, $object, $context);
    193     }
    194 
    195     /**
    196      * Log info message
    197      * @param  mixed       $object
    198      * @param  array       $context
    199      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    200      */
    201     public function info($object, $context = array())
    202     {
    203         return $this->log(self::INFO, $object, $context);
    204     }
    205 
    206     /**
    207      * Log notice message
    208      * @param  mixed       $object
    209      * @param  array       $context
    210      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    211      */
    212     public function notice($object, $context = array())
    213     {
    214         return $this->log(self::NOTICE, $object, $context);
    215     }
    216 
    217     /**
    218      * Log warning message
    219      * @param  mixed       $object
    220      * @param  array       $context
    221      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    222      */
    223     public function warning($object, $context = array())
    224     {
    225         return $this->log(self::WARN, $object, $context);
    226     }
    227 
    228     /**
    229      * DEPRECATED for function warning
    230      * Log warning message
    231      * @param  mixed       $object
    232      * @param  array       $context
    233      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    234      */
    235     public function warn($object, $context = array())
    236     {
    237         return $this->log(self::WARN, $object, $context);
    238     }
    239 
    240     /**
    241      * Log error message
    242      * @param  mixed       $object
    243      * @param  array       $context
    244      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    245      */
    246     public function error($object, $context = array())
    247     {
    248         return $this->log(self::ERROR, $object, $context);
    249     }
    250 
    251     /**
    252      * Log critical message
    253      * @param  mixed       $object
    254      * @param  array       $context
    255      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    256      */
    257     public function critical($object, $context = array())
    258     {
    259         return $this->log(self::CRITICAL, $object, $context);
    260     }
    261 
    262     /**
    263      * DEPRECATED for function critical
    264      * Log fatal message
    265      * @param  mixed       $object
    266      * @param  array       $context
    267      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    268      */
    269     public function fatal($object, $context = array())
    270     {
    271         return $this->log(self::CRITICAL, $object, $context);
    272     }
    273 
    274     /**
    275      * Log alert message
    276      * @param  mixed       $object
    277      * @param  array       $context
    278      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    279      */
    280     public function alert($object, $context = array())
    281     {
    282         return $this->log(self::ALERT, $object, $context);
    283     }
    284 
    285     /**
    286      * Log emergency message
    287      * @param  mixed       $object
    288      * @param  array       $context
    289      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    290      */
    291     public function emergency($object, $context = array())
    292     {
    293         return $this->log(self::EMERGENCY, $object, $context);
    294     }
    295 
    296     /**
    297      * Log message
    298      * @param  mixed       $level
    299      * @param  mixed       $object
    300      * @param  array       $context
    301      * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
    302      * @throws \InvalidArgumentException If invalid log level
    303      */
    304     public function log($level, $object, $context = array())
    305     {
    306         if (!isset(self::$levels[$level])) {
    307             throw new \InvalidArgumentException('Invalid log level supplied to function');
    308         } else if ($this->enabled && $this->writer && $level <= $this->level) {
    309             $message = (string)$object;
    310             if (count($context) > 0) {
    311                 if (isset($context['exception']) && $context['exception'] instanceof \Exception) {
    312                     $message .= ' - ' . $context['exception'];
    313                     unset($context['exception']);
    314                 }
    315                 $message = $this->interpolate($message, $context);
    316             }
    317             return $this->writer->write($message, $level);
    318         } else {
    319             return false;
    320         }
    321     }
    322 
    323     /**
    324      * DEPRECATED for function log
    325      * Log message
    326      * @param   mixed    $object The object to log
    327      * @param   int      $level  The message level
    328      * @return  int|bool
    329      */
    330     protected function write($object, $level)
    331     {
    332         return $this->log($level, $object);
    333     }
    334 
    335     /**
    336      * Interpolate log message
    337      * @param  mixed     $message               The log message
    338      * @param  array     $context               An array of placeholder values
    339      * @return string    The processed string
    340      */
    341     protected function interpolate($message, $context = array())
    342     {
    343         $replace = array();
    344         foreach ($context as $key => $value) {
    345             $replace['{' . $key . '}'] = $value;
    346         }
    347         return strtr($message, $replace);
    348     }
    349 }