]> git.mxchange.org Git - friendica.git/blob - src/Object/Log/ParsedLog.php
b1bc821125231b80498ec558487ed603d8a2f26e
[friendica.git] / src / Object / Log / ParsedLog.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2021, Friendica
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 ParsedLog
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                 // if data is empty is serialized as '[]'. To ease the parsing
68                 // let's replace it with '{""}'. It will be replaced by null later
69                 $logline = str_replace(' [] - {', ' {""} - {', $logline);
70
71                 // here we hope that there will not be the string ' - {' inside the $jsonsource value
72                 list($logline, $jsonsource) = explode(' - {', $logline);
73                 $jsonsource                 = '{' . $jsonsource;
74
75                 $jsondata = null;
76
77                 if (strpos($logline, '{"') > 0) {
78                         list($logline, $jsondata) = explode('{"', $logline, 2);
79
80                         $jsondata = '{"' . $jsondata;
81                 }
82
83                 preg_match(self::REGEXP, $logline, $matches);
84
85                 $this->date    = $matches[1];
86                 $this->context = $matches[2];
87                 $this->level   = $matches[3];
88                 $this->message = trim($matches[4]);
89                 $this->data    = $jsondata == '{""}' ? null : $jsondata;
90                 $this->source  = $jsonsource;
91                 $this->try_fix_json();
92
93                 $this->logline = $logline;
94         }
95
96         /**
97          * Fix message / data split
98          *
99          * In log boundary between message and json data is not specified.
100          * If message  contains '{' the parser thinks there starts the json data.
101          * This method try to parse the found json and if it fails, search for next '{'
102          * in json data and retry
103          */
104         private function try_fix_json()
105         {
106                 if (is_null($this->data) || $this->data == '') {
107                         return;
108                 }
109                 try {
110                         $d = json_decode($this->data, true, 512, JSON_THROW_ON_ERROR);
111                 } catch (\JsonException $e) {
112                         // try to find next { in $str and move string before to 'message'
113
114                         $pos = strpos($this->data, '{', 1);
115
116                         $this->message .= substr($this->data, 0, $pos);
117                         $this->data = substr($this->data, $pos);
118                         $this->try_fix_json();
119                 }
120         }
121
122         /**
123          * Return decoded `data` as array suitable for template
124          *
125          * @return array
126          */
127         public function get_data()
128         {
129                 $data = json_decode($this->data, true);
130                 if ($data) {
131                         foreach ($data as $k => $v) {
132                                 $data[$k] = print_r($v, true);
133                         }
134                 }
135                 return $data;
136         }
137
138         /**
139          * Return decoded `source` as array suitable for template
140          *
141          * @return array
142          */
143         public function get_source()
144         {
145                 return json_decode($this->source, true);
146         }
147 }