diff options
Diffstat (limited to 'application/Utils.php')
-rw-r--r-- | application/Utils.php | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/application/Utils.php b/application/Utils.php index bcfda65c..7a9d2645 100644 --- a/application/Utils.php +++ b/application/Utils.php | |||
@@ -4,21 +4,23 @@ | |||
4 | */ | 4 | */ |
5 | 5 | ||
6 | /** | 6 | /** |
7 | * Logs a message to a text file | 7 | * Format log using provided data. |
8 | * | 8 | * |
9 | * The log format is compatible with fail2ban. | 9 | * @param string $message the message to log |
10 | * @param string|null $clientIp the client's remote IPv4/IPv6 address | ||
10 | * | 11 | * |
11 | * @param string $logFile where to write the logs | 12 | * @return string Formatted message to log |
12 | * @param string $clientIp the client's remote IPv4/IPv6 address | ||
13 | * @param string $message the message to log | ||
14 | */ | 13 | */ |
15 | function logm($logFile, $clientIp, $message) | 14 | function format_log(string $message, string $clientIp = null): string |
16 | { | 15 | { |
17 | file_put_contents( | 16 | $out = $message; |
18 | $logFile, | 17 | |
19 | date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL, | 18 | if (!empty($clientIp)) { |
20 | FILE_APPEND | 19 | // Note: we keep the first dash to avoid breaking fail2ban configs |
21 | ); | 20 | $out = '- ' . $clientIp . ' - ' . $out; |
21 | } | ||
22 | |||
23 | return $out; | ||
22 | } | 24 | } |
23 | 25 | ||
24 | /** | 26 | /** |