1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Logger\Adapters; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
18
|
|
|
use Quantum\Logger\ReportableInterface; |
19
|
|
|
use Quantum\Exceptions\DiException; |
20
|
|
|
use Quantum\Logger\LoggerException; |
21
|
|
|
use ReflectionException; |
22
|
|
|
use Quantum\Di\Di; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class SingleAdapter |
26
|
|
|
* @package Quantum\Logger |
27
|
|
|
*/ |
28
|
|
|
class SingleAdapter implements ReportableInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FileSystem |
33
|
|
|
*/ |
34
|
|
|
private $fs; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $logFile; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* SingleAdapter constructor. |
43
|
|
|
* @param array $params |
44
|
|
|
* @throws DiException |
45
|
|
|
* @throws LoggerException |
46
|
|
|
* @throws ReflectionException |
47
|
|
|
* @throws \Quantum\Exceptions\LangException |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $params) |
50
|
|
|
{ |
51
|
|
|
$this->fs = Di::get(FileSystem::class); |
52
|
|
|
|
53
|
|
|
if (!$this->fs->extension($params['path'])) { |
54
|
|
|
throw LoggerException::logPathIsNotFile($params['path']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->logFile = $params['path']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function report(string $level, $message, ?array $context = []) |
64
|
|
|
{ |
65
|
|
|
$this->fs->append($this->logFile, $this->formatMessage($level, $message, $context)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $level |
70
|
|
|
* @param $message |
71
|
|
|
* @param array|null $context |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function formatMessage(string $level, $message, ?array $context = []): string |
75
|
|
|
{ |
76
|
|
|
return sprintf( |
77
|
|
|
"[%s] %s: %s%s", |
78
|
|
|
date('Y-m-d H:i:s'), |
79
|
|
|
ucfirst($level), |
80
|
|
|
is_array($message) ? json_encode($message, JSON_PRETTY_PRINT) : $message, |
81
|
|
|
isset($context['trace']) ? (PHP_EOL . $context['trace'] . PHP_EOL) : PHP_EOL |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |