]> git.mxchange.org Git - friendica.git/blob - src/Object/Log/ParsedLogLine.php
Continued:
[friendica.git] / src / Object / Log / ParsedLogLine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object\Log;
23
24 /**
25  * Parse a log line and offer some utility methods
26  */
27 class ParsedLogLine
28 {
29         const REGEXP = '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[^ ]*) (\w+) \[(\w*)\]: (.*)/';
30
31         /** @var int */
32         public $id = 0;
33
34         /** @var string */
35         public $date = null;
36
37         /** @var string */
38         public $context = null;
39
40         /** @var string */
41         public $level = null;
42
43         /** @var string */
44         public $message = null;
45
46         /** @var string */
47         public $data = null;
48
49         /** @var string */
50         public $source = null;
51
52         /** @var string */
53         public $logline;
54
55         /**
56          * @param int line id
57          * @param string $logline Source log line to parse
58          */
59         public function __construct(int $id, string $logline)
60         {
61                 $this->id = $id;
62                 $this->parse($logline);
63         }
64
65         private function parse($logline)
66         {
67                 $this->logline = $logline;
68
69                 // if data is empty is serialized as '[]'. To ease the parsing
70                 // let's replace it with '{""}'. It will be replaced by null later
71                 $logline = str_replace(' [] - {', ' {""} - {', $logline);
72
73
74                 if (strstr($logline, ' - {') === false) {
75                         // the log line is not well formed
76                         $jsonsource = null;
77                 } else {
78                         // here we hope that there will not be the string ' - {' inside the $jsonsource value
79                         list($logline, $jsonsource) = explode(' - {', $logline);
80                         $jsonsource                 = '{' . $jsonsource;
81                 }
82
83                 $jsondata = null;
84                 if (strpos($logline, '{"') > 0) {
85                         list($logline, $jsondata) = explode('{"', $logline, 2);
86
87                         $jsondata = '{"' . $jsondata;
88                 }
89
90                 preg_match(self::REGEXP, $logline, $matches);
91
92                 if (count($matches) == 0) {
93                         // regexp not matching
94                         $this->message = $this->logline;
95                 } else {
96                         $this->date    = $matches[1];
97                         $this->context = $matches[2];
98                         $this->level   = $matches[3];
99                         $this->message = $matches[4];
100                         $this->data    = $jsondata == '{""}' ? null : $jsondata;
101                         $this->source  = $jsonsource;
102                         $this->tryfixjson();
103                 }
104
105                 $this->message = trim($this->message);
106         }
107
108         /**
109          * Fix message / data split
110          *
111          * In log boundary between message and json data is not specified.
112          * If message  contains '{' the parser thinks there starts the json data.
113          * This method try to parse the found json and if it fails, search for next '{'
114          * in json data and retry
115          */
116         private function tryfixjson()
117         {
118                 if (is_null($this->data) || $this->data == '') {
119                         return;
120                 }
121                 try {
122                         $d = json_decode($this->data, true, 512, JSON_THROW_ON_ERROR);
123                 } catch (\JsonException $e) {
124                         // try to find next { in $str and move string before to 'message'
125
126                         $pos = strpos($this->data, '{', 1);
127                         if ($pos === false) {
128                                 $this->message .= $this->data;
129                                 $this->data = null;
130                                 return;
131                         }
132
133                         $this->message .= substr($this->data, 0, $pos);
134                         $this->data = substr($this->data, $pos);
135                         $this->tryfixjson();
136                 }
137         }
138
139         /**
140          * Return decoded `data` as array suitable for template
141          *
142          * @return array
143          */
144         public function getData()
145         {
146                 $data = json_decode($this->data, true);
147                 if ($data) {
148                         foreach ($data as $k => $v) {
149                                 $data[$k] = print_r($v, true);
150                         }
151                 }
152                 return $data;
153         }
154
155         /**
156          * Return decoded `source` as array suitable for template
157          *
158          * @return array
159          */
160         public function getSource()
161         {
162                 return json_decode($this->source, true);
163         }
164 }