1 | <?php |
||
2 | namespace SimpleLogger; |
||
3 | |||
4 | use SimpleLogger\Colours; |
||
5 | use Psr\Log\AbstractLogger; |
||
6 | |||
7 | class SimpleLogger extends AbstractLogger |
||
8 | { |
||
9 | /** |
||
10 | * Configuration class object. |
||
11 | * |
||
12 | * @var object |
||
13 | */ |
||
14 | private $configuration; |
||
15 | |||
16 | /** |
||
17 | * Constructor. |
||
18 | * |
||
19 | * @param object $configuration Configuration class object |
||
20 | * @return void |
||
21 | */ |
||
22 | public function __construct(Configuration $configuration = null) |
||
23 | { |
||
24 | $this->configuration = ($configuration instanceof Configuration) ? $configuration : new Configuration(); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Format log line output. |
||
29 | * |
||
30 | * @param string $level log level |
||
31 | * @param string $message log message |
||
32 | * @return void |
||
33 | */ |
||
34 | final protected function formatter($level, $message, $format) |
||
35 | { |
||
36 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
37 | |||
38 | $timestamp = date('c'); |
||
39 | $class = isset($backtrace[3]['class']) ? $backtrace[3]['class'] : ""; |
||
40 | $file = isset($backtrace[2]['file']) ? $backtrace[2]['file'] : ""; |
||
41 | $function = isset($backtrace[3]['function']) ? $backtrace[3]['function'] : ""; |
||
42 | $line = isset($backtrace[2]['line']) ? $backtrace[2]['line'] : ""; |
||
43 | $pid = getmypid(); |
||
44 | $tag = $this->configuration->getTag(); |
||
45 | |||
46 | $identifier = ['%t', '%c', '%f', '%F', '%l', '%L', '%p', '%m', '%T']; |
||
47 | $values = [$timestamp, $class, $function, $file, strtoupper($level), $line, $pid, $message, $tag]; |
||
48 | $format = str_replace($identifier, $values, $format); |
||
49 | |||
50 | return $format; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function log($level, $message, array $context = []) |
||
57 | { |
||
58 | $stdout = fopen('php://stdout', 'w'); |
||
59 | if ($this->configuration->getLevel($level) >= $this->configuration->getVerbosity()) |
||
60 | { |
||
61 | if ($this->configuration->getLevel($level) >= $this->configuration->getConsoleVerbosity()) |
||
62 | { |
||
63 | $console_format = $this->configuration->getLoggerConsoleFormat(); |
||
64 | $logline = $this->formatter($level, $message, $console_format); |
||
65 | list($foreground, $background) = $this->configuration->getColours($level); |
||
66 | fwrite($stdout, Colours::setColour($logline, $foreground, $background) . PHP_EOL); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
67 | } |
||
68 | |||
69 | if (($logfile = $this->configuration->getLogfile()) !== null) |
||
70 | { |
||
71 | if (is_writable($logfile) === false) |
||
72 | { |
||
73 | throw new \RuntimeException("Log file {$logfile} is not writable."); |
||
74 | } |
||
75 | |||
76 | $file_format = $this->configuration->getLoggerFileFormat(); |
||
77 | $logline = $this->formatter($level, $message, $file_format); |
||
78 | $handle = fopen($logfile, "a+"); |
||
79 | fwrite($handle, $logline . PHP_EOL); |
||
80 | fclose($handle); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 |