Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractWriter
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 setLogLimit
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLogLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasLogLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWithinLogLimit
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 writeLog
n/a
0 / 0
n/a
0 / 0
0
 getContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sanitize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Log\Writer;
16
17use Pop\Log\Context;
18use Pop\Log\Level;
19
20/**
21 * Log writer abstract class
22 *
23 * @category   Pop
24 * @package    Pop\Log
25 * @author     Nick Sagona, III <nick@popphp.org>
26 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30abstract class AbstractWriter implements WriterInterface
31{
32
33    /**
34     * Log limit, stored as the canonical PSR-3 level string
35     * @var ?string
36     */
37    protected ?string $limit = null;
38
39    /**
40     * Set log limit
41     *
42     * Accepts either a PSR-3 level string or a legacy severity int (0-7) for backward compatibility;
43     * always normalized and stored as the canonical PSR-3 string.
44     *
45     * @param  string|int $level
46     * @return AbstractWriter
47     */
48    public function setLogLimit(string|int $level): AbstractWriter
49    {
50        $this->limit = Level::fromSeverity(Level::toSeverity($level));
51        return $this;
52    }
53
54    /**
55     * Get log limit
56     *
57     * @return string|null
58     */
59    public function getLogLimit(): string|null
60    {
61        return $this->limit;
62    }
63
64    /**
65     * Has log limit
66     *
67     * @return bool
68     */
69    public function hasLogLimit(): bool
70    {
71        return ($this->limit !== null);
72    }
73
74    /**
75     * Check if a log level is within the set log level limit
76     *
77     * @param  string|int $level
78     * @return bool
79     */
80    public function isWithinLogLimit(string|int $level): bool
81    {
82        $severity = Level::toSeverity($level);
83        return (($this->limit === null) || ($severity <= Level::toSeverity($this->limit)));
84    }
85
86    /**
87     * Write to the log
88     *
89     * @param  string $level
90     * @param  string $message
91     * @param  array  $context
92     * @return AbstractWriter
93     */
94    abstract public function writeLog(string $level, string $message, array $context = []): AbstractWriter;
95
96    /**
97     * Get context for log
98     *
99     * @param  array $context
100     * @return string
101     */
102    public function getContext(array $context = []): string
103    {
104        return Context::serialize($context);
105    }
106
107    /**
108     * Strip CR/LF from a value before it's written into a delimited/line-based log format, to prevent an
109     * embedded newline from forging a fake extra line (or, for writers that build header-style text like
110     * Mail's Subject line, a fake extra header).
111     *
112     * @param  string $value
113     * @return string
114     */
115    protected function sanitize(string $value): string
116    {
117        return Context::sanitize($value);
118    }
119
120}