How can i get microseconds with Logger?

this is my code:

$logFormater = new \Phalcon\Logger\Formatter\Line;
$logFormater->setFormat('[%date%][%type%] %message%');

but i get only 000000 for the microseconds

example: [13:51:10.000000]

PHP-Documentation at says: Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds.

is there a parameter i can set to get this value? Or do i have to implement it on my own?


edited Oct '14

i can calculate the microseconds with this

( microtime(true) - time() ) * 1000

but how do i get it into the logger at the "correct" place, so i get it like


i thought, i got it, but the following code did not work because i only get access to the %message%-part.

i wanted to replace "%micro%" with the microseconds

$Logger = new \Phalcon\Logger\Adapter\File($fileName);
$logFormater = new \Phalcon\Logger\Formatter\Line;
$logFormater->setFormat('[%date%.%micro%][%type%] %message%');
$message = str_replace('%micro%', (time() - microtime(true)) * 1000, $message);
$Logger->log($message, $priority);

edited Oct '14

Doesn't look like you can override this one :-/

If you have a look at the source code at you see that the function phalcon_date is called with a date, a date format and a timestamp.

phalcon_date(date, date_format, timestamp TSRMLS_CC);

That function is in string.c in the kernel:

void phalcon_date(zval *return_value, zval *format, zval *timestamp TSRMLS_DC)
    long int ts;
    zval copy;
    int use_copy = 0;
    char *formatted;
    if (unlikely(Z_TYPE_P(format) != IS_STRING)) {
        zend_make_printable_zval(format, &copy, &use_copy);
        if (use_copy) {
            format = ©
    ts = (timestamp) ? phalcon_get_intval(timestamp) : time(NULL);
    formatted = php_format_date(Z_STRVAL_P(format), Z_STRLEN_P(format), ts, 1 TSRMLS_CC);
    ZVAL_STRING(return_value, formatted, 0);
    if (unlikely(use_copy)) {

It's the phalcongetintval function that is responsible for returning you the timestamp. Unfortunately I cannot find where the phpformatdate function is defined (can't find the ext/date/php_date.c file), which is the one that prints out the string to your log. But then still, I see no possibility to override the timestamp formatting. You could write it yourself or submit a feature request.

Best regards, Sven

BTW: how do you format an answer here to get those nice black backgrounds?

edited Oct '14

@sventunus Hi, i use three backticks followed by text "php".

Replace the following singlequotes with: `




There was a markdown help, but the link is suddenly dead.



Cool, thanks 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;
        $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.


Nice fix! Did some deeper digging too BTW, and that phpformatdate function is just in PHP's source code:

So nothing wrong with the Phalcon code, it should just work if you pass in the correct format. That means you have probably discovered a bug in PHP :-) Will look into it more, am fascinated by your discovery.


It is a bug indeed (well, a feature request maybe), and an old one too! Check