We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Logger Formatter usage of $context array

How is the Logger Formatter supposed to work with the optional $context array? I take it to mean you can set the format to include 'new' formatters, and you use the $context array to pass the value of the formatter.

Something like the below:

$formatter = $logger->getFormatter();

$formatter->setFormat(("[%date%][%type%] %message% - %my_token%");

$logger->info("this is a test", array("my_token" => "some value"));

But this doesn't seem to work. Any suggestions?

Thanks,

Ronald



907
Accepted
answer

After looking through the various C programs it turns out I was using incorrect delimiters. They are hard coded as '{' and '}'.

The following does work as expected:

$formatter = $logger->getFormatter();

$formatter->setFormat(("[%date%][%type%] %message% - {my_token}");

$logger->info("this is a test", array("my_token" => "some value"));

Would be nice if this was clearer from the documentation, or maybe using the same delimiters as all the other format strings, eg. %date%, etc.



26.3k

Phalcon is following PSR standards I think (for sure Phalcon has right to vote on PSR). Speaking about logging PSR-3 Phalcon is following I think 99% of the current standard. Good explanation of what you ask I think is at http://www.php-fig.org/psr/psr-3/