Skip to content
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

Add support for dynamic log channel #37

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ This is the contents of the published config file:
'identifier' => 'id',
//Do you need logging?
'logging' => [
'channel' => 'default', //Which channel should be used?
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
'enabled' => false,
'level' => 'debug',
],
Expand Down Expand Up @@ -104,6 +105,7 @@ public function getCacheableProperties(): array {
'prefix' => 'cacheable',
'identifier' => 'id',
'logging' => [
'channel' => 'default',
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
'enabled' => false,
'level' => 'debug',
],
Expand Down
1 change: 1 addition & 0 deletions config/cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'identifier' => 'id',
//Do you need logging?
'logging' => [
'channel' => 'default', //Which channel should be used?
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
'enabled' => false,
'level' => 'debug',
],
Expand Down
33 changes: 26 additions & 7 deletions src/Database/Query/CacheableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace ElipZis\Cacheable\Database\Query;

use Illuminate\Support\Arr;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Connection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

/**
* Capture all select queries and decide if to cache or not.
Expand Down Expand Up @@ -39,6 +39,11 @@ class CacheableQueryBuilder extends Builder
*/
protected ?string $prefix;

/**
* @var string|null
*/
protected ?string $logChannel;

/**
* @var bool
*/
Expand Down Expand Up @@ -81,6 +86,7 @@ public function __construct(
$this->modelIdentifier = $cacheableProperties['identifier'] ?? null;
$this->ttl = $cacheableProperties['ttl'] ?? null;
$this->prefix = $cacheableProperties['prefix'] ?? null;
$this->logChannel = Arr::get($cacheableProperties, 'logging.channel', null);
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
$this->logEnabled = Arr::get($cacheableProperties, 'logging.enabled', false);
$this->logLevel = Arr::get($cacheableProperties, 'logging.level', null);
}
Expand Down Expand Up @@ -284,12 +290,25 @@ protected function log(string $message, string $level = 'debug')
return false;
}

if ($this->logLevel) {
Log::log($this->logLevel, "[Cacheable] {$message}");
} else {
Log::log($level, "[Cacheable] {$message}");
// Handle dynamic log level and log channel, so we can change it on the fly
// for example, if log channel is set to 'some_channel', it will log to 'some_channel' channel
// if log channel is set to 'default', it will log to the default channel
if ($this->logChannel == null || $this->logChannel == 'default') {
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
$this->logChannel = Log::getDefaultDriver();
}

// Check if the log channel exists on the driver list
if (! in_array($this->logChannel, array_keys(config('logging.channels')))) {
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
Log::log('error', "[Cacheable] Log channel '{$this->logChannel}' does not exist, using default driver instead.");
$this->logChannel = Log::getDefaultDriver();
}

// Check if log level is set, if not, use the default level
$this->logLevel = $this->logLevel ?? $level;

// Log it to the channel
Log::channel($this->logChannel)->log($this->logLevel, "[Cacheable] {$message}");

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/Models/Traits/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function getCacheableProperties(): array
'prefix' => config('cacheable.prefix', 'cacheable'),
'identifier' => config('cacheable.identifier', 'id'),
'logging' => [
'channel' => config('cacheable.logging.channel', 'default'),
Sleepy4k marked this conversation as resolved.
Show resolved Hide resolved
'enabled' => config('cacheable.logging.enabled', false),
'level' => config('cacheable.logging.level', 'debug', ),
],
Expand Down