97 lines
2.1 KiB
PHP
97 lines
2.1 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(string $message, array $tags, ?array $backtrace = null) : void
|
|
{
|
|
$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.
|
|
*
|
|
* @throws \core\config\ConfigException
|
|
*/
|
|
public function display() : string
|
|
{
|
|
$hardcoded_configuration = \core\config\class\core\log\FormatConfiguration(array(
|
|
'message' => '[{tags}] - {date}: {message} in {file} at line {line}',
|
|
'date' => 'Y-m-d H:i:s',
|
|
)); # NOTE: hard-coded fallback
|
|
$msg_format = $hardcoded_configuration.get('message');
|
|
$date_format = $hardcoded_configuration.get('date');
|
|
|
|
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log', 'format'))
|
|
{
|
|
$configuration = \core\config\GlobalConfiguration::read('class', 'core', 'log', 'format');
|
|
|
|
if ($configuration->exists('message'))
|
|
{
|
|
$msg_format = $configuration->get('message');
|
|
}
|
|
if ($configuration->exists('date'))
|
|
{
|
|
$date_format = $configuration->get('date');
|
|
}
|
|
}
|
|
|
|
return \core\substitute(
|
|
$msg_format,
|
|
array(
|
|
'date' => \date($date_format),
|
|
'file' => $this->backtrace['file'],
|
|
'line' => $this->backtrace['line'],
|
|
'message' => $this->message,
|
|
'tags' => $this->tags,
|
|
),
|
|
) . \PHP_EOL;
|
|
}
|
|
}
|
|
|
|
?>
|