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 all 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' => null, //Which channel should be used?
'enabled' => false,
'level' => 'debug',
],
Expand Down Expand Up @@ -104,6 +105,7 @@ public function getCacheableProperties(): array {
'prefix' => 'cacheable',
'identifier' => 'id',
'logging' => [
'channel' => 'anotherChannel',
'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' => null, //Which channel should be used?
'enabled' => false,
'level' => 'debug',
],
Expand Down
27 changes: 20 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,13 +39,18 @@ class CacheableQueryBuilder extends Builder
*/
protected ?string $prefix;

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

/**
* @var bool
*/
protected bool $logEnabled = false;

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

Expand Down Expand Up @@ -81,8 +86,16 @@ 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);

if ($this->logChannel == null) {
$this->logChannel = Log::getDefaultDriver();
} elseif (! in_array($this->logChannel, array_keys(config('logging.channels')))) {
Log::log('error', "[Cacheable] Log channel '{$this->logChannel}' does not exist, using default driver instead.");
$this->logChannel = Log::getDefaultDriver();
}
}

/**
Expand Down Expand Up @@ -278,16 +291,16 @@ protected function getModelCacheKey(string $modelClass = null): string
* @param string $level
* @return bool
*/
protected function log(string $message, string $level = 'debug')
protected function log(string $message, string $level = 'debug'): bool
{
if (! $this->logEnabled) {
return false;
}

if ($this->logLevel) {
Log::log($this->logLevel, "[Cacheable] {$message}");
Log::channel($this->logChannel)->log($this->logLevel, "[Cacheable] {$message}");
} else {
Log::log($level, "[Cacheable] {$message}");
Log::channel($this->logChannel)->log($level, "[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', null),
'enabled' => config('cacheable.logging.enabled', false),
'level' => config('cacheable.logging.level', 'debug', ),
],
Expand Down