Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger\Formatter\Line::setDateFormat does not support microseconds #2893

Closed
swen100 opened this issue Oct 10, 2014 · 5 comments
Closed

Logger\Formatter\Line::setDateFormat does not support microseconds #2893

swen100 opened this issue Oct 10, 2014 · 5 comments
Labels
enhancement Enhancement to the framework

Comments

@swen100
Copy link

swen100 commented Oct 10, 2014

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

@swen100 swen100 changed the title Logger\Formatter\Line does not support microseconds Logger\Formatter\Line::setDateFormat does not support microseconds Oct 10, 2014
@swen100
Copy link
Author

swen100 commented Oct 10, 2014

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.

@swen100 swen100 closed this as completed Oct 13, 2014
@stdnk
Copy link

stdnk commented Dec 23, 2019

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.

@niden
Copy link
Member

niden commented Dec 23, 2019

@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");

@niden
Copy link
Member

niden commented Dec 23, 2019

Nevermind I just noticed your findings in the first message.

This should be changed to use the object vs the function.

@niden niden reopened this Dec 23, 2019
@niden niden added 4.0 enhancement Enhancement to the framework labels Dec 23, 2019
@niden niden mentioned this issue Dec 23, 2019
5 tasks
niden added a commit that referenced this issue Dec 23, 2019
* niden-T2893-logger-date-format:
  [#2893] - Updated changelog
  [#2893] - Updated changelog
  [#2893] - Corrected typo
  [#2893] - Minor correction
  [#2893] - Added tests
  [#2893] - Restructured logger to use DateTimeImmutable to handle microseconds
@niden
Copy link
Member

niden commented Dec 23, 2019

This has been addressed #14648

@swen100 @stdnk Thank you for bringing this to life.

@niden niden closed this as completed Dec 23, 2019
niden added a commit that referenced this issue Jan 21, 2020
niden added a commit that referenced this issue Jan 21, 2020
niden added a commit that referenced this issue Feb 4, 2020
niden added a commit that referenced this issue Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to the framework
Projects
None yet
Development

No branches or pull requests

3 participants