web/lib/Zend/Log/Formatter/Simple.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Log
       
    17  * @subpackage Formatter
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Simple.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /** Zend_Log_Formatter_Interface */
       
    24 require_once 'Zend/Log/Formatter/Interface.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Log
       
    29  * @subpackage Formatter
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  * @version    $Id: Simple.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    33  */
       
    34 class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
       
    35 {
       
    36     /**
       
    37      * @var string
       
    38      */
       
    39     protected $_format;
       
    40 
       
    41     const DEFAULT_FORMAT = '%timestamp% %priorityName% (%priority%): %message%';
       
    42 
       
    43     /**
       
    44      * Class constructor
       
    45      *
       
    46      * @param  null|string  $format  Format specifier for log messages
       
    47      * @throws Zend_Log_Exception
       
    48      */
       
    49     public function __construct($format = null)
       
    50     {
       
    51         if ($format === null) {
       
    52             $format = self::DEFAULT_FORMAT . PHP_EOL;
       
    53         }
       
    54 
       
    55         if (! is_string($format)) {
       
    56             require_once 'Zend/Log/Exception.php';
       
    57             throw new Zend_Log_Exception('Format must be a string');
       
    58         }
       
    59 
       
    60         $this->_format = $format;
       
    61     }
       
    62 
       
    63     /**
       
    64      * Formats data into a single line to be written by the writer.
       
    65      *
       
    66      * @param  array    $event    event data
       
    67      * @return string             formatted line to write to the log
       
    68      */
       
    69     public function format($event)
       
    70     {
       
    71         $output = $this->_format;
       
    72         foreach ($event as $name => $value) {
       
    73 
       
    74             if ((is_object($value) && !method_exists($value,'__toString'))
       
    75                 || is_array($value)) {
       
    76 
       
    77                 $value = gettype($value);
       
    78             }
       
    79 
       
    80             $output = str_replace("%$name%", $value, $output);
       
    81         }
       
    82         return $output;
       
    83     }
       
    84 
       
    85 }