rosavox/lib/alveolata/log/output-implementation-file.php
2025-05-23 07:33:29 +00:00

99 lines
1.9 KiB
PHP

<?php
namespace alveolata\log;
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/string/functions.php');
require_once(DIR_ALVEOLATA . '/list/functions.php');
require_once(DIR_ALVEOLATA . '/report/functions.php');
require_once(DIR_ALVEOLATA . '/log/output-interface.php');
/**
* @author Christian Fraß <frass@greenscale.de>
*/
class implementation_file implements interface_output
{
/**
* @var string
* @author Christian Fraß <frass@greenscale.de>
*/
protected $path;
/**
* @var boolean
*/
protected $human_readable;
/**
* @param int $level_threshold
* @author Christian Fraß <frass@greenscale.de>
*/
public function __construct(
string $path,
array $options = []
)
{
$options = \array_merge(
[
'human_readable' => false,
],
$options
);
$this->path = $path;
$this->human_readable = $options['human_readable'];
}
/**
* @implementation
* @author Christian Fraß <frass@greenscale.de>
*/
public function process(
int $level,
\alveolata\report\struct_report $report
) : void
{
$content = (
$this->human_readable
? \alveolata\string\coin(
(
empty($report->details)
? "<{{datetime}}> [{{level}}] {{incident}}\n"
: "<{{datetime}}> [{{level}}] {{incident}} | {{details}}\n"
),
[
'datetime' => date('Y-m-d|H:i:s', $report->timestamp),
'level' => output_translate_level($level),
'incident' => $report->incident,
'details' => \alveolata\json\encode($report->details),
]
)
: (
\alveolata\json\encode(
[
'timestamp' => $report->timestamp,
'datetime' => date('c', $report->timestamp),
'level_value' => $level,
'level_name' => output_translate_level($level),
'incident' => $report->incident,
'details' => $report->details,
]
)
.
"\n"
)
);
file_put_contents(
$this->path,
$content,
FILE_APPEND
);
}
}
?>