Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| AbstractWriter | |
100.00% |
32 / 32 |
|
100.00% |
5 / 5 |
19 | |
100.00% |
1 / 1 |
| setLogLimit | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getLogLimit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasLogLimit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isWithinLogLimit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| writeLog | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getContext | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Log\Writer; |
| 15 | |
| 16 | /** |
| 17 | * Log writer abstract class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Log |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.0.3 |
| 25 | */ |
| 26 | abstract class AbstractWriter implements WriterInterface |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Log limit |
| 31 | * @var ?int |
| 32 | */ |
| 33 | protected ?int $limit = null; |
| 34 | |
| 35 | /** |
| 36 | * Set log limit |
| 37 | * |
| 38 | * @param int $level |
| 39 | * @return AbstractWriter |
| 40 | */ |
| 41 | public function setLogLimit(int $level): AbstractWriter |
| 42 | { |
| 43 | $level = (int)$level; |
| 44 | |
| 45 | if (($level < 0) || ($level > 7)) { |
| 46 | throw new \InvalidArgumentException('Error: The level ' . $level . ' is an invalid level.'); |
| 47 | } |
| 48 | |
| 49 | $this->limit = $level; |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get log limit |
| 55 | * |
| 56 | * @return int|null |
| 57 | */ |
| 58 | public function getLogLimit(): int|null |
| 59 | { |
| 60 | return $this->limit; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Has log limit |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function hasLogLimit(): bool |
| 69 | { |
| 70 | return ($this->limit !== null); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if a log level is within the set log level limit |
| 75 | * |
| 76 | * @param int $level |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function isWithinLogLimit(int $level): bool |
| 80 | { |
| 81 | if (($level < 0) || ($level > 7)) { |
| 82 | throw new \InvalidArgumentException('Error: The level ' . $level . ' is an invalid level.'); |
| 83 | } |
| 84 | |
| 85 | return (($this->limit === null) || ($level <= $this->limit)); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Write to the log |
| 90 | * |
| 91 | * @param mixed $level |
| 92 | * @param string $message |
| 93 | * @param array $context |
| 94 | * @return AbstractWriter |
| 95 | */ |
| 96 | abstract public function writeLog(mixed $level, string $message, array $context = []): AbstractWriter; |
| 97 | |
| 98 | /** |
| 99 | * Get context for log |
| 100 | * |
| 101 | * @param array $context |
| 102 | * @return string |
| 103 | */ |
| 104 | public function getContext(array $context = []): string |
| 105 | { |
| 106 | $messageContext = ''; |
| 107 | |
| 108 | if (isset($context['timestamp'])) { |
| 109 | unset($context['timestamp']); |
| 110 | } |
| 111 | if (isset($context['name'])) { |
| 112 | unset($context['name']); |
| 113 | } |
| 114 | if (isset($context['format'])) { |
| 115 | $format = $context['format']; |
| 116 | unset($context['format']); |
| 117 | } else { |
| 118 | $format = 'text'; |
| 119 | } |
| 120 | |
| 121 | switch ($format) { |
| 122 | // If the data values needs to be preserved, use JSON encoding or PHP serialization |
| 123 | case 'json': |
| 124 | $messageContext = json_encode($context); |
| 125 | break; |
| 126 | case 'php': |
| 127 | $messageContext = serialize($context); |
| 128 | break; |
| 129 | // Else, complex values like arrays and objects will get reduced to a basic string representation, i.e. [Array] |
| 130 | default: |
| 131 | foreach ($context as $key => $value) { |
| 132 | if (is_array($value)) { |
| 133 | $value = '[Array]'; |
| 134 | } |
| 135 | if (is_object($value)) { |
| 136 | $value = '[Object]'; |
| 137 | } |
| 138 | $messageContext .= (string)$key . '=' . (string)$value . ';'; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $messageContext; |
| 143 | } |
| 144 | |
| 145 | } |