How can the array output at the end of the log file be removed when using Monolog in PHP?

To remove the array output at the end of the log file when using Monolog in PHP, you can create a custom formatter that excludes the array data. This can be achieved by extending the LineFormatter class and overriding the format method to exclude array data from being logged.

use Monolog\Formatter\LineFormatter;

class CustomFormatter extends LineFormatter
{
    public function format(array $record)
    {
        $output = parent::format($record);
        
        // Remove array output at the end of the log file
        $output = preg_replace('/\[\d+\] \{.*\}/', '', $output);

        return $output;
    }
}

$logger = new Monolog\Logger('my_logger');
$handler = new Monolog\Handler\StreamHandler('path/to/logfile.log');
$handler->setFormatter(new CustomFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"));
$logger->pushHandler($handler);

$logger->info('This is a log message', ['key' => 'value']);