1
0
www.mikescher.com/www/extern/egg/Logger.php

102 lines
1.9 KiB
PHP
Raw Normal View History

2019-12-28 14:00:11 +01:00
<?php
require_once 'Utils.php';
interface ILogger
{
public function proclog($text);
}
class FileLogger implements ILogger
{
/** @var string $path */
private $path;
/**
* @var string $filename
* @var int $count
*/
public function __construct($filename, $count)
{
for ($i=$count-1;$i>0;$i--)
{
$f2 = Utils::sharpFormat($filename, [ 'num' => '_'.( $i ) ]);
$f1 = Utils::sharpFormat($filename, [ 'num' => '_'.( $i-1 ) ]);
if ($i-1 === 0) $f1 = Utils::sharpFormat($filename, [ 'num' => '' ]);
if (file_exists($f2)) @unlink($f2);
if (file_exists($f1)) @rename($f1, $f2);
}
$f0 = Utils::sharpFormat($filename, ['num'=>'' ]);
if (file_exists($f0)) @unlink($f0);
$this->path = $f0;
}
public function proclog($text)
{
if ($text !== '') $text = '[' . date('H:i:s') . '] ' . $text;
file_put_contents($this->path, $text . PHP_EOL , FILE_APPEND | LOCK_EX);
}
}
2019-12-29 00:26:57 +01:00
class SingleFileLogger implements ILogger
{
/** @var string $path */
private $path;
/**
* @var string $filename
*/
public function __construct($filename)
{
$this->path = $filename;
2022-10-15 16:56:50 +02:00
file_put_contents($this->path, '');
2019-12-29 00:26:57 +01:00
}
public function proclog($text)
{
file_put_contents($this->path, $text . PHP_EOL , FILE_APPEND | LOCK_EX);
}
}
2019-12-28 14:00:11 +01:00
class SessionLogger implements ILogger
{
/** @var string $sessionvar */
private $sessionvar;
/** @var string $sessionvar */
public function __construct($sessionvar)
{
$this->sessionvar = $sessionvar;
if (session_status() !== PHP_SESSION_DISABLED)
{
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
$_SESSION[$sessionvar] = '';
session_commit();
}
}
public function proclog($text)
{
if (session_status() === PHP_SESSION_DISABLED) return;
2022-10-16 15:50:38 +02:00
$_SESSION[$this->sessionvar] .= $text . "\n";
2019-12-28 14:00:11 +01:00
session_commit();
}
}
class OutputLogger implements ILogger
{
public function proclog($text)
{
if ($text !== '') $text = '[' . date('H:i:s') . '] ' . $text;
print $text;
2022-10-16 15:50:38 +02:00
print "\n";
2019-12-28 14:00:11 +01:00
}
}