Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
File | |
100.00% |
46 / 46 |
|
100.00% |
2 / 2 |
14 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
writeLog | |
100.00% |
41 / 41 |
|
100.00% |
1 / 1 |
10 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Log\Writer; |
15 | |
16 | /** |
17 | * File log writer class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Log |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 3.3.0 |
25 | */ |
26 | class File extends AbstractWriter |
27 | { |
28 | |
29 | /** |
30 | * Log file |
31 | * @var string |
32 | */ |
33 | protected $file = null; |
34 | |
35 | /** |
36 | * Log file type |
37 | * @var string |
38 | */ |
39 | protected $type = null; |
40 | |
41 | /** |
42 | * Constructor |
43 | * |
44 | * Instantiate the file writer object |
45 | * |
46 | * @param string $file |
47 | */ |
48 | public function __construct($file) |
49 | { |
50 | if (!file_exists($file)) { |
51 | touch($file); |
52 | } |
53 | |
54 | $parts = pathinfo($file); |
55 | |
56 | $this->file = $file; |
57 | $this->type = (isset($parts['extension']) && !empty($parts['extension'])) ? $parts['extension'] : null; |
58 | } |
59 | |
60 | /** |
61 | * Write to the log |
62 | * |
63 | * @param mixed $level |
64 | * @param string $message |
65 | * @param array $context |
66 | * @return File |
67 | */ |
68 | public function writeLog($level, $message, array $context = []) |
69 | { |
70 | if ($this->isWithinLogLimit($level)) { |
71 | switch (strtolower($this->type)) { |
72 | case 'csv': |
73 | $message = '"' . str_replace('"', '\"', $message) . '"' ; |
74 | $entry = $context['timestamp'] . "," . $level . "," . |
75 | $context['name'] . "," . $message . "," . $this->getContext($context) . PHP_EOL; |
76 | file_put_contents($this->file, $entry, FILE_APPEND); |
77 | break; |
78 | |
79 | case 'tsv': |
80 | $message = '"' . str_replace('"', '\"', $message) . '"' ; |
81 | $entry = $context['timestamp'] . "\t" . $level . "\t" . |
82 | $context['name'] . "\t" . $message . "\t" . $this->getContext($context) . PHP_EOL; |
83 | file_put_contents($this->file, $entry, FILE_APPEND); |
84 | break; |
85 | |
86 | case 'xml': |
87 | $output = file_get_contents($this->file); |
88 | if (strpos($output, '<?xml version') === false) { |
89 | $output = '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL . |
90 | '<log>' . PHP_EOL . '</log>' . PHP_EOL; |
91 | } |
92 | |
93 | $messageContext = $this->getContext($context); |
94 | |
95 | $entry = ($messageContext != '') ? |
96 | ' <entry timestamp="' . $context['timestamp'] . '" priority="' . |
97 | $level . '" name="' . $context['name'] . '" context="' . $messageContext . |
98 | '"><![CDATA[' . $message . ']]></entry>' . PHP_EOL : |
99 | ' <entry timestamp="' . $context['timestamp'] . '" priority="' . |
100 | $level . '" name="' . $context['name'] . '"><![CDATA[' . $message . ']]></entry>' . PHP_EOL; |
101 | |
102 | $output = str_replace('</log>' . PHP_EOL, $entry . '</log>' . PHP_EOL, $output); |
103 | file_put_contents($this->file, $output); |
104 | break; |
105 | |
106 | case 'json': |
107 | $output = file_get_contents($this->file); |
108 | $json = (strpos($output, '{') !== false) ? |
109 | json_decode($output, true) : []; |
110 | |
111 | $messageContext = $this->getContext($context); |
112 | |
113 | $json[] = [ |
114 | 'timestamp' => $context['timestamp'], |
115 | 'priority' => $level, |
116 | 'name' => $context['name'], |
117 | 'message' => $message, |
118 | 'context' => $messageContext |
119 | ]; |
120 | |
121 | file_put_contents($this->file, json_encode($json, JSON_PRETTY_PRINT)); |
122 | break; |
123 | |
124 | default: |
125 | $entry = $context['timestamp'] . "\t" . $level . "\t" . |
126 | $context['name'] . "\t" . $message . "\t" . $this->getContext($context) . PHP_EOL; |
127 | file_put_contents($this->file, $entry, FILE_APPEND); |
128 | } |
129 | } |
130 | |
131 | return $this; |
132 | } |
133 | |
134 | } |