Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Created June 4, 2024 13:30
Show Gist options
  • Save DominikStyp/c7ec46d87f2903f1d864ac887a11be56 to your computer and use it in GitHub Desktop.
Save DominikStyp/c7ec46d87f2903f1d864ac887a11be56 to your computer and use it in GitHub Desktop.
Laravel: How to change/swap container instance behind the Facade for example Log
<?php
namespace App\Providers;
use App\Patches\LogManager;
use Illuminate\Log\LogServiceProvider;
use Illuminate\Support\Facades\Log;
class CustomLogServiceProvider extends LogServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$instance = new LogManager($this->app);
// this is crutial since Illuminate\Support\Facades\Facade caches the instance in the static variable $resolvedInstance
Log::swap($instance);
// this will make sure that app('log') is also resolved correctly
$this->app->extend('log', function ($app) use ($instance) {
return $instance;
});
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment