From 7dde48c0edf47ee31154b4889b1ab44fb36258f5 Mon Sep 17 00:00:00 2001 From: objec Date: Fri, 12 Jun 2026 00:57:35 +0400 Subject: [PATCH] Plugin logger - Create log_plugin_message() to prevent plugin logs from spamming the core logs - Create ability to log to different logs or a base log if parameter is not specified. - Update README.md - Change BasePlugin::log() wrapper function to log to log_plugin_message() and add logTo() to log to a specific plugin log. - Add plugin logger service to keep the logger loaded. Signed-off-by: objec --- app/Config/Services.php | 10 ++++++++++ app/Helpers/plugin_helper.php | 7 +++++++ app/Libraries/Plugins/BasePlugin.php | 7 ++++++- app/Plugins/README.md | 27 ++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/app/Config/Services.php b/app/Config/Services.php index dd06674aa..67730e15b 100644 --- a/app/Config/Services.php +++ b/app/Config/Services.php @@ -3,6 +3,7 @@ namespace Config; use App\Libraries\MY_Language; +use App\Libraries\Plugins\PluginLogger; use App\Libraries\Plugins\PluginManager; use Locale; use HTMLPurifier; @@ -62,6 +63,15 @@ class Services extends BaseService return new MY_Language($locale); } + public static function pluginLogger(bool $getShared = true): PluginLogger + { + if ($getShared) { + return static::getSharedInstance('pluginLogger'); + } + + return new PluginLogger(); + } + public static function pluginManager(bool $getShared = true): PluginManager { if ($getShared) { diff --git a/app/Helpers/plugin_helper.php b/app/Helpers/plugin_helper.php index 5ea9c4232..ef3033c5e 100644 --- a/app/Helpers/plugin_helper.php +++ b/app/Helpers/plugin_helper.php @@ -2,6 +2,13 @@ use CodeIgniter\Events\Events; +if (!function_exists('log_plugin_message')) { + function log_plugin_message(string $pluginId, string $level, string $message, ?string $logName = null): void + { + service('pluginLogger')->log($pluginId, $level, $message, $logName); + } +} + if (!function_exists('pluginContent')) { function pluginContent(string $section, array $data = []): string { diff --git a/app/Libraries/Plugins/BasePlugin.php b/app/Libraries/Plugins/BasePlugin.php index 4acd7b064..4b32d51a1 100644 --- a/app/Libraries/Plugins/BasePlugin.php +++ b/app/Libraries/Plugins/BasePlugin.php @@ -68,7 +68,12 @@ abstract class BasePlugin implements PluginInterface protected function log(string $level, string $message): void { - log_message($level, "[Plugin:{$this->getPluginName()}] {$message}"); + log_plugin_message($this->getPluginId(), $level, $message); + } + + protected function logTo(string $logName, string $level, string $message): void + { + log_plugin_message($this->getPluginId(), $level, $message, $logName); } protected function renderView(string $viewName, array $data = []): string diff --git a/app/Plugins/README.md b/app/Plugins/README.md index 468028b66..42cd416a7 100644 --- a/app/Plugins/README.md +++ b/app/Plugins/README.md @@ -68,7 +68,8 @@ class MyPlugin extends BasePlugin |--------|-----------|-------------| | `getSetting` | `(string $key, mixed $default = null): mixed` | Read one plugin setting | | `setSetting` | `(string $key, mixed $value): bool` | Write one plugin setting | -| `log` | `(string $level, string $message): void` | Write to CI4 log with plugin prefix | +| `log` | `(string $level, string $message): void` | Write to plugin-specific log file (`writable/logs/plugin-{id}-{date}.log`) | +| `logTo` | `(string $logName, string $level, string $message): void` | Write to a named plugin log file (`writable/logs/plugin-{id}-{logName}-{date}.log`) | | `renderView` | `(string $viewName, array $data = []): string` | Render a view from the plugin's own `Views/` directory | #### `renderView()` @@ -543,14 +544,30 @@ added as a feature. Specify the event, where it should be triggered, and what da If a plugin event trigger exists but the data you need is not passed to the callback, please open an issue in the opensourcepos repository requesting the data to be added as a feature. -## Testing +## Logging -Enable plugin logging to debug: +Plugins have two logging paths: + +**Standard CI4 log** — use `log_message()` directly; goes to `writable/logs/log-{date}.log` only: ```php log_message('debug', 'Debug message'); -log_message('info', 'Info message'); -log_message('error', 'Error message'); +``` + +**Plugin-specific log** — goes to a dedicated plugin file in `writable/logs/`, NOT the CI4 log: + +```php +// From inside a plugin class (pluginId inferred automatically): +$this->log('debug', 'Debug message'); +// → writable/logs/plugin-{id}-{date}.log + +// Named log channel (useful for separating API calls, sync events, etc.): +$this->logTo('api', 'debug', 'API response received'); +// → writable/logs/plugin-{id}-api-{date}.log + +// From library classes inside a plugin (pass pluginId explicitly): +log_plugin_message('myplugin', 'debug', 'Sync started'); +log_plugin_message('myplugin', 'debug', 'API call', 'api'); ``` Check logs in `writable/logs/`.