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

Comments

@swen100
Copy link

@swen100 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

This comment has been minimized.

Copy link
Author

@swen100 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 Oct 13, 2014
@stdnk

This comment has been minimized.

Copy link

@stdnk 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

This comment has been minimized.

Copy link
Member

@niden 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

This comment has been minimized.

Copy link
Member

@niden 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 added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
…le microseconds
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
@niden niden mentioned this issue Dec 23, 2019
4 of 5 tasks complete
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
…le microseconds
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
…om/niden/cphalcon into niden-T2893-logger-date-format

* 'T2893-logger-date-format' of https://github.com/niden/cphalcon:
  [phalcon#14643] - Updated changelog
  [phalcon#14643] - Corrected the setcookie parameters
  [phalcon#2893] - Updated changelog
  [phalcon#2893] - Updated changelog
  [phalcon#2893] - Corrected typo
  [phalcon#2893] - Minor correction
  [phalcon#2893] - Added tests
  [phalcon#2893] - Restructured logger to use DateTimeImmutable to handle microseconds
  add mysql column definitions tests
  Fixed Mysql column definition
niden added a commit to niden/cphalcon that referenced this issue Dec 23, 2019
* niden-T2893-logger-date-format:
  [phalcon#14643] - Updated changelog
  [phalcon#14643] - Corrected the setcookie parameters
  [phalcon#2893] - Updated changelog
  [phalcon#2893] - Updated changelog
  [phalcon#2893] - Corrected typo
  [phalcon#2893] - Minor correction
  [phalcon#2893] - Added tests
  [phalcon#2893] - Restructured logger to use DateTimeImmutable to handle microseconds
  add mysql column definitions tests
  Fixed Mysql column definition
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

This comment has been minimized.

Copy link
Member

@niden niden commented Dec 23, 2019

This has been addressed #14648

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

@niden niden closed this Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.