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

How can I output an array with logger?

Hello, I want to output and array with logger and I can't see a solution into doc or something similar. For example i want.

$example = array('one', 'two');

$this->logger->info($example);

Any idea?

Thx!

edited Feb '19

You can pass in an array parameter as the second argument:

// You can also pass context parameters like this
$logger->log(
    'This is a {message}', 
    [ 
        'message' => 'parameter' 
    ]
);

https://docs.phalcon.io/latest/en/logging

Or you could json_encode your array to procude a string:

$logger->log(json_encode(['some'=>'array']));


3.1k

With json_encode is OK, but How can I do that into all loggers without put it in all loggers json_encode? I mean, the parent or something like that.

You can pass in an array parameter as the second argument:

// You can also pass context parameters like this
$logger->log(
   'This is a {message}', 
   [ 
       'message' => 'parameter' 
   ]
);

https://docs.phalcon.io/latest/en/logging

Or you could json_encode your array to procude a string:

$logger->log(json_encode(['some'=>'array']));


77.7k
Accepted
answer

You could create your own FormatterInterface

https://docs.phalcon.io/latest/en/api/Phalcon_Logger_Formatter

https://github.com/phalcon/cphalcon/blob/master/phalcon/logger/formatter/json.zep

(the last link is an example of encoding ALL fields as json, not just the context array, you would have to modify it to suit your needs)



3.1k

Thank you Lajos!

You could create your own FormatterInterface

https://docs.phalcon.io/latest/en/api/Phalcon_Logger_Formatter

https://github.com/phalcon/cphalcon/blob/master/phalcon/logger/formatter/json.zep

(the last link is an example of encoding ALL fields as json, not just the context array, you would have to modify it to suit your needs)