mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-06-14 18:52:07 -04:00
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 <objecttothis@gmail.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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/`.
|
||||
|
||||
Reference in New Issue
Block a user