We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



908
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 https://www.php-fig.org/psr/psr-3/