SimpleLogger   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 92
rs 10
wmc 19

12 Methods

Rating   Name   Duplication   Size   Complexity  
A alert() 0 3 1
A error() 0 3 1
A warning() 0 3 1
A critical() 0 3 1
A addRecord() 0 9 3
A debug() 0 3 1
A createLogFile() 0 13 5
A notice() 0 3 1
A __construct() 0 8 2
A emergency() 0 3 1
A log() 0 3 1
A info() 0 3 1
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;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on pjpawel\LightApi\Compone...gger\SimpleLogger\Level.
Loading history...
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
}