mirror of
				https://github.com/Seldaek/monolog.git
				synced 2025-10-29 11:26:09 +01:00 
			
		
		
		
	BC Break * For Logstash, format context as object instead of individual fields * For Logstash, format extra as object instead of individual fields
		
			
				
	
	
		
			102 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php declare(strict_types=1);
 | |
| 
 | |
| /*
 | |
|  * 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;
 | |
| 
 | |
| /**
 | |
|  * Serializes a log message to Logstash Event Format
 | |
|  *
 | |
|  * @see http://logstash.net/
 | |
|  * @see https://github.com/elastic/logstash/blob/master/logstash-core-event/lib/logstash/event.rb
 | |
|  *
 | |
|  * @author Tim Mower <timothy.mower@gmail.com>
 | |
|  */
 | |
| class LogstashFormatter extends NormalizerFormatter
 | |
| {
 | |
|     /**
 | |
|      * @var string the name of the system for the Logstash log message, used to fill the @source field
 | |
|      */
 | |
|     protected $systemName;
 | |
| 
 | |
|     /**
 | |
|      * @var string an application name for the Logstash log message, used to fill the @type field
 | |
|      */
 | |
|     protected $applicationName;
 | |
| 
 | |
|     /**
 | |
|      * @var string a prefix for 'extra' fields from the Monolog record (optional)
 | |
|      */
 | |
|     protected $extraPrefix;
 | |
| 
 | |
|     /**
 | |
|      * @var string a prefix for 'context' fields from the Monolog record (optional)
 | |
|      */
 | |
|     protected $contextPrefix;
 | |
| 
 | |
|     /**
 | |
|      * @param string $applicationName the application that sends the data, used as the "type" field of logstash
 | |
|      * @param string $systemName      the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
 | |
|      * @param string $extraPrefix     prefix for extra keys inside logstash "fields"
 | |
|      * @param string $contextPrefix   prefix for context keys inside logstash "fields", defaults to ctxt_
 | |
|      */
 | |
|     public function __construct(string $applicationName, string $systemName = null, string $extraPrefix = null, string $contextPrefix = 'ctxt_')
 | |
|     {
 | |
|         // logstash requires a ISO 8601 format date with optional millisecond precision.
 | |
|         parent::__construct('Y-m-d\TH:i:s.uP');
 | |
| 
 | |
|         $this->systemName = $systemName ?: gethostname();
 | |
|         $this->applicationName = $applicationName;
 | |
|         $this->extraPrefix = $extraPrefix;
 | |
|         $this->contextPrefix = $contextPrefix;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * {@inheritdoc}
 | |
|      */
 | |
|     public function format(array $record): string
 | |
|     {
 | |
|         $record = parent::format($record);
 | |
| 
 | |
|         if (empty($record['datetime'])) {
 | |
|             $record['datetime'] = gmdate('c');
 | |
|         }
 | |
|         $message = [
 | |
|             '@timestamp' => $record['datetime'],
 | |
|             '@version' => 1,
 | |
|             'host' => $this->systemName,
 | |
|         ];
 | |
|         if (isset($record['message'])) {
 | |
|             $message['message'] = $record['message'];
 | |
|         }
 | |
|         if (isset($record['channel'])) {
 | |
|             $message['type'] = $record['channel'];
 | |
|             $message['channel'] = $record['channel'];
 | |
|         }
 | |
|         if (isset($record['level_name'])) {
 | |
|             $message['level'] = $record['level_name'];
 | |
|         }
 | |
|         if (isset($record['level'])) {
 | |
|             $message['monolog_level'] = $record['level'];
 | |
|         }
 | |
|         if ($this->applicationName) {
 | |
|             $message['type'] = $this->applicationName;
 | |
|         }
 | |
|         if (!empty($record['extra'])) {
 | |
|             $message[$this->extraPrefix.'extra'] = $record['extra'];
 | |
|         }
 | |
|         if (!empty($record['context'])) {
 | |
|             $message[$this->contextPrefix.'context'] = $record['context'];
 | |
|         }
 | |
| 
 | |
|         return $this->toJson($message) . "\n";
 | |
|     }
 | |
| }
 |