enygma / expose

An Intrusion Detection System library loosely based on PHP IDS
MIT License
266 stars 55 forks source link

How can I save the datetime field in ISODate format in MongoDB #34

Closed laukaichung closed 8 years ago

laukaichung commented 9 years ago

I want to change the datetime format to ISODate in MongoDB so that I can use MongoDB’s “time to live” or TTL collection feature.

I want to store the timestamp like this:

 "datetime": ISODate("2015-09-08T17:43:25.678Z") 

I have changed the class in Log/Mongo.php to use new \MongoDate

    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');

            $exampleDate = new \MongoDate();

            $record['datetime'] =  $exampleDate;

            return $record;
        });

        return $logger->$level($message, $context);
    }

But that doesn't convert the timestamp to ISODate but still store as an object:

 "datetime": "[object] (MongoDate: 0.32700000 1441719561)",

Would you please tell me how to store the datetime field in ISODate format?

Awnage commented 8 years ago

I see that you have also posted this question to StackOverflow with no response so far.

Since the question is more about Mongo/PHP than expose, SO is a better forum to request assistance. If I find something, I will respond there.