1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace pjpawel\LightApi\Component\Logger\SimpleLogger; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use Exception; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Stringable; |
10
|
|
|
|
11
|
|
|
class SimpleLogger implements LoggerInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private const SEPARATOR = ' '; |
15
|
|
|
|
16
|
|
|
private Level $logLevel; |
17
|
|
|
private string $logFilePath; |
18
|
|
|
private DateTimeZone $timeZone; |
19
|
|
|
|
20
|
|
|
public function __construct(string $logFilePath, Level $logLevel = Level::Debug) |
21
|
|
|
{ |
22
|
|
|
if (!is_file($logFilePath)) { |
23
|
|
|
$this->createLogFile($logFilePath); |
24
|
|
|
} |
25
|
|
|
$this->logFilePath = realpath($logFilePath); |
26
|
|
|
$this->logLevel = $logLevel; |
27
|
|
|
$this->timeZone = new DateTimeZone(date_default_timezone_get()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
private function createLogFile(string $filePath): void |
34
|
|
|
{ |
35
|
|
|
if (is_file($filePath)) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
$dirname = dirname($filePath); |
39
|
|
|
if (!is_dir($dirname) && !mkdir($dirname, 0777, true)) { |
40
|
|
|
throw new Exception('Cannot create log dir'); |
41
|
|
|
} |
42
|
|
|
if (!file_put_contents($filePath, 'Started logging...' . PHP_EOL, FILE_APPEND)) { |
43
|
|
|
throw new Exception('Cannot create log file'); |
44
|
|
|
} |
45
|
|
|
chmod($filePath, 0777); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function addRecord(Level $level, string $message, array $context): void |
49
|
|
|
{ |
50
|
|
|
if ($level->isToLog($this->logLevel)) { |
51
|
|
|
$message = (new DateTime('now', $this->timeZone))->format(DATE_RFC3339_EXTENDED) |
52
|
|
|
. self::SEPARATOR . $level->name . self::SEPARATOR . $message; |
|
|
|
|
53
|
|
|
if (!empty($context)) { |
54
|
|
|
$message .= self::SEPARATOR . var_export($context, true); |
55
|
|
|
} |
56
|
|
|
file_put_contents($this->logFilePath, $message . PHP_EOL, FILE_APPEND); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function emergency(Stringable|string $message, array $context = []): void |
61
|
|
|
{ |
62
|
|
|
$this->addRecord(Level::Emergency, $message, $context); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function alert(Stringable|string $message, array $context = []): void |
66
|
|
|
{ |
67
|
|
|
$this->addRecord(Level::Alert, $message, $context); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function critical(Stringable|string $message, array $context = []): void |
71
|
|
|
{ |
72
|
|
|
$this->addRecord(Level::Critical, $message, $context); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function error(Stringable|string $message, array $context = []): void |
76
|
|
|
{ |
77
|
|
|
$this->addRecord(Level::Error, $message, $context); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function warning(Stringable|string $message, array $context = []): void |
81
|
|
|
{ |
82
|
|
|
$this->addRecord(Level::Warning, $message, $context); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function notice(Stringable|string $message, array $context = []): void |
86
|
|
|
{ |
87
|
|
|
$this->addRecord(Level::Notice, $message, $context); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function info(Stringable|string $message, array $context = []): void |
91
|
|
|
{ |
92
|
|
|
$this->addRecord(Level::Info, $message, $context); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function debug(Stringable|string $message, array $context = []): void |
96
|
|
|
{ |
97
|
|
|
$this->addRecord(Level::Emergency, $message, $context); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function log($level, Stringable|string $message, array $context = []): void |
101
|
|
|
{ |
102
|
|
|
$this->addRecord(Level::Emergency, $message, $context); |
103
|
|
|
} |
104
|
|
|
} |