BRACKETS-by-TRIAD / craftable

Admin panel builder / CRUD generator for Laravel.
https://getcraftable.com
MIT License
984 stars 191 forks source link

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

Open chillnet opened 11 months ago

chillnet commented 11 months ago

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;
        }
    }
}
palypster commented 11 months ago

Yes, PR would be perfect

stale[bot] commented 6 months ago

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.