Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncated JsonFormatter logs at 8191 characters #1890

Closed
Mad4T opened this issue May 21, 2024 · 5 comments
Closed

Truncated JsonFormatter logs at 8191 characters #1890

Mad4T opened this issue May 21, 2024 · 5 comments
Labels

Comments

@Mad4T
Copy link

Mad4T commented May 21, 2024

version
Monolog version: 3.0
Laravel: 10.0

Issue Description:
We're encountering log truncation at 8191 characters within our Laravel application deployed on AWS ECS. CloudWatch logs from this specific app are affected, while logs from other apps function normally.
We suspect a potential limitation within Monolog (version 3.0) might be causing this behavior.

Expected Behavior:
We expect CloudWatch logs from our Laravel app to be captured in their entirety without truncation.

Code Snippet (relevant part of logger.php):
'stderr' => [ 'driver' => 'monolog', 'handler' => StreamHandler::class, 'formatter' => env('LOG_STDERR_FORMATTER'), 'with' => [ 'stream' => 'php://stderr', ], ]

@Mad4T Mad4T added the Support label May 21, 2024
@Seldaek
Copy link
Owner

Seldaek commented Jun 28, 2024

AFAIK there is no such limit in Monolog itself, but I am not very familiar with ECS and there might be something else there.

@Mad4T
Copy link
Author

Mad4T commented Jun 30, 2024

Issue happens in ECS and EKS. Also worth noting that logs from none php apps are not truncated.

@Seldaek
Copy link
Owner

Seldaek commented Jul 6, 2024

Yeah sorry but that's something you'll have to debug yourself I am afraid.. There is too much environment specific stuff at play here for me to help much.

@Mad4T Mad4T closed this as completed Jul 11, 2024
@Seldaek
Copy link
Owner

Seldaek commented Jul 11, 2024

If you figure it out please share here, it may help someone else one day

@Mad4T
Copy link
Author

Mad4T commented Jul 14, 2024

if we ever find the reason will post it here, but we ended up doing a workaround to truncate the logs using Processor :

<?php

namespace App\Logging;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Throwable;

class TruncateStackTraceProcessor implements ProcessorInterface
{
    private int $traceLimit;

    public function __construct(int $traceLimit = 30)
    {
        $this->traceLimit = $traceLimit;
    }

    public function __invoke(LogRecord $record): LogRecord
    {
        $context = $record->context;
        if (isset($context['exception']) && $context['exception'] instanceof Throwable) {
            $exception = $record['context']['exception'];
            $trace = $exception->getTrace();

            if (count($trace) > $this->traceLimit) {
                $trace = array_slice($trace, 0, $this->traceLimit);
            }

            $context['exception'] = [
                'class' => get_class($record['context']['exception']),
                'message' => $record['context']['exception']->getMessage(),
                'code' => $record['context']['exception']->getCode(),
                'file' => $record['context']['exception']->getFile() . ':' . $record['context']['exception']->getLine(),
                'trace' => $this->formatStacktrace($trace),
            ];

            $record = $record->with(context: $context);
        }

        return $record;
    }

    private function formatStacktrace(array $trace): string {
        return collect($trace)
            ->map(function ($el) {
                $file = $el['file'] ?? '[unknown]';
                $line = $el['line'] ?? '[unknown]';
                $class = $el['class'] ?? '[unknown]';
                $function = $el['function'] ?? '[unknown]';
                $type = $el['type'] ?? '->';
                return "at {$class}{$type}{$function} ({$file}:{$line})";
            })
            ->join("\n");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants