Skip to content

Commit

Permalink
Remove strftime (deprecated in php 8.1)
Browse files Browse the repository at this point in the history
Add possibility to configure timeFormatter
  • Loading branch information
sveneld committed Jan 7, 2024
1 parent 524a61c commit 759e60e
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 33 deletions.
18 changes: 8 additions & 10 deletions Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Log/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/*
Expand Down
2 changes: 1 addition & 1 deletion Log/display.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Log/error_log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion Log/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion Log/firebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Log/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Log/sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion Log/syslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion Log/win.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function log($message, $priority = null)
/* Build the output line that contains the log entry row. */
$line = '<tr>';
$line .= sprintf('<td>%s.%s</td>',
$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 .= '<td>' . $this->ident . '</td>';
}
Expand Down
28 changes: 14 additions & 14 deletions docs/guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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') |
Expand Down Expand Up @@ -355,8 +355,8 @@ Configuration
| ``lineFormat`` | String | ``<b>%3$s</b>:| `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') |
Expand Down Expand Up @@ -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') |
Expand Down Expand Up @@ -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') |
Expand Down Expand Up @@ -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') |
Expand Down Expand Up @@ -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') |
Expand Down Expand Up @@ -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') |
Expand Down

0 comments on commit 759e60e

Please sign in to comment.