mirror of
				https://github.com/Seldaek/monolog.git
				synced 2025-10-27 02:21:43 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			102 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| /*
 | |
|  * This file is part of the Monolog package.
 | |
|  *
 | |
|  * (c) Jordi Boggiano <j.boggiano@seld.be>
 | |
|  *
 | |
|  * For the full copyright and license information, please view the LICENSE
 | |
|  * file that was distributed with this source code.
 | |
|  */
 | |
| 
 | |
| namespace Monolog\Formatter;
 | |
| 
 | |
| use Exception;
 | |
| 
 | |
| /**
 | |
|  * Formats incoming records into a one-line string
 | |
|  *
 | |
|  * This is especially useful for logging to files
 | |
|  *
 | |
|  * @author Jordi Boggiano <j.boggiano@seld.be>
 | |
|  * @author Christophe Coevoet <stof@notk.org>
 | |
|  */
 | |
| class LineFormatter extends NormalizerFormatter
 | |
| {
 | |
|     const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
 | |
| 
 | |
|     protected $format;
 | |
| 
 | |
|     /**
 | |
|      * @param string $format     The format of the message
 | |
|      * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
 | |
|      */
 | |
|     public function __construct($format = null, $dateFormat = null)
 | |
|     {
 | |
|         $this->format = $format ?: static::SIMPLE_FORMAT;
 | |
|         parent::__construct($dateFormat);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * {@inheritdoc}
 | |
|      */
 | |
|     public function format(array $record)
 | |
|     {
 | |
|         $vars = parent::format($record);
 | |
| 
 | |
|         $output = $this->format;
 | |
|         foreach ($vars['extra'] as $var => $val) {
 | |
|             if (false !== strpos($output, '%extra.'.$var.'%')) {
 | |
|                 $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output);
 | |
|                 unset($vars['extra'][$var]);
 | |
|             }
 | |
|         }
 | |
|         foreach ($vars as $var => $val) {
 | |
|             if (false !== strpos($output, '%'.$var.'%')) {
 | |
|                 $output = str_replace('%'.$var.'%', $this->convertToString($val), $output);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return $output;
 | |
|     }
 | |
| 
 | |
|     public function formatBatch(array $records)
 | |
|     {
 | |
|         $message = '';
 | |
|         foreach ($records as $record) {
 | |
|             $message .= $this->format($record);
 | |
|         }
 | |
| 
 | |
|         return $message;
 | |
|     }
 | |
| 
 | |
|     protected function normalizeException(Exception $e)
 | |
|     {
 | |
|         $previousText = '';
 | |
|         if ($previous = $e->getPrevious()) {
 | |
|             do {
 | |
|                 $previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
 | |
|             } while ($previous = $previous->getPrevious());
 | |
|         }
 | |
| 
 | |
|         return '[object] ('.get_class($e).': '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
 | |
|     }
 | |
| 
 | |
|     protected function convertToString($data)
 | |
|     {
 | |
|         if (null === $data || is_bool($data)) {
 | |
|             return var_export($data, true);
 | |
|         }
 | |
| 
 | |
|         if (is_scalar($data)) {
 | |
|             return (string) $data;
 | |
|         }
 | |
| 
 | |
|         if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
 | |
|             return $this->toJson($data, true);
 | |
|         }
 | |
| 
 | |
|         return str_replace('\\/', '/', @json_encode($data));
 | |
|     }
 | |
| }
 |