From 478ce8afb467d59eb3dce5ec4a9e9446b134aecf Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 16 Jan 2016 16:16:56 +0100 Subject: [PATCH] Logging: improve formatting to enable fail2ban parsing Fixes #436 Modifications: - remove calls to strval() on safe data - update the date format: 'Y/m/d_H:i:s' => 'Y/m/d H:i:s' Signed-off-by: VirtualTam --- application/Utils.php | 9 +++++++-- tests/UtilsTest.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/application/Utils.php b/application/Utils.php index a9a10ece..cb30595f 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -6,14 +6,19 @@ /** * Logs a message to a text file * + * The log format is compatible with fail2ban. + * * @param string $logFile where to write the logs * @param string $clientIp the client's remote IPv4/IPv6 address * @param string $message the message to log */ function logm($logFile, $clientIp, $message) { - $line = strval(date('Y/m/d_H:i:s')).' - '.$clientIp.' - '.strval($message).'\n'; - file_put_contents($logFile, $line, FILE_APPEND); + file_put_contents( + $logFile, + date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).'\n', + FILE_APPEND + ); } /** diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 869a9695..f3bb556e 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -22,7 +22,7 @@ class UtilsTest extends PHPUnit_Framework_TestCase protected static $testLogFile = 'tests.log'; // Expected log date format - protected static $dateFormat = 'Y/m/d_H:i:s'; + protected static $dateFormat = 'Y/m/d H:i:s'; /**