0

Coming from Yii2 at the end of each request when something is logged, Yii2 adds additional data to your log, for example the $_POST data so you know what paramaters caused the issue. Is there a way to add these information in Monolog too?

I don't want to use a Processor as it includes all those parameters to each and every record. I would just like to add an additional string in case something is logged at the end of the log after all messages are included/send (for example via BufferHandler when sending Mails via SwiftMailerHandler)

Robin Schambach
  • 617
  • 3
  • 11
  • 26

1 Answers1

0

I found a way.. I can extend the default HtmlFormatter or LineFormatter and include my custom information that way

namespace modules\myspa\monolog;

class HtmlFormatter extends \Monolog\Formatter\HtmlFormatter
{
    public function formatBatch(array $records): string
    {
        $formatBatch = parent::formatBatch($records);
        $formatBatch .= '<br><br>Foobar.. add some custom info';
        
        return $formatBatch;
    }
}

Then I can add this one as a formatter

$swiftMailHandler = new SwiftMailerHandler($swiftMailer, $message, \Monolog\Logger::ERROR);

// add my formatter
$swiftMailHandler->setFormatter(new HtmlFormatter());

$psrLogger->pushHandler(new BufferHandler($swiftMailHandler));
Robin Schambach
  • 617
  • 3
  • 11
  • 26