Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robopzet's avatar

Laravel will not use my custom log class

To make the Log facade methods accept a custom message class, which includes a message ID, I created a custom log class. It extends the Log facade. Calling Logger::error() works, but I want to keep using the normal Log::error() call.

I cannot get Laravel to use this logger. In a service provider I tried to register the logger in several ways in the register() and boot() methods:

// Use the service alias.
AliasLoader::getInstance()->alias('log', Logger::class);

// Uppercase first letter.
AliasLoader::getInstance()->alias('Log', Logger::class);

// Put it in the container, using the name that Laravel puts it under.
app()->bind(\Illuminate\Log\LogManager::class, Logger::class);

With every option I tried calls to Log::error() is still handled by the default logger, not my implementation.

Does anyone know to get Laravel to use my custom logger?

0 likes
3 replies
jlrdw's avatar

I suggest stick with laravels error handler, you can still have custom messages.

puklipo's avatar

In Laravel, you usually don't customize the Facade by replacing the target class entirely.

Please read the documentation carefully. There is an easier way to achieve this.

https://laravel.com/docs/11.x/logging

  • If you simply want to add some kind of ID, you can use Context.
  • If you want to make more complex customizations, create your own drivers, formatters, handlers, etc.

There is a mechanism in place that allows you to extend it as much as you want.

Please write in your question what you really want to do.

robopzet's avatar

@puklipo every logged text is defined in an enum with its name as the unique ID. To be able to route, recognize and process logged messages, this ID must get into the logging. Log messages are serialized to JSON using Monlog's JSONFormatter.

A custom Logger class with the same static methods as the Log facade uses, also accepts the custom messages instances as input. This class formats text and puts arguments and ID in the context, then calls the normal Laravel Log facade.

What I do not like is that now the Log facade cannot be used, because processing has to go though the custom logger. Replacing the Log facade implementation with the custom one would allow everyone to keep using the known Log facade and have the custom information in the logging.

Please or to participate in this conversation.