]> git.mxchange.org Git - friendica.git/commitdiff
Add tests for ParsedLog and fix parsing
authorfabrixxm <fabrix.xm@gmail.com>
Tue, 20 Jul 2021 16:03:20 +0000 (18:03 +0200)
committerfabrixxm <fabrix.xm@gmail.com>
Thu, 19 Aug 2021 12:55:33 +0000 (14:55 +0200)
src/Object/Log/ParsedLog.php
tests/src/Object/Log/ParsedLogTest.php [new file with mode: 0644]

index c33f4f95060d35aebe3d01f8ee15c0bf10986bdc..b1bc821125231b80498ec558487ed603d8a2f26e 100644 (file)
@@ -64,7 +64,13 @@ class ParsedLog
 
        private function parse($logline)
        {
-               list($logline, $jsonsource) = explode(' - ', $logline);
+               // if data is empty is serialized as '[]'. To ease the parsing
+               // let's replace it with '{""}'. It will be replaced by null later
+               $logline = str_replace(' [] - {', ' {""} - {', $logline);
+
+               // here we hope that there will not be the string ' - {' inside the $jsonsource value
+               list($logline, $jsonsource) = explode(' - {', $logline);
+               $jsonsource                 = '{' . $jsonsource;
 
                $jsondata = null;
 
@@ -73,13 +79,14 @@ class ParsedLog
 
                        $jsondata = '{"' . $jsondata;
                }
+
                preg_match(self::REGEXP, $logline, $matches);
 
                $this->date    = $matches[1];
                $this->context = $matches[2];
                $this->level   = $matches[3];
-               $this->message = $matches[4];
-               $this->data    = $jsondata;
+               $this->message = trim($matches[4]);
+               $this->data    = $jsondata == '{""}' ? null : $jsondata;
                $this->source  = $jsonsource;
                $this->try_fix_json();
 
@@ -96,7 +103,7 @@ class ParsedLog
         */
        private function try_fix_json()
        {
-               if (is_null($this->data) || $this->data == "") {
+               if (is_null($this->data) || $this->data == '') {
                        return;
                }
                try {
diff --git a/tests/src/Object/Log/ParsedLogTest.php b/tests/src/Object/Log/ParsedLogTest.php
new file mode 100644 (file)
index 0000000..ab98030
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\src\Object\Log;
+
+use Friendica\Object\Log\ParsedLog;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Log parser testing class
+ */
+class ParsedLogTest extends TestCase
+{
+       public static function do_log_line($logline, $expected_data)
+       {
+               $parsed = new ParsedLog(0, $logline);
+               foreach ($expected_data as $k => $v) {
+                       self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
+               }
+       }
+
+       /**
+        * test parsing a generic log line
+        */
+       public function testGenericLogLine()
+       {
+               self::do_log_line(
+                       '2021-05-24T15:40:01Z worker [NOTICE]: Spool file does does not start with "item-" {"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"} - {"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
+                       [
+                               'date'    => '2021-05-24T15:40:01Z',
+                               'context' => 'worker',
+                               'level'   => 'NOTICE',
+                               'message' => 'Spool file does does not start with "item-"',
+                               'data'    => '{"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"}',
+                               'source'  => '{"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
+                       ]
+               );
+       }
+
+       /**
+        * test parsing a log line with empty data
+        */
+       public function testEmptyDataLogLine()
+       {
+               self::do_log_line(
+                       '2021-05-24T15:23:58Z index [INFO]: No HTTP_SIGNATURE header [] - {"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
+                       [
+                               'date'    => '2021-05-24T15:23:58Z',
+                               'context' => 'index',
+                               'level'   => 'INFO',
+                               'message' => 'No HTTP_SIGNATURE header',
+                               'data'    => null,
+                               'source'  => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
+                       ]
+               );
+       }
+
+       /**
+        * test parsing a log line with various " - " in it
+        */
+       public function testTrickyDashLogLine()
+       {
+               self::do_log_line(
+                       '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"worker_id":"ece8fc8","worker_cmd":"Cron"} - {"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
+                       [
+                               'date'    => '2021-05-24T15:30:01Z',
+                               'context' => 'worker',
+                               'level'   => 'NOTICE',
+                               'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
+                               'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
+                               'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
+                       ]
+               );
+       }
+}