[GH-ISSUE #642] How to save datetime field in ISODate format instead of seconds #232

Closed
opened 2026-03-04 02:13:21 +03:00 by kerem · 1 comment
Owner

Originally created by @laukaichung on GitHub (Sep 8, 2015).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/642

I have been using a php IDS system expose that uses monolog to store logs into MongoDB. I'm trying to change the datetime field to ISODate format so that I can use the MongoDB’s “time to live” or TTL collection feature to delete logs.

Here's the part responsible for storing datetime in \Expose\Log\Mongo();

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 $record['datetime'] = $record['datetime']->format('U'); to

$record['datetime'] = new \MongoDate();

but the timestamp is stored as

"datetime": "[object] (MongoDate: 0.84500000 1441721683)"

instead of

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

How can I get the datetime to store in ISODate format instead of seconds?

Originally created by @laukaichung on GitHub (Sep 8, 2015). Original GitHub issue: https://github.com/Seldaek/monolog/issues/642 I have been using a php IDS system `expose` that uses monolog to store logs into MongoDB. I'm trying to change the `datetime` field to ISODate format so that I can use the MongoDB’s “time to live” or TTL collection feature to delete logs. Here's the part responsible for storing datetime in `\Expose\Log\Mongo();` ``` 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 `$record['datetime'] = $record['datetime']->format('U');` to `$record['datetime'] = new \MongoDate();` but the timestamp is stored as ``` "datetime": "[object] (MongoDate: 0.84500000 1441721683)" ``` instead of ``` "datetime":ISODate("2015-09-08T17:43:25.678Z"); ``` How can I get the `datetime` to store in ISODate format instead of seconds?
kerem closed this issue 2026-03-04 02:13:21 +03:00
Author
Owner

@Seldaek commented on GitHub (Sep 13, 2015):

You'd need to extend and override the JsonFormatter::normalize method and set a custom formatter on that MongoDBHandler I believe.

<!-- gh-comment-id:139928720 --> @Seldaek commented on GitHub (Sep 13, 2015): You'd need to extend and override the JsonFormatter::normalize method and set a custom formatter on that MongoDBHandler I believe.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#232
No description provided.