From ae12fdeaecfeaa5ef190bdace661c7ec0d9ae526 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 13 May 2014 14:03:27 +0200 Subject: [PATCH] Changed logger so it avoids blank lines --- src/Api/Jobs/LogJobs/GetLogJob.php | 1 + src/Logger.php | 8 +++++++- tests/JobTests/AddPostJobTest.php | 2 +- tests/JobTests/AddUserJobTest.php | 2 +- tests/JobTests/EditPostJobTest.php | 2 +- tests/JobTests/EditUserJobTest.php | 2 +- tests/JobTests/FlagPostJobTest.php | 2 +- tests/JobTests/FlagUserJobTest.php | 2 +- 8 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Api/Jobs/LogJobs/GetLogJob.php b/src/Api/Jobs/LogJobs/GetLogJob.php index d8425ed3..04e26cf8 100644 --- a/src/Api/Jobs/LogJobs/GetLogJob.php +++ b/src/Api/Jobs/LogJobs/GetLogJob.php @@ -30,6 +30,7 @@ class GetLogJob extends AbstractJob implements IPagedJob //load lines $lines = file_get_contents($path); + $lines = trim($lines); $lines = explode(PHP_EOL, str_replace(["\r", "\n"], PHP_EOL, $lines)); $lines = array_reverse($lines); diff --git a/src/Logger.php b/src/Logger.php index cec6db55..465eba33 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -27,13 +27,19 @@ class Logger if (empty(self::$buffer)) return; + $sep = file_exists(self::$path) + ? PHP_EOL + : ''; $fh = fopen(self::$path, 'ab'); if (!$fh) throw new SimpleException('Cannot write to log files'); if (flock($fh, LOCK_EX)) { foreach (self::$buffer as $logEvent) - fwrite($fh, $logEvent->getFullText() . PHP_EOL); + { + fwrite($fh, $sep . $logEvent->getFullText()); + $sep = PHP_EOL; + } fflush($fh); flock($fh, LOCK_UN); fclose($fh); diff --git a/tests/JobTests/AddPostJobTest.php b/tests/JobTests/AddPostJobTest.php index 16b4e339..ebd59152 100644 --- a/tests/JobTests/AddPostJobTest.php +++ b/tests/JobTests/AddPostJobTest.php @@ -143,7 +143,7 @@ class AddPostJobTest extends AbstractTest $logPath = Logger::getLogPath(); $x = file_get_contents($logPath); - $lines = array_filter(explode("\n", $x)); + $lines = explode("\n", $x); $this->assert->areEqual(1, count($lines)); } diff --git a/tests/JobTests/AddUserJobTest.php b/tests/JobTests/AddUserJobTest.php index 055afb73..8bbbac42 100644 --- a/tests/JobTests/AddUserJobTest.php +++ b/tests/JobTests/AddUserJobTest.php @@ -300,7 +300,7 @@ class AddUserJobTest extends AbstractTest $logPath = Logger::getLogPath(); $x = file_get_contents($logPath); - $lines = array_filter(explode("\n", $x)); + $lines = explode("\n", $x); $this->assert->areEqual(2, count($lines)); } } diff --git a/tests/JobTests/EditPostJobTest.php b/tests/JobTests/EditPostJobTest.php index 1e85b351..94a6fbf7 100644 --- a/tests/JobTests/EditPostJobTest.php +++ b/tests/JobTests/EditPostJobTest.php @@ -54,7 +54,7 @@ class EditPostJobTest extends AbstractTest $logPath = Logger::getLogPath(); $x = file_get_contents($logPath); - $lines = array_filter(explode("\n", $x)); + $lines = explode("\n", $x); $this->assert->areEqual(3, count($lines)); } } diff --git a/tests/JobTests/EditUserJobTest.php b/tests/JobTests/EditUserJobTest.php index 1f243c22..816f1732 100644 --- a/tests/JobTests/EditUserJobTest.php +++ b/tests/JobTests/EditUserJobTest.php @@ -52,7 +52,7 @@ class EditUserJobTest extends AbstractTest $logPath = Logger::getLogPath(); $x = file_get_contents($logPath); - $lines = array_filter(explode("\n", $x)); + $lines = explode("\n", $x); $this->assert->areEqual(2, count($lines)); } diff --git a/tests/JobTests/FlagPostJobTest.php b/tests/JobTests/FlagPostJobTest.php index 0587db75..33ad9181 100644 --- a/tests/JobTests/FlagPostJobTest.php +++ b/tests/JobTests/FlagPostJobTest.php @@ -17,7 +17,7 @@ class FlagPostJobTest extends AbstractTest $logPath = Logger::getLogPath(); $logs = file_get_contents($logPath); - $logs = array_filter(explode("\n", $logs)); + $logs = explode("\n", $logs); $this->assert->areEqual(1, count($logs)); $this->assert->isTrue(strpos($logs[0], 'flagged @' . $post->getId() . ' for moderator attention') !== false); } diff --git a/tests/JobTests/FlagUserJobTest.php b/tests/JobTests/FlagUserJobTest.php index e1385f34..eb11cd0a 100644 --- a/tests/JobTests/FlagUserJobTest.php +++ b/tests/JobTests/FlagUserJobTest.php @@ -17,7 +17,7 @@ class FlagUserJobTest extends AbstractTest $logPath = Logger::getLogPath(); $logs = file_get_contents($logPath); - $logs = array_filter(explode("\n", $logs)); + $logs = explode("\n", $logs); $this->assert->areEqual(1, count($logs)); $this->assert->isTrue(strpos($logs[0], 'flagged +' . $user->getName() . ' for moderator attention') !== false); }