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

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

Open
chillnet opened this issue Dec 2, 2023 · 2 comments
Labels

Comments

@chillnet
Copy link

chillnet commented Dec 2, 2023

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
Copy link
Contributor

Yes, PR would be perfect

Copy link

stale bot commented Apr 22, 2024

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 stale bot added the stale label Apr 22, 2024
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