2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class for logging LBRY Daemon interactions
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
2022-02-14 02:34:20 +01:00
|
|
|
defined('ABSPATH') || die(); // Exit if accessed directly
|
2018-09-01 01:50:29 +02:00
|
|
|
|
|
|
|
class LBRY_Daemon_Logger
|
|
|
|
{
|
2022-02-12 22:28:25 +01:00
|
|
|
|
2018-09-11 22:10:15 +02:00
|
|
|
/**
|
2018-10-29 22:55:26 +01:00
|
|
|
* The directory to log to
|
|
|
|
* @var string
|
2018-09-11 22:10:15 +02:00
|
|
|
*/
|
2018-10-29 22:55:26 +01:00
|
|
|
private $dir = LBRY_ABSPATH . 'logs/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The filename to log to
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $filename;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file ext
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $ext = '.log';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Max size of the file before rotating
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $maxsize = 15728640; // 15MB;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename unique name for this logger to log to
|
|
|
|
*/
|
|
|
|
public function __construct($filename = 'daemon')
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
$this->filename = $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log to a file, with file size maximum. Adds hashs to old files
|
|
|
|
*
|
|
|
|
* @param string $event
|
|
|
|
* @param string $text
|
|
|
|
*/
|
|
|
|
public function log($event, $message = null)
|
|
|
|
{
|
|
|
|
$filepath = $this->dir . $this->filename . $this->ext;
|
|
|
|
|
|
|
|
// If our file is past our size limit, stash it
|
|
|
|
if (file_exists($filepath) && filesize($filepath) > $this->maxsize) {
|
|
|
|
rename($filepath, $this->dir . $this->filename . time() . $this->ext);
|
|
|
|
|
|
|
|
$logfiles = scandir($this->dir);
|
|
|
|
|
|
|
|
// If we have to many files, delete the oldest one
|
|
|
|
if (count($logfiles) > 7) {
|
|
|
|
$oldest = PHP_INT_MAX;
|
|
|
|
foreach ($logfiles as $file) {
|
|
|
|
if (!strstr($file, $this->filename)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stamp = substr($file, strlen($this->filename));
|
|
|
|
$stamp = str_replace($this->ext, '', $stamp);
|
|
|
|
if ($stamp && $stamp < $oldest) {
|
|
|
|
$oldest = $stamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($logfiles as $file) {
|
|
|
|
if (strstr($file, $this->filename) && strstr($file, $oldest)) {
|
|
|
|
unlink($this->dir . $file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
$space = str_repeat(' ', strlen($date) + 1);
|
|
|
|
$data = date('Y-m-d H:i:s') . " ";
|
|
|
|
$data .= "EVENT: " . $event . PHP_EOL;
|
|
|
|
$data .= $space . "MESSAGE: " . $message . PHP_EOL;
|
|
|
|
file_put_contents($filepath, $data, FILE_APPEND);
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
}
|