Logger\Formatter\Line::setDateFormat does not support microseconds
when i use
$logFormater = new \Phalcon\Logger\Formatter\Line;
$logFormater->setFormat('[%date%][%type%] %message%');
$logFormater->setDateFormat('H:i:s.u');
i only get 000000 for the microseconds (look at the ".u" in the setDateFormat) i get something like this: [13:51:10.000000]
PHP-Documentation at http://de2.php.net/manual/en/function.date.php says: Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds.
Aha, is the phalcon-method setDateFormat() only a wrapper to the native Date()-function?
I want to get the microseconds in the log at the "correct" place, what can do?
Swen
i solved it with a separate class which inherits from \Phalcon\Logger\Formatter\Line
class LogFormatter extends \Phalcon\Logger\Formatter\Line {
public function format($message, $type, $timestamp, $context ) {
$dateTime = new \DateTime;
$dateTime->setTimestamp($timestamp);
$dateFormat = str_replace('u', floor((microtime(true) - $timestamp) * 1000), $this->getDateFormat());
$formated = str_replace('%date%', $dateTime->format( $dateFormat ), $this->getFormat() );
$formated = str_replace('%message%', $message, $formated);
return str_replace('%type%', $this->getTypeString($type), $formated) . "\n";
}
}
Strange thing is, that toward the php documentation DateTime::format() does not give me the micreconds! So i made a workaraound.
All in all it works fine for me.
It would be nice to see a millisecond in the logs out of the box without the extension adapter. This can be easily done by simply adding the "v" parameter to the setDateFormat method if working with the date is implemented through the DateTime inside the adapter.
@stdnk Did you try by setting the date format when constructing the formatter? Internally it uses the date() function so something like this should work
date("U.u")
and as a result you could do this:
$line = new Line("[%date%][%type%] %message%", "U.u");
Nevermind I just noticed your findings in the first message.
This should be changed to use the object vs the function.
This has been addressed https://github.com/phalcon/cphalcon/pull/14648/
@swen100 @stdnk Thank you for bringing this to life.