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.

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 29d ago

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.phalconphp.com/latest/en/logging

Or you could json_encode your array to procude a string:

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


224

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.phalconphp.com/latest/en/logging

Or you could json_encode your array to procude a string:

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


62.7k
Accepted
answer

You could create your own FormatterInterface

https://docs.phalconphp.com/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)



224

Thank you Lajos!

You could create your own FormatterInterface

https://docs.phalconphp.com/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)