Passed
Push — master ( 08c723...c10409 )
by Stefan
01:27
created

SKien/XLogger/FileLogger.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\XLogger;
5
6
use Psr\Log\LogLevel;
7
8
/**
9
 * PSR-3 compliant logger for Output to plain text file (.log, .csv, .txt)
10
 *
11
 * Each log item is represented by one single line containing the fields
12
 * separated by defined character.
13
 *
14
 * Dependent on the file extension following field separator is used:
15
 * - `.log` :   TAB
16
 * - `.csv` :   semicolon (;)
17
 * - `.txt` :   colon (,)
18
 *
19
 * CR/LF in message are replaced with Space to keep one item in a single line.
20
 * Field separator is replaced to keep the number of fields consistent.
21
 *
22
 * @package XLogger
23
 * @author Stefanius <[email protected]>
24
 * @copyright MIT License - see the LICENSE file for details
25
 */
26
class FileLogger extends XLogger
27
{
28
    /** @var resource|bool file handle of the opened logfile     */
29
    protected $logfile = false;
30
    /** @var string separator     */
31
    protected string $strSep = '';
32
    /** @var string replacement for separator inside of message      */
33
    protected string $strReplace = '';
34
    /** @var bool $bReset reset file when open     */
35
    protected bool $bReset = false;
36
37
    /**
38
     * Init logging level and remote username (if set).
39
     * @see XLogger::setLogLevel()
40
     * @param string $level the min. `LogLevel` to be logged
41
     */
42
    public function __construct(string $level = LogLevel::DEBUG)
43
    {
44
        parent::__construct($level);
45
    }
46
47
    /**
48
     * close file if already opened.
49
     */
50
    public function __destruct()
51
    {
52
        // textbased file kept open...
53
        $this->closeLogfile();
54
    }
55
56
    /**
57
     * Logs with an arbitrary level.
58
     * @param string    $level
59
     * @param mixed     $message
60
     * @param mixed[]   $context
61
     * @return void
62
     * @throws \Psr\Log\InvalidArgumentException
63
     */
64
    public function log($level, $message, array $context = array()) : void
65
    {
66
        // check, if requested level should be logged
67
        // causes InvalidArgumentException in case of unknown level.
68
        if ($this->logLevel($level)) {
69
            // Open file if not opened so far, dependend on the file extension the separator is set.
70
            $this->openLogfile();
71
            if (!is_resource($this->logfile)) {
72
                return;
73
            }
74
75
            // timestamp
76
            $strLine = date('Y-m-d H:i:s');
77
            // IP adress
78
            if (($this->iOptions & self::LOG_IP) != 0) {
79
                $strIP = $_SERVER['REMOTE_ADDR'];
80
                if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
81
                    $strIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
82
                }
83
                $strLine .= $this->strSep . $strIP;
84
            }
85
            // user
86
            if (($this->iOptions & self::LOG_USER) != 0) {
87
                $strLine .= $this->strSep . $this->prepareText($this->strUser);
88
            }
89
            // backtrace - caller
90
            if (($this->iOptions & self::LOG_BT) != 0) {
91
                $strLine .= $this->strSep . $this->getCaller();
92
            }
93
            // the message
94
            $strMessage = $this->replaceContext($message, $context);
95
            $strLine .= $this->strSep . $this->prepareText(strtoupper($level) . ': ' . $strMessage);
96
            // user agent
97
            if (($this->iOptions & self::LOG_BT) != 0) {
98
                $strLine .= $this->strSep . $this->prepareText($_SERVER["HTTP_USER_AGENT"]);
99
            }
100
101
            // and write to the file
102
            flock($this->logfile, LOCK_EX);
103
            fwrite($this->logfile, $strLine . PHP_EOL);
104
            fflush($this->logfile);
105
            flock($this->logfile, LOCK_UN);
106
        }
107
    }
108
109
    /**
110
     * close file if open, reopen and truncate existing file
111
     */
112
    public function reset() : void
113
    {
114
        if (is_resource($this->logfile)) {
115
            fclose($this->logfile);
116
            $this->logfile = fopen($this->getFullpath(), 'w');
117
        } else {
118
            $this->bReset = true;
119
        }
120
    }
121
122
    /**
123
     * Open the logfile if not done so far.
124
     * Dependend on the file extension the speparator is set.
125
     * @return void
126
     */
127
    protected function openLogfile() : void
128
    {
129
        if ($this->logfile === false) {
130
            $strFullPath = $this->getFullpath();
131
            switch (strtolower(pathinfo($strFullPath, PATHINFO_EXTENSION))) {
1 ignored issue
show
It seems like pathinfo($strFullPath, S...ger\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            switch (strtolower(/** @scrutinizer ignore-type */ pathinfo($strFullPath, PATHINFO_EXTENSION))) {
Loading history...
132
                case 'csv':
133
                case 'txt':
134
                    $this->strSep = ";";
135
                    $this->strReplace = ",";
136
                    break;
137
                case 'log':
138
                default:
139
                    $this->strSep = "\t";
140
                    $this->strReplace = " ";
141
                    break;
142
            }
143
            $this->logfile = fopen($strFullPath, $this->bReset ? 'w' : 'a');
144
            $this->bReset = false;
145
        }
146
    }
147
148
    /**
149
     * close file if already opened.
150
     * @return void
151
     */
152
    protected function closeLogfile() : void
153
    {
154
        if (is_resource($this->logfile)) {
155
            fclose($this->logfile);
156
            $this->logfile = false;
157
        }
158
    }
159
160
161
    /**
162
     * @param string $strMessage
163
     * @return string
164
     */
165
    protected function prepareText(string $strMessage) : string
166
    {
167
        // it make sense to replace LF because each line representing one log item!
168
        // ... and also the separator should not be included in the message itself
169
        if (strlen($this->strSep) > 0) {
170
            $strMessage = str_replace("\r\n", ' ', $strMessage);
171
            $strMessage = str_replace("\r", ' ', $strMessage);
172
            $strMessage = str_replace("\n", ' ', $strMessage);
173
            $strMessage = str_replace($this->strSep, $this->strReplace, $strMessage);
174
        }
175
        return $strMessage;
176
    }
177
}
178