craftable icon indicating copy to clipboard operation
craftable copied to clipboard

Declaration of Brackets\AdvancedLogger\Formatters\LineWithHashFormatter incompatible with Monolog\Formatter\LineFormatter - Laravel 10

Open chillnet opened this issue 1 year ago • 2 comments

After upgrading Laravel from version 9 to 10, my production app encountered an error.

Declaration of Brackets\AdvancedLogger\Formatters\LineWithHashFormatter::format(array $record): string must be compatible with Monolog\Formatter\LineFormatter::format(Monolog\LogRecord $record)

I fixed the issue by overwriting the specific file in composer.json. However, others may have the same problem and would appreciate a quick solution.

composer.json:

...
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "exclude-from-classmap": [
        "vendor/brackets/advanced-logger/src/Formatters/LineWithHashFormatter.php"
    ],
    "files": [
        "app/Overrides/vendor/brackets/advanced-logger/src/Formatters/LineWithHashFormatter.php"
    ],
    "classmap": [
        "app/Models",
        "app/Settings",
        "app/Jobs"
    ]
},

The fix in the LineWithHashFormatter.php, was fairly straightforward. Would you like me to open a pull request for this change?

<?php

namespace Brackets\AdvancedLogger\Formatters;

use Brackets\AdvancedLogger\Services\Benchmark;
use Monolog\Formatter\LineFormatter;
use Monolog\LogRecord;

/**
 * Class LineWithHashFormatter
 */
class LineWithHashFormatter extends LineFormatter
{
    public const KEY = 'hash';

    /**
     * @param LogRecord $record $record
     * @return array|mixed|string|string[]|void|null
     */
    public function format(LogRecord $record): string
    {
        $output = parent::format($record);
        if (str_contains($output, '%' . self::KEY . '%')) {
            $output = str_replace(
                '%' . self::KEY . '%',
                $this->stringify($this->getRequestHash()),
                $output
            );
        }
        return $output;
    }

    /**
     * Get request hash
     *
     * @return string|null
     */
    protected function getRequestHash(): ?string
    {
        try {
            return Benchmark::hash(config('advanced-logger.request.benchmark', 'application'));
        } catch (\Exception $e) {
            return null;
        }
    }
}

chillnet avatar Dec 02 '23 15:12 chillnet

Yes, PR would be perfect

palypster avatar Dec 02 '23 17:12 palypster

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 22 '24 10:04 stale[bot]