1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Log; |
4
|
|
|
|
5
|
|
|
use Psr\Log\AbstractLogger; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Anax Logger class. |
10
|
|
|
* |
11
|
|
|
* The message MUST be a string or object implementing __toString(). |
12
|
|
|
* |
13
|
|
|
* The message MAY contain placeholders in the form: {foo} where foo |
14
|
|
|
* will be replaced by the context data in key "foo". |
15
|
|
|
* |
16
|
|
|
* The context array can contain arbitrary data, the only assumption that |
17
|
|
|
* can be made by implementors is that if an Exception instance is given |
18
|
|
|
* to produce a stack trace, it MUST be in a key named "exception". |
19
|
|
|
* |
20
|
|
|
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
21
|
|
|
* for the full interface specification. |
22
|
|
|
*/ |
23
|
|
|
class FileLogger extends AbstractLogger |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Logs with an arbitrary level. |
27
|
|
|
* |
28
|
|
|
* @param mixed $level the loglevel to use for this entry. |
29
|
|
|
* @param string $message to log. |
30
|
|
|
* @param array $context extra information to log. |
31
|
|
|
* |
32
|
|
|
* @return null |
33
|
|
|
*/ |
34
|
|
|
public function log($level, $message, array $context = []) |
35
|
|
|
{ |
36
|
|
|
$file = ANAX_INSTALL_PATH . "/log/dev_log"; |
37
|
|
|
$dateTime = new \DateTime("now", new \DateTimeZone("Europe/Stockholm")); |
38
|
|
|
$date = $dateTime->format("Y-m-d H:m:s e"); |
39
|
|
|
|
40
|
|
|
$contextString = ""; |
41
|
|
|
foreach ($context as $key => $val) { |
42
|
|
|
$contextString .= " $key=" . htmlentities($val) . ","; |
43
|
|
|
} |
44
|
|
|
$contextString = rtrim($contextString, ","); |
45
|
|
|
|
46
|
|
|
$record = "[$date] [$level] {$message}{$contextString}\n"; |
47
|
|
|
file_put_contents($file, $record, FILE_APPEND); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|