Zusammengeführt
Oldperl hat 17 Commits von feature/make-logrotation-switchable-#9 nach develop 2024-03-12 18:26:58 +00:00 zusammengeführt
2 geänderte Dateien mit 169 neuen und 4 gelöschten Zeilen
Nur Änderungen aus Commit 1eb4c86d57 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -77,7 +77,7 @@ class Log
/** /**
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __construct($writer) public function __construct(LogWriter $writer = null)
{ {
$createWriter = false; $createWriter = false;
@ -116,7 +116,8 @@ class Log
/** /**
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function setShortcutHandler($shortcut, $handler) { public function setShortcutHandler($shortcut, $handler): bool
{
if ($shortcut == '') { if ($shortcut == '') {
throw new InvalidArgumentException('The shortcut name must not be empty.'); throw new InvalidArgumentException('The shortcut name must not be empty.');
} }
@ -138,4 +139,168 @@ class Log
return true; return true;
} }
public function unsetShortcutHandler($shortcut)
{
if(!in_array($shortcut, $this->shortcutHandlers)) {
throw new InvalidArgumentException('The specified shortcut handler does not exist.');
}
unset($this->shortcutHandlers[$shortcut]);
return true;
}
public function buffer(string $message, $priority = null): void
{
$this->buffer[] = [$message, $priority];
}
public function commit(bool $clearBuffer = true)
{
if (count($this->buffer) == 0) {
cWarning(__FILE__, __LINE__, 'There are no buffered messages to commit.');
return false;
}
foreach ($this->buffer as $bufferInfo) {
$this->log($bufferInfo[0], $bufferInfo[1]);
}
if ($clearBuffer) {
$this->clearBuffer();
}
}
public function clearBuffer(): void
{
$this->buffer = [];
}
public function log(string $message, $priority = null): void
{
if ($priority && !is_int($priority) && in_array($priority, $this->priorities)) {
$priority = array_search($priority, $this->priorities);
}
if ($priority === null || !array_key_exists($priority, $this->priorities)) {
$priority = $this->getWriter()->getOption('default_priority');
}
$logMessage = $this->getWriter()->getOption('log_format');
$lineEnding = $this->getWriter()->getOption('line_ending');
foreach ($this->shortcutHandlers as $shortcut => $handler) {
if (cString::getPartOfString($shortcut, 0, 1) != '%') {
$shortcut = '%' . $shortcut;
}
$info = [
'message' => $message,
'priority' => $priority
];
$value = call_user_func($handler, $info);
$logMessage = str_replace($shortcut, $value, $logMessage);
}
$this->getWriter()->write($logMessage . $lineEnding, $priority);
}
/**
* @throws InvalidArgumentException
*/
public function addPriority(string $name, int $value): void
{
if ($name == '') {
throw new InvalidArgumentException('Priority name must not be empty.');
}
if (in_array($name, $this->priorities)) {
throw new InvalidArgumentException('The given priority name already exists.');
}
if (array_key_exists($value, $this->priorities)) {
throw new InvalidArgumentException('The priority value already exists.');
}
$this->priorities[$value] = $name;
}
/**
* @throws InvalidArgumentException
*/
public function removePriority(string $name): void
{
if ($name == '') {
throw new InvalidArgumentException('Priority name must not be empty.');
}
if (!in_array($name, $this->priorities)) {
throw new InvalidArgumentException('Priority name does not exist.');
}
if (in_array($name, $this->defaultPriorities)) {
throw new InvalidArgumentException('Removing default priorities is not allowed.');
}
$priorityIndex = array_search($name, $this->priorities);
unset($this->priorities[$priorityIndex]);
}
/**
* @throws InvalidArgumentException
*/
public function __call(string $method, array $arguments) {
$priorityName = cString::toUpperCase($method);
if (!in_array($priorityName, $this->priorities)) {
throw new InvalidArgumentException('The given priority ' . $priorityName . ' is not supported.');
}
$priorityIndex = array_search($priorityName, $this->priorities);
$this->log($arguments[0], $priorityIndex);
}
/**
* Shortcut Handler Date.
* Returns the current date.
*
* @return string
* The current date
*/
public function shDate(): string
{
return date('Y-m-d H:i:s');
}
/**
* Shortcut Handler Level.
* Returns the canonical name of the priority.
* The canonical name is padded to 10 characters to achieve a better
* formatting.
*
* @param array $info
* @return string
* The canonical log level
*/
public function shLevel(array $info): string
{
$logLevel = $info['priority'];
return str_pad($this->priorities[$logLevel], 10, ' ', STR_PAD_BOTH);
}
/**
* Shortcut Handler Message.
* Returns the log message.
*
* @param array $info
* @return string
* The log message
*/
public function shMessage(array $info): string
{
return $info['message'];
}
} }

Datei anzeigen

@ -24,9 +24,9 @@ abstract class LogWriter
*/ */
public static function factory($writerName, array $writerOptions): LogWriter public static function factory($writerName, array $writerOptions): LogWriter
{ {
$logWriterClassName = 'LogWriter' . ucfirst($writerName); $logWriterClassName = __NAMESPACE__ . '\\LogWriter' . ucfirst($writerName);
if(!class_exists($logWriterClassName)) { if(!class_exists($logWriterClassName)) {
throw new InvalidArgumentException('Unknown ConLite LogWriter class: ' . $writerName); throw new InvalidArgumentException('Unknown ConLite LogWriter class: ' . $logWriterClassName);
} }
$writer = new $logWriterClassName($writerOptions); $writer = new $logWriterClassName($writerOptions);