Access Underlying Monolog Instance used by Laravel

Snippet

Access the underlying Monolog instance used by Laravel with the following snippet:

$monolog = \Log::getMonolog();

Example Use Case

For example, when using the spatie/image-optimizer package, there is an option to include a logger via a class that implements Psr\Log\LoggerInterface. In the case of a Laravel project it is convenient to output this log data directly to the laravel.log file by utlizing the existing Monolog instance directly:

use Spatie\ImageOptimizer\OptimizerChainFactory;

$optimizerChain = OptimizerChainFactory::create();

$optimizerChain
   ->useLogger(\Log::getMonolog())
   ->optimize($pathToImage);

Tags

 Laravel  snippets  Monolog  Logging