init new log writer stuff psr-4 compatible
This commit is contained in:
parent
824e1f1625
commit
ea2fbc499a
4 changed files with 223 additions and 0 deletions
141
conlite/classes/Log/Log.php
Normal file
141
conlite/classes/Log/Log.php
Normal file
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace ConLite\Log;
|
||||
|
||||
use ConLite\Exceptions\InvalidArgumentException;
|
||||
use cString;
|
||||
use ReflectionClass;
|
||||
|
||||
class Log
|
||||
{
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const EMERG = 0;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const ALERT = 1;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const CRIT = 2;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const ERR = 3;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const WARN = 4;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const NOTICE = 5;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const INFO = 6;
|
||||
|
||||
/**
|
||||
* logging level
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const DEBUG = 7;
|
||||
|
||||
protected $writer;
|
||||
|
||||
protected $shortcutHandlers = [];
|
||||
|
||||
protected $priorities = [];
|
||||
|
||||
protected $defaultPriorities = [];
|
||||
|
||||
protected $buffer = [];
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct($writer)
|
||||
{
|
||||
$createWriter = false;
|
||||
|
||||
if(!$writer) {
|
||||
$createWriter = true;
|
||||
} elseif (!is_object($writer) || !($writer instanceof LogWriter)) {
|
||||
cWarning(__FILE__, __LINE__, 'The passed class is not a subclass of ConLite LogWriter. Creating new one.');
|
||||
$createWriter = true;
|
||||
}
|
||||
|
||||
if($createWriter) {
|
||||
$options = ['destination' => \cRegistry::getConfigValue('path', 'logs') . 'conlite.log'];
|
||||
$writer = LogWriter::factory('File', $options);
|
||||
}
|
||||
|
||||
$this->setWriter($writer);
|
||||
$this->setShortcutHandler('%date', [$this, 'shDate']);
|
||||
$this->setShortcutHandler('%level', [$this, 'shLevel']);
|
||||
$this->setShortcutHandler('%message', [$this, 'shMessage']);
|
||||
|
||||
$this->getWriter()->setOption('log_format', '[%date] [%level] %message', false);
|
||||
|
||||
$reflection = new ReflectionClass($this);
|
||||
$this->priorities = $this->defaultPriorities = array_flip($reflection->getConstants());
|
||||
}
|
||||
|
||||
public function getWriter() {
|
||||
return $this->writer;
|
||||
}
|
||||
|
||||
public function setWriter(LogWriter $writer): void
|
||||
{
|
||||
$this->writer = $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function setShortcutHandler($shortcut, $handler) {
|
||||
if ($shortcut == '') {
|
||||
throw new InvalidArgumentException('The shortcut name must not be empty.');
|
||||
}
|
||||
|
||||
if (cString::getPartOfString($shortcut, 0, 1) == '%') {
|
||||
$shortcut = cString::getPartOfString($shortcut, 1);
|
||||
}
|
||||
|
||||
if (!is_callable($handler)) {
|
||||
throw new InvalidArgumentException('The specified shortcut handler does not exist.');
|
||||
}
|
||||
|
||||
if (array_key_exists($shortcut, $this->shortcutHandlers)) {
|
||||
throw new InvalidArgumentException('The shortcut ' . $shortcut . ' is already in use!');
|
||||
}
|
||||
|
||||
$this->shortcutHandlers[$shortcut] = $handler;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
66
conlite/classes/Log/LogWriter.php
Normal file
66
conlite/classes/Log/LogWriter.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace ConLite\Log;
|
||||
|
||||
use ConLite\Exceptions\InvalidArgumentException;
|
||||
|
||||
abstract class LogWriter
|
||||
{
|
||||
/**
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(
|
||||
protected array $options = []
|
||||
)
|
||||
{
|
||||
$this->setOptions($options);
|
||||
|
||||
$this->setOption('default_priority', Log::INFO, false);
|
||||
$this->setOption('line_ending', PHP_EOL, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws invalidArgumentException
|
||||
*/
|
||||
public static function factory($writerName, array $writerOptions): LogWriter
|
||||
{
|
||||
$logWriterClassName = 'LogWriter' . ucfirst($writerName);
|
||||
if(!class_exists($logWriterClassName)) {
|
||||
throw new InvalidArgumentException('Unknown ConLite LogWriter class: ' . $writerName);
|
||||
}
|
||||
|
||||
$writer = new $logWriterClassName($writerOptions);
|
||||
if(!($writer instanceof LogWriter)) {
|
||||
throw new InvalidArgumentException('Provided class is not an instance of ConLite LogWriter');
|
||||
}
|
||||
|
||||
return $writer;
|
||||
}
|
||||
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function getOption($option) {
|
||||
return $this->options[$option];
|
||||
}
|
||||
|
||||
public function setOptions(array $options): void
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function setOption($option, $value, $force = false) {
|
||||
if (!$force && isset($this->options[$option])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
public function removeOption($option) {
|
||||
unset($this->options[$option]);
|
||||
}
|
||||
|
||||
abstract function write($message, $priority);
|
||||
}
|
8
conlite/classes/Log/LogWriterFile.php
Normal file
8
conlite/classes/Log/LogWriterFile.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace ConLite\Log;
|
||||
|
||||
class LogWriterFile extends LogWriter
|
||||
{
|
||||
|
||||
}
|
8
conlite/classes/Log/ModuleLog.php
Normal file
8
conlite/classes/Log/ModuleLog.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace ConLite\Log;
|
||||
|
||||
class ModuleLog extends Log
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in a new issue