The PHP IDS system expose uses Monolog to store logs into MongoDB. The following is how it stores a log:
{
"message": "Executing on data 4f2793132469524563fa9b46207b21ee",
"context": [
],
"level": NumberLong(200),
"level_name": "INFO",
"channel": "audit",
"datetime": "1441721696",
"extra": [
]
}
I want to use the auto-delete function in Mongo, and I need the datetime
field to store in ISOdate format, like this:
"datetime":ISODate("2015-09-08T17:43:25.678Z")
I look at the class Mongo
in \Expose\Log\Mongo();
and this is the part responsible for storing the datetime
in seconds format
public function log($level, $message, array $context = array())
{
$logger = new \Monolog\Logger('audit');
try {
$handler = new \Monolog\Handler\MongoDBHandler(
new \MongoClient($this->getConnectString()),
$this->getDbName(),
$this->getDbCollection()
);
} catch (\MongoConnectionException $e) {
throw new \Exception('Cannot connect to Mongo - please check your server');
}
$logger->pushHandler($handler);
$logger->pushProcessor(function ($record) {
$record['datetime'] = $record['datetime']->format('U');
return $record;
});
return $logger->$level($message, $context);
}
I have changed the $record['datetime'] into this
//$record['datetime'] = $record['datetime']->format('U');
$record['datetime'] = new \MongoDate();;
but the time isn't store as ISOdate but this:
"datetime": "[object] (MongoDate: 0.84500000 1441721683)"
Can anyone tell me how to store the datetime
in ISODate format?
You are correct to use MongoDate to generate the datetime object. eg:
If you echo this back you will get the format you describe, but internally Mongo should be storing it correctly. To check, you can test:
which should return it in a ISO readable format.
Then you can write your own PSR-3 compatible logger that stores data in that manner.
On your Mongo instance you will want to set the TTL on your field like such:
For more info on the TTL setting see the Mongo docs: Expire Data from Collections by Setting TTL