85 lines
1.8 KiB
PHP
85 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace core\log;
|
|
|
|
/**
|
|
* A log event
|
|
*/
|
|
class Event
|
|
{
|
|
/**
|
|
* Message associated to this event
|
|
*
|
|
* @var \string
|
|
*/
|
|
protected \string $message;
|
|
|
|
/**
|
|
* Time associated to this event
|
|
*
|
|
* @var \int
|
|
*/
|
|
protected \int $timestamp;
|
|
|
|
/**
|
|
* Tags associated to this event
|
|
*
|
|
* @var \core\log\Tag[]
|
|
*/
|
|
protected \array $tags;
|
|
|
|
/**
|
|
* Backtrace associated to this event
|
|
*
|
|
* @var ?\array
|
|
*/
|
|
protected ?\array $backtrace;
|
|
|
|
public function __construct(message: \string $message, tags: \array $tags, backtrace: ?\array = null)
|
|
{
|
|
$this->message = $message; # not escaped
|
|
$this->tags = $tags;
|
|
$this->timestamp = \time();
|
|
|
|
if ($backtrace === null)
|
|
{
|
|
$backtrace = \debug_backtrace();
|
|
$key = \array_search(__FUNCTION__, \array_column($backtrace, 'function'),); # NOTE: manual search, can change with time
|
|
$backtrace = $backtrace[$key];
|
|
}
|
|
$this->backtrace = $backtrace;
|
|
}
|
|
|
|
/**
|
|
* Display this event for logging
|
|
*
|
|
* This can be changed from configuration.
|
|
* The EOL (end of line sequence) is automatically added at the end.
|
|
*/
|
|
public function display() : \string
|
|
{
|
|
$configuration = \core\config\GlobalConfiguration::read('class', 'core', 'log');
|
|
$format = '[{tags}] - {date}: {message} in {file} at line {line}'; # NOTE: hard-coded fallback
|
|
$date_format = 'Y-m-d H:i:s'; # NOTE: hard-coded fallback
|
|
if ($configuration->exist('format'))
|
|
{
|
|
$format = $configuration->get('format');
|
|
}
|
|
if ($configuration->exist('date-format'))
|
|
{
|
|
$date_format = $configuration->get('date-format');
|
|
}
|
|
return \core\substitute(
|
|
$format,
|
|
array(
|
|
'date' => \date($date_format),
|
|
'file' => $this->backtrace['file'],
|
|
'line' => $this->backtrace['line'],
|
|
'message' => $this->message,
|
|
'tags' => $this->tags,
|
|
),
|
|
) . \PHP_EOL;
|
|
}
|
|
}
|
|
|
|
?>
|