diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-20 11:47:07 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-20 11:47:07 +0200 |
commit | b38a1b0209f546d4824a0db81a34c4e30fcdebaf (patch) | |
tree | 0f812cd69bfc0ba654ce0d4b832850b41cc658fa /application/Utils.php | |
parent | ca5e98da4867f720dc863dac55cd1fa2360068e7 (diff) | |
download | Shaarli-b38a1b0209f546d4824a0db81a34c4e30fcdebaf.tar.gz Shaarli-b38a1b0209f546d4824a0db81a34c4e30fcdebaf.tar.zst Shaarli-b38a1b0209f546d4824a0db81a34c4e30fcdebaf.zip |
Use PSR-3 logger for login attempts
Fixes #1122
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 | /** |