Skip to content

Commit

Permalink
Remove strftime (deprecated in php 8.1) (#25)
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
sveneld authored Jan 7, 2024
1 parent c137ad5 commit 99e96b0
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 59 deletions.
84 changes: 76 additions & 8 deletions Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
class Log
{
const DEFAULT_TIME_FORMAT = 'M d H:i:s';

/**
* Indicates whether or not the log can been opened / connected.
*
Expand Down Expand Up @@ -437,7 +439,7 @@ protected function extractMessage($message)
*
* @since Log 1.9.4
*/
public function _getBacktraceVars($depth)
private function getBacktraceVars($depth)
{
/* Start by generating a backtrace from the current call (here). */
$bt = debug_backtrace();
Expand Down Expand Up @@ -526,7 +528,7 @@ protected function format($format, $timestamp, $priority, $message)
if (preg_match('/%[5678]/', $format)) {
/* Plus 2 to account for our internal function calls. */
$d = $this->backtrace_depth + 2;
list($file, $line, $func, $class) = $this->_getBacktraceVars($d);
list($file, $line, $func, $class) = $this->getBacktraceVars($d);
}

/*
Expand Down Expand Up @@ -753,7 +755,7 @@ public function attach(&$observer)
return false;
}

$this->listeners[$observer->_id] = &$observer;
$this->listeners[$observer->getId()] = &$observer;

return true;
}
Expand All @@ -771,11 +773,11 @@ public function attach(&$observer)
public function detach($observer)
{
if (!is_a($observer, 'Log_observer') ||
!isset($this->listeners[$observer->_id])) {
!isset($this->listeners[$observer->getId()])) {
return false;
}

unset($this->listeners[$observer->_id]);
unset($this->listeners[$observer->getId()]);

return true;
}
Expand All @@ -789,9 +791,12 @@ public function detach($observer)
*/
protected function announce($event)
{
foreach ($this->listeners as $id => $listener) {
if ($event['priority'] <= $this->listeners[$id]->_priority) {
$this->listeners[$id]->notify($event);
/**
* @var Log_observer $listener
*/
foreach ($this->listeners as $listener) {
if ($event['priority'] <= $listener->getPriority()) {
$listener->notify($event);
}
}
}
Expand Down Expand Up @@ -831,4 +836,67 @@ public function getIdent()
{
return $this->ident;
}

/**
* 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 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
*/
protected function formatTime($time, $timeFormat = self::DEFAULT_TIME_FORMAT, callable $timeFormatter = null)
{
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);
$timeFormat = $this->convertStrftimeFormatConverter($timeFormat);
}

return date($timeFormat, $time);
}

/**-
* Function to convert strftime format to format acceptable by date function
*
* @param string $timeFormat
* @return string
*/
private function convertStrftimeFormatConverter($timeFormat)
{
$strf_syntax = [
'%O', '%d', '%a', '%e', '%A', '%u', '%w', '%j',
'%V',
'%B', '%m', '%b', '%-m',
'%G', '%Y', '%y',
'%P', '%p', '%l', '%I', '%H', '%M', '%S',
'%z', '%Z',
'%s'
];

// http://php.net/manual/en/function.date.php
$date_syntax = [
'S', 'd', 'D', 'j', 'l', 'N', 'w', 'z',
'W',
'F', 'm', 'M', 'n',
'o', 'Y', 'y',
'a', 'A', 'g', 'h', 'H', 'i', 's',
'O', 'T',
'U'
];

$pattern = array_map(
function ($s) {
return '/(?<!\\\\|\%)' . $s . '/';
},
$strf_syntax
);

return preg_replace($pattern, $date_syntax, $timeFormat);
}
}
18 changes: 13 additions & 5 deletions Log/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ class Log_console extends Log
private $lineFormat = '%1$s %2$s [%3$s] %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* current locale.
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* Constructs a new Log_console object.
Expand Down Expand Up @@ -94,6 +98,10 @@ public function __construct($name, $ident = '', $conf = array(),
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}

/*
* If output buffering has been requested, we need to register a
* shutdown function that will dump the buffer upon termination.
Expand Down Expand Up @@ -190,7 +198,7 @@ public function log($message, $priority = null)

/* Build the string containing the complete log line. */
$line = $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat,$this->timeFormatter),
$priority, $message) . "\n";

/*
Expand Down
17 changes: 13 additions & 4 deletions Log/display.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ class Log_display extends Log
private $lineFormat = '<b>%3$s</b>: %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
* current locale.
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* Flag indicating whether raw message text should be passed directly to
Expand Down Expand Up @@ -99,6 +104,10 @@ public function __construct($name = '', $ident = '', $conf = array(),
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}

/* Message text conversion can be disabled. */
if (isset($conf['rawText'])) {
$this->rawText = $conf['rawText'];
Expand Down Expand Up @@ -161,7 +170,7 @@ public function log($message, $priority = null)

/* Build and output the complete log line. */
echo $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat, $this->timeFormatter),
$priority,
$message);

Expand Down
17 changes: 13 additions & 4 deletions Log/error_log.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ class Log_error_log extends Log
private $lineFormat = '%2$s: %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
* current locale.
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* Constructs a new Log_error_log object.
Expand Down Expand Up @@ -84,6 +89,10 @@ public function __construct($name, $ident = '', $conf = array(),
if (!empty($conf['timeFormat'])) {
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}
}

/**
Expand Down Expand Up @@ -136,7 +145,7 @@ public function log($message, $priority = null)

/* Build the string containing the complete log line. */
$line = $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat, $this->timeFormatter),
$priority, $message);

/* Pass the log line and parameters to the error_log() function. */
Expand Down
17 changes: 13 additions & 4 deletions Log/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ class Log_file extends Log
private $lineFormat = '%1$s %2$s [%3$s] %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
* current locale.
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* String containing the end-on-line character sequence.
Expand Down Expand Up @@ -127,6 +132,10 @@ public function __construct($name, $ident = '', $conf = array(),
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}

if (!empty($conf['eol'])) {
$this->eol = $conf['eol'];
} else {
Expand Down Expand Up @@ -273,7 +282,7 @@ public function log($message, $priority = null)

/* Build the string containing the complete log line. */
$line = $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat, $this->timeFormatter),
$priority, $message) . $this->eol;

/* If locking is enabled, acquire an exclusive lock on the file. */
Expand Down
18 changes: 13 additions & 5 deletions Log/firebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ class Log_firebug extends Log
private $lineFormat = '%2$s [%3$s] %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* current locale.
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
*
* Note! Default lineFormat of this driver does not display time.
*
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* Mapping of log priorities to Firebug methods.
Expand Down Expand Up @@ -95,6 +99,10 @@ public function __construct($name = '', $ident = 'PHP', $conf = array(),
if (!empty($conf['timeFormat'])) {
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}
}

/**
Expand Down Expand Up @@ -175,7 +183,7 @@ public function log($message, $priority = null)

/* Build the string containing the complete log line. */
$line = $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat, $this->timeFormatter),
$priority,
$message);

Expand Down
17 changes: 13 additions & 4 deletions Log/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ class Log_mail extends Log
private $lineFormat = '%1$s %2$s [%3$s] %4$s';

/**
* String containing the timestamp format. It will be passed directly to
* strftime(). Note that the timestamp string will generated using the
* String containing the timestamp format. It will be passed to date().
* If timeFormatter configured, it will be used.
* current locale.
* @var string
*/
private $timeFormat = '%b %d %H:%M:%S';
private $timeFormat = 'M d H:i:s';

/**
* @var callable
*/
private $timeFormatter;

/**
* String holding the mail message body.
Expand Down Expand Up @@ -137,6 +142,10 @@ public function __construct($name, $ident = '', $conf = array(),
$this->timeFormat = $conf['timeFormat'];
}

if (!empty($conf['timeFormatter'])) {
$this->timeFormatter = $conf['timeFormatter'];
}

if (!empty($conf['mailBackend'])) {
$this->mailBackend = $conf['mailBackend'];
}
Expand Down Expand Up @@ -265,7 +274,7 @@ public function log($message, $priority = null)

/* Append the string containing the complete log line. */
$this->message .= $this->format($this->lineFormat,
strftime($this->timeFormat),
$this->formatTime(time(), $this->timeFormat, $this->timeFormatter),
$priority, $message) . "\r\n";
$this->shouldSend = true;

Expand Down
Loading

0 comments on commit 99e96b0

Please sign in to comment.