Skip to content

add "__set_state" to Handlers to allow laravel config cache #1965

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

Open
Curicows opened this issue Apr 22, 2025 · 0 comments
Open

add "__set_state" to Handlers to allow laravel config cache #1965

Curicows opened this issue Apr 22, 2025 · 0 comments
Labels

Comments

@Curicows
Copy link

I have a project that uses the Laravel framework. I'm using the TelegramBotHandler to handle logs.

"telegram" => [
    'driver'  => 'monolog',
    'handler' => FilterHandler::class,
    'tap' => [TelegramFormatter::class],
    'level' => "debug",
    'with' => [
        'handler' => new TelegramBotHandler(
            apiKey: env('TELEGRAM_API_KEY', ""),
            channel: env('TELEGRAM_CHANNEL', ""),
            level: Level::Debug,
            parseMode: "MarkdownV2"
        )
    ]
],

While it works perfect in the local environment, when I try to cache the Laravel config an error is thrown:

laravel@ubuntu:/path/to/project$ artisan optimize

   INFO  Caching framework bootstrap, configuration, and metadata.  

  config .............................................................................................................................. 76.17ms FAIL
[2025-04-22 17:28:06] development.ERROR: Your configuration files are not serializable. {"exception":"[object] (LogicException(code: 0): Your configuration files are not serializable. at /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php:72)
[stacktrace]
#0 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\ConfigCacheCommand->handle()
#1 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::{closure:Illuminate\\Container\\BoundMethod::call():35}()
#2 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(96): Illuminate\\Container\\Util::unwrapIfClosure()
#3 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#4 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(754): Illuminate\\Container\\BoundMethod::call()
#5 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\\Container\\Container->call()
#6 /path/to/project/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#7 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\\Component\\Console\\Command\\Command->run()
#8 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(68): Illuminate\\Console\\Command->run()
#9 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(41): Illuminate\\Console\\Command->runCommand()
#10 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(53): Illuminate\\Console\\Command->callSilent()
#11 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(48): Illuminate\\Console\\Command->callSilently()
#12 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php(41): Illuminate\\Foundation\\Console\\OptimizeCommand->{closure:Illuminate\\Foundation\\Console\\OptimizeCommand::handle():48}()
#13 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/View/Components/Factory.php(59): Illuminate\\Console\\View\\Components\\Task->render()
#14 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(48): Illuminate\\Console\\View\\Components\\Factory->__call()
#15 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\OptimizeCommand->handle()
#16 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::{closure:Illuminate\\Container\\BoundMethod::call():35}()
#17 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(96): Illuminate\\Container\\Util::unwrapIfClosure()
#18 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#19 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(754): Illuminate\\Container\\BoundMethod::call()
#20 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\\Container\\Container->call()
#21 /path/to/project/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#22 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\\Component\\Console\\Command\\Command->run()
#23 /path/to/project/vendor/symfony/console/Application.php(1094): Illuminate\\Console\\Command->run()
#24 /path/to/project/vendor/symfony/console/Application.php(342): Symfony\\Component\\Console\\Application->doRunCommand()
#25 /path/to/project/vendor/symfony/console/Application.php(193): Symfony\\Component\\Console\\Application->doRun()
#26 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run()
#27 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1234): Illuminate\\Foundation\\Console\\Kernel->handle()
#28 /path/to/project/artisan(13): Illuminate\\Foundation\\Application->handleCommand()
#29 {main}

[previous exception] [object] (Error(code: 0): Call to undefined method Monolog\\Handler\\TelegramBotHandler::__set_state() at /path/to/project/bootstrap/cache/config.php:902)
[stacktrace]
#0 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php(68): require()
#1 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\ConfigCacheCommand->handle()
#2 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::{closure:Illuminate\\Container\\BoundMethod::call():35}()
#3 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(96): Illuminate\\Container\\Util::unwrapIfClosure()
#4 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#5 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(754): Illuminate\\Container\\BoundMethod::call()
#6 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\\Container\\Container->call()
#7 /path/to/project/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#8 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\\Component\\Console\\Command\\Command->run()
#9 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(68): Illuminate\\Console\\Command->run()
#10 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(41): Illuminate\\Console\\Command->runCommand()
#11 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(53): Illuminate\\Console\\Command->callSilent()
#12 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(48): Illuminate\\Console\\Command->callSilently()
#13 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php(41): Illuminate\\Foundation\\Console\\OptimizeCommand->{closure:Illuminate\\Foundation\\Console\\OptimizeCommand::handle():48}()
#14 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/View/Components/Factory.php(59): Illuminate\\Console\\View\\Components\\Task->render()
#15 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(48): Illuminate\\Console\\View\\Components\\Factory->__call()
#16 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\OptimizeCommand->handle()
#17 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::{closure:Illuminate\\Container\\BoundMethod::call():35}()
#18 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(96): Illuminate\\Container\\Util::unwrapIfClosure()
#19 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#20 /path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(754): Illuminate\\Container\\BoundMethod::call()
#21 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\\Container\\Container->call()
#22 /path/to/project/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#23 /path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\\Component\\Console\\Command\\Command->run()
#24 /path/to/project/vendor/symfony/console/Application.php(1094): Illuminate\\Console\\Command->run()
#25 /path/to/project/vendor/symfony/console/Application.php(342): Symfony\\Component\\Console\\Application->doRunCommand()
#26 /path/to/project/vendor/symfony/console/Application.php(193): Symfony\\Component\\Console\\Application->doRun()
#27 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run()
#28 /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1234): Illuminate\\Foundation\\Console\\Kernel->handle()
#29 /path/to/project/artisan(13): Illuminate\\Foundation\\Application->handleCommand()
#30 {main}
"} 

   LogicException 

  Your configuration files are not serializable.

  at vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php:72
     68▕             require $configPath;
     69▕         } catch (Throwable $e) {
     70▕             $this->files->delete($configPath);
     71▕ 
  ➜  72▕             throw new LogicException('Your configuration files are not serializable.', 0, $e);
     73▕         }
     74▕ 
     75▕         $this->components->info('Configuration cached successfully.');
     76▕     }

  1   bootstrap/cache/config.php:902
      Error::("Call to undefined method Monolog\Handler\TelegramBotHandler::__set_state()")
      +29 vendor frames 

  31  artisan:13
      Illuminate\Foundation\Application::handleCommand()

Inspecting the generated file before the "LogicException" has been thrown resulted in this cache:

'telegram' => 
      array (
        'driver' => 'monolog',
        'handler' => 'Monolog\\Handler\\FilterHandler',
        'tap' => 
        array (
          0 => 'App\\Log\\TelegramFormatter',
        ),
        'level' => 'debug',
        'with' => 
        array (
          'handler' => 
          \Monolog\Handler\TelegramBotHandler::__set_state(array(
             'level' => 
            \Monolog\Level::Debug,
             'bubble' => true,
             'processors' => 
            array (
            ),
             'formatter' => NULL,
             'apiKey' => 'redacted',
             'channel' => '@redacted',
             'parseMode' => 'MarkdownV2',
             'disableWebPagePreview' => NULL,
             'disableNotification' => NULL,
             'splitLongMessages' => false,
             'delayBetweenMessages' => false,
             'topic' => NULL,
          )),
        ),
      ),

The error occurs because when Laravel cache the config that directly instantiates an object it is transformed to call the static magic method "__set_state" of the class used. But an error is thrown because the Handler classes doesn't have this magic method.

I'm able to create a pull request to resolve that and if more information is needed I can provide it, thanks for the attention.

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

1 participant