phpmonolog

Monolog Array log TypeError


I am successfully using Monolog in many PHP projects and I frequently use the second argument to Monolog\\Logger to log arrays:

$a = array("lemon", "banana", "apple");
$log->debug("Array: ", $a);

[2018-10-01 10:43:33] /5bb1fa556588f/62309.DEBUG: Array:  ["lemon","banana","apple"] {"file":"/path/to/file.php","line":4,"class":null,"function":"myfunc"}

The problem happens when $a is not an array but for some reasons it is a boolean or something else. In this situation you have more reasons to correctly log it, but when this happens Monolog throws a TypeError exception and the scripts exits after it:

$a = false;
$log->debug("Array: ", $a);

[2018-10-01 10:45:26] /5bb1fac59dc3e/62693.ERROR: Uncaught Exception TypeError: "Argument 2 passed to Monolog\Logger::debug() must be of the type array, boolean given, called in /path/to/file.php on line 4" at /path/to/project/vendor/monolog/monolog/src/Monolog/Logger.php line 530 {"exception":"[object] (TypeError(code: 0): Argument 2 passed to Monolog\\Logger::debug() must be of the type array, boolean given, called in /path/to/file.php on line 4 at /path/to/project/vendor/monolog/monolog/src/Monolog/Logger.php:530)"} {"file":null,"line":null,"class":null,"function":null}

This is because Monolog does not automatically cast the given variable to array.

I am not thinking this is wrong, but I wish to understand what is the better way to solve this problem:


Solution

  • Monolog is a common PHP logging library which implements the PSR-3 LoggerInterface. If you take a look at that definition of how the debug() method should behave you will see that the second argument ($context) must be an array. This is why you're getting a type error. It's not possible for Monolog to let you pass a non-array type to this method since it's dictated by the interface.

    public function debug($message, array $context = array());
    

    I think out of the options that you've suggested so far, the best is to cast your context var as an array (like in your example).