From 759e60e82efbe102f6275e11db327e616518beb7 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sun, 7 Jan 2024 20:48:15 +0100 Subject: [PATCH] Remove strftime (deprecated in php 8.1) Add possibility to configure timeFormatter --- Log.php | 18 ++++++++---------- Log/console.php | 2 +- Log/display.php | 2 +- Log/error_log.php | 2 +- Log/file.php | 2 +- Log/firebug.php | 2 +- Log/mail.php | 2 +- Log/sqlite.php | 2 +- Log/syslog.php | 2 +- Log/win.php | 2 +- docs/guide.txt | 28 ++++++++++++++-------------- 11 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Log.php b/Log.php index 2071d5c..34314b7 100644 --- a/Log.php +++ b/Log.php @@ -37,7 +37,7 @@ */ class Log { - const TIME_FORMAT = 'M d H:i:s'; + const DEFAULT_TIME_FORMAT = 'M d H:i:s'; /** * Indicates whether or not the log can been opened / connected. @@ -835,25 +835,23 @@ public function getIdent() } /** - * Function to format time in specified format, which will be used in log record - * By default will be used format self::TIME_FORMAT + * Function to format unix timestamp in specified format, which will be used in log record + * By default will be used format self::DEFAULT_TIME_FORMAT * timeFormatter function will be used if it is set * - * @param string $timeFormat - * @param int $time - * @param callable|null $timeFormatter + * @param int $time unix timestamp + * @param string $timeFormat specified format, which will be used in log record + * @param callable|null $timeFormatter function which will be used to format time * @return string */ - public function timeFormat($timeFormat, $time, callable $timeFormatter = null) + public function formatTime($time, $timeFormat = self::DEFAULT_TIME_FORMAT, callable $timeFormatter = null) { - $timeFormat = empty($timeFormat) ? self::TIME_FORMAT : $timeFormat; - if (!is_null($timeFormatter) && is_callable($timeFormatter)) { return call_user_func($timeFormatter, $timeFormat, $time); } if (strpos($timeFormat, '%') !== false) { - trigger_error('Using strftime style formatting is deprecated', E_USER_WARNING); + trigger_error('Using strftime-style formatting is deprecated', E_USER_WARNING); $timeFormat = $this->convertStrftimeFormatConverter($timeFormat); } diff --git a/Log/console.php b/Log/console.php index 157e74d..5232474 100644 --- a/Log/console.php +++ b/Log/console.php @@ -198,7 +198,7 @@ public function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat,$this->timeFormatter), $priority, $message) . "\n"; /* diff --git a/Log/display.php b/Log/display.php index 3a550bb..b9ebfe8 100644 --- a/Log/display.php +++ b/Log/display.php @@ -170,7 +170,7 @@ public function log($message, $priority = null) /* Build and output the complete log line. */ echo $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message); diff --git a/Log/error_log.php b/Log/error_log.php index 1d56c09..cc1bc9f 100644 --- a/Log/error_log.php +++ b/Log/error_log.php @@ -145,7 +145,7 @@ public function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message); /* Pass the log line and parameters to the error_log() function. */ diff --git a/Log/file.php b/Log/file.php index f451b86..cd2defe 100644 --- a/Log/file.php +++ b/Log/file.php @@ -282,7 +282,7 @@ public function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message) . $this->_eol; /* If locking is enabled, acquire an exclusive lock on the file. */ diff --git a/Log/firebug.php b/Log/firebug.php index f784969..910cc9d 100644 --- a/Log/firebug.php +++ b/Log/firebug.php @@ -183,7 +183,7 @@ public function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message); diff --git a/Log/mail.php b/Log/mail.php index 2079896..831b6d9 100644 --- a/Log/mail.php +++ b/Log/mail.php @@ -274,7 +274,7 @@ public function log($message, $priority = null) /* Append the string containing the complete log line. */ $this->message .= $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message) . "\r\n"; $this->shouldSend = true; diff --git a/Log/sqlite.php b/Log/sqlite.php index a1b49b9..53dbd60 100644 --- a/Log/sqlite.php +++ b/Log/sqlite.php @@ -172,7 +172,7 @@ public function log($message, $priority = null) $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' . "VALUES ('%s', '%s', %d, '%s')", $this->table, - $this->timeFormat('Y-m-d H:i:s', time(), $this->timeFormatter), + $this->formatTime(time(), 'Y-m-d H:i:s', $this->timeFormatter), sqlite_escape_string($this->ident), $priority, sqlite_escape_string($message)); diff --git a/Log/syslog.php b/Log/syslog.php index 02b4a86..0d997d3 100644 --- a/Log/syslog.php +++ b/Log/syslog.php @@ -180,7 +180,7 @@ public function log($message, $priority = null) /* Apply the configured line format to the message string. */ $message = $this->format($this->lineFormat, - $this->timeFormat($this->timeFormat, time(), $this->timeFormatter), + $this->formatTime(time(), $this->timeFormat, $this->timeFormatter), $priority, $message); /* Split the string into parts based on our maximum length setting. */ diff --git a/Log/win.php b/Log/win.php index b7c4df7..f0c9125 100644 --- a/Log/win.php +++ b/Log/win.php @@ -258,7 +258,7 @@ public function log($message, $priority = null) /* Build the output line that contains the log entry row. */ $line = ''; $line .= sprintf('%s.%s', - $this->timeFormat('H:i:s', $sec), substr($usec, 2, 2)); + $this->formatTime($sec, 'H:i:s'), substr($usec, 2, 2)); if (!empty($this->ident)) { $line .= '' . $this->ident . ''; } diff --git a/docs/guide.txt b/docs/guide.txt index 0f4c57c..f9c43f6 100644 --- a/docs/guide.txt +++ b/docs/guide.txt @@ -321,8 +321,8 @@ Configuration | ``lineFormat`` | String | ``%1$s %2$s | `Log line format`_ | | | | [%3$s] %4$s`` | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -355,8 +355,8 @@ Configuration | ``lineFormat`` | String | ``%3$s:| `Log line format`_ | | | | %4$s`` | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -417,8 +417,8 @@ Configuration | ``lineFormat`` | String | ``%2$s: %4$s``| `Log line format`_ | | | | | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -506,8 +506,8 @@ Configuration | ``lineFormat`` | String | ``%1$s %2$s | `Log line format`_ | | | | [%3$s] %4$s`` | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -546,8 +546,8 @@ Configuration | ``lineFormat`` | String | ``%2$s [%3$s] | `Log line format`_ | | | | %4$s`` | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -595,8 +595,8 @@ Configuration | ``lineFormat`` | String | ``%1$s %2$s | `Log line format`_ | | | | [%3$s] %4$s`` | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') | @@ -877,8 +877,8 @@ Configuration | ``lineFormat`` | String | ``%4$s`` | `Log line format`_ | | | | | specification. | +-------------------+-----------+---------------+---------------------------+ -| ``timeFormat`` | String | ``%b %d | Time stamp format | -| | | %H:%M:%S`` | (for date_). | +| ``timeFormat`` | String | ``M d H:i:s`` | Time stamp format | +| | | | (for date_). | +-------------------+-----------+---------------+---------------------------+ | ``timeFormatter`` | Callable | null | Time formatter example | | | | | fn() => date('H:i:s') |