1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog\Formatter; 13 14/** 15 * Encodes message information into JSON in a format compatible with Loggly. 16 * 17 * @author Adam Pancutt <adam@pancutt.com> 18 */ 19class LogglyFormatter extends JsonFormatter 20{ 21 /** 22 * Overrides the default batch mode to new lines for compatibility with the 23 * Loggly bulk API. 24 */ 25 public function __construct(int $batchMode = self::BATCH_MODE_NEWLINES, bool $appendNewline = false) 26 { 27 parent::__construct($batchMode, $appendNewline); 28 } 29 30 /** 31 * Appends the 'timestamp' parameter for indexing by Loggly. 32 * 33 * @see https://www.loggly.com/docs/automated-parsing/#json 34 * @see \Monolog\Formatter\JsonFormatter::format() 35 */ 36 public function format(array $record): string 37 { 38 if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTimeInterface)) { 39 $record["timestamp"] = $record["datetime"]->format("Y-m-d\TH:i:s.uO"); 40 unset($record["datetime"]); 41 } 42 43 return parent::format($record); 44 } 45} 46