Problem in formatting array with monolog and mongodb
igor-mobapps opened this issue · comments
Hello, I don't know if it is exactly a bug
, but I am using the monolog as an Audit system together with the MongoDB handler, when trying to submit an array of arrays to mongoDB, the monolog is returning me a badly formatted array with [...]
in string format.
Settings
OS
: Win 10 Pro v20.04
php -v
PHP 7.4.9
MongoDB
"mongodb/mongodb": "^1.7"
Monolog
"monolog/monolog": "^2.1"
mongo -version
MongoDB shell version v4.4.1
Build Info: {
"version": "4.4.1",
"gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "windows",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
Test using monolog with mongoDBHandler
$client = new MongoDB\Client("mongodb://localhost:27017");
$monolog = new Logger(
'AUDIT',
[new MongoDBHandler($client, 'audit_test', 'audit_trail', Logger::INFO)],
);
$monolog->log(Logger::INFO, 'teste', [
'oldState' => [
'user' => [ // WORKING
'first_name' => 'first unknown',
'last_name' => 'last unknown'
],
'users' => [ // NOT WORKING
'abk1' => [
'first_name' => 'first unknown v1',
'last_name' => 'last unknown v1',
],
'abk2' => [
'first_name' => 'first unknown v2',
'last_name' => 'last unknown v2'
]
]
]
]);
Test using only mongodb without monolog
$client = new MongoDB\Client("mongodb://localhost:27017");
$db = $client->audit_test;
$insertOneResult = $db->teste->insertOne([
'oldState' => [
'user' => [
'first_name' => 'first unknown',
'last_name' => 'last unknown'
],
'users' => [
'abk1' => [
'first_name' => 'first unknown v1',
'last_name' => 'last unknown v1',
],
'abk2' => [
'first_name' => 'first unknown v2',
'last_name' => 'last unknown v2'
]
]
]
]);
Output
This is not a bug. MongoDBFormatter has a maxNestingLevel field with default value 3. You have to set a new formatter which have proper nesting level to your need or have infinite nesting level to your MongoDBHandler.
$handler = new MongoDBHandler($mongodb, 'db', 'collection');
$handler->setFormatter(new MongoDBFormatter(5)); // setted to max 5 nesting levels
$handler->setFormatter(new MongoDBFormatter(0)); // infinite nesting levels
@serkanerip thanks!