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