PHP IDS 系统暴露使用Monolog将日志存储到 MongoDB。以下是它存储日志的方式:
{
"message": "Executing on data 4f2793132469524563fa9b46207b21ee",
"context": [
],
"level": NumberLong(200),
"level_name": "INFO",
"channel": "audit",
"datetime": "1441721696",
"extra": [
]
}
我想在 Mongo 中使用自动删除功能,我需要该datetime
字段以 ISOdate 格式存储,如下所示:
"datetime":ISODate("2015-09-08T17:43:25.678Z")
我查看了里面的类Mongo
,\Expose\Log\Mongo();
这是负责存储datetime
以秒为单位的格式的部分
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);
}
我已将 $record['datetime'] 更改为此
//$record['datetime'] = $record['datetime']->format('U');
$record['datetime'] = new \MongoDate();;
但时间不存储为 ISOdate 而是这样:
"datetime": "[object] (MongoDate: 0.84500000 1441721683)"
谁能告诉我如何以datetime
ISODate 格式存储?