marcio
marcio

Reputation: 10512

How to disable laravel logs temporarily?

I felt into a log recursion problem and I would like to disable logs only for a single operation. Is there any kind of runtime switch for Laravel logs? Like:

<?php
Log::stop();
// bypass log here
Log::resume();

Unfortunately, there is no way to do that through the facade like on example above. What's the best way to turn Laravel log off temporarily?

Upvotes: 3

Views: 3778

Answers (2)

marcio
marcio

Reputation: 10512

Another possible (less general) solution in case you are registering custom listeners is to use Config facade to store state and bypass a given callback:

<?php

Event::listen('illuminate.query', function($query, $bindings, $time, $name){
    if(Config::get('log.enabled', true)){
        Log::info($query, ['context' => ['bindings' => $bindings]]);
    }
});

Log::listen(function($level, $message, $context) {
    Config::set('log.enabled', false);
    // do something bypassing listener who expects log.enabled 
    Config::set('log.enabled', true);
});

Upvotes: 0

Teaqu
Teaqu

Reputation: 3263

Here is a hacky way to do it. Swap the facade instance with a mock.

With Mockery:

$app = Log::getFacadeRoot();
Log::shouldReceive('error', 'warning', 'debug', 'info', 'notice', 'critical', 'alert', 'emergency');

// bypass log here

Log::swap($app);

Without Mockery:

class LogMock {
    public function __call($name, $arguments) {}
}

$app = Log::getFacadeRoot();
Log::swap(new LogMock);

// bypass log here

Log::swap($app);

Upvotes: 4

Related Questions