How can I reliably handle exception traces that potentially contain binary data and store those traces in MongoDB from PHP?
We only use MongoDB for logging, and we have a dedicated collection for logging any form of uncaught exception. Recently, I managed to catch the following message in a text log in the event there's an issue storing the log in mongodb.
Detected invalid UTF-8 for field path "message":
We have recently started using BINARY(16) UUIDs in MySQL for a high-write table, and an exception was thrown on some code. The raw binary was in the getTrace() causing the above message when we tried to store this in MongoDB. We are currently storing these type of logs as such
$collection->insertOne([
'errorMessage' => $exception->getMessage(),
'errorFile' => $exception->getFile(),
'errorLine' => $exception->getLine(),
'message' => htmlspecialchars(print_r($exception->getTrace(),true)),
]);
Would it be better to just store $exception->getTrace() without the wrapping functions and process on the other end, or would this result in the same scenario? We were doing the htmlspecialchars and the print_r as a quick and dirty way to convert from an array into a string for a different product to read in. At this point, we have no reservations against changing this methodology.
preg_match('/[^[:print:]]/', $data)[:print:]finds any UTF8 printable chars, so[^[:print:]]find not-printable chars. In PHP 7 you can do some things more with non-utf8 and JSON - but I don't know if it will help you with mongo.