Files
opensourcepos/app/Libraries/Plugins/PluginLogger.php
objec 962407b3f6 Review suggestion corrections
- Remove unused function
- reorder parameters in log(), logTo() and log_plugin_message()

Signed-off-by: objec <objecttothis@gmail.com>
2026-06-12 11:16:58 +04:00

71 lines
1.9 KiB
PHP

<?php
namespace App\Libraries\Plugins;
class PluginLogger
{
private string $basePath;
private string $dateFormat;
public function __construct(string $basePath = '', string $dateFormat = 'Y-m-d H:i:s')
{
$this->basePath = $basePath !== '' ? $basePath : WRITEPATH . 'logs/';
$this->dateFormat = $dateFormat;
}
public function log(string $level, string $message, string $pluginId, ?string $logName = null): void
{
$filepath = $this->basePath . $this->buildFilename($pluginId, $logName);
$this->write($filepath, $level, $message);
}
private function sanitize(string $id): string
{
return strtolower(preg_replace('/[^a-zA-Z0-9-]/', '-', $id));
}
private function buildFilename(string $pluginId, ?string $logName): string
{
$id = $this->sanitize($pluginId);
$date = date('Y-m-d');
if ($logName !== null && $logName !== '') {
return 'plugin-' . $id . '-' . $this->sanitize($logName) . '-' . $date . '.log';
}
return 'plugin-' . $id . '-' . $date . '.log';
}
private function write(string $filepath, string $level, string $message): void
{
$newFile = !is_file($filepath);
$fp = @fopen($filepath, 'ab');
if ($fp === false) {
log_message('warning', 'PluginLogger: could not open log file: ' . $filepath);
return;
}
$date = date($this->dateFormat);
$line = strtoupper($level) . ' - ' . $date . ' --> ' . $message . "\n";
flock($fp, LOCK_EX);
$result = null;
for ($written = 0, $length = strlen($line); $written < $length; $written += $result) {
$result = fwrite($fp, substr($line, $written));
if ($result === false) {
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
if ($newFile) {
chmod($filepath, 0660);
}
}
}