关于laravel:laravel-框架使用mongodb-保存日志

50次阅读

共计 2150 个字符,预计需要花费 6 分钟才能阅读完成。

1.php 装置 mongodb 扩大
2. 在 laravel 根目录,执行 composer require mongodb/mongodb
3. 在 logging.php 配置 mongodb 驱动

'mongodb' => [
            'driver' => 'monolog',
            'handler' => Monolog\Handler\MongoDBHandler::class,
            'formatter' => 'default',
            'handler_with' => ['mongodb' => new Client("mongodb://localhost:27017"),
                'database' => "mongolog",
                'collection' => "logs",
            ],
        ],

4. 测试
执行 php artisan tinker
输出:Log::warning(“mongolog test”);
查看 mongodb 数据库后果:

use mongolog;
db.logs.find()

[
{

"_id": {"$oid": "5f2789d82e933325a57cf5d2"},
"channel": "local",
"context": {
  "exception": {
    "class": "Symfony\\Component\\Console\\Exception\\CommandNotFoundException",
    "message": "Command \"think\"is not defined.",
    "code": 0,
    "file": "/home/gordon/workspace/php/blog/vendor/symfony/console/Application.php:644",
    "trace": "#0 /home/gordon/workspace/php/blog/vendor/symfony/console/Application.php(228): Symfony\\Component\\Console\\Application->find('think')\n#1 /home/gordon/workspace/php/blog/vendor/symfony/console/Application.php(140): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\n#2 /home/gordon/workspace/php/blog/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\n#3 /home/gordon/workspace/php/blog/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\n#4 /home/gordon/workspace/php/blog/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))\n#5 {main}"
  }
},
"datetime": {"$date": "2020-08-03T03:51:52.847Z"},
"extra": [],
"level": 400,
"level_name": "ERROR",
"message": "Command \"think\"is not defined."

},
{

"_id": {"$oid": "5f2789f2f4f0b727732172d2"},
"channel": "local",
"context": [],
"datetime": {"$date": "2020-08-03T03:52:18.395Z"},
"extra": [],
"level": 300,
"level_name": "WARNING",
"message": "12312"

}
]

正文完
 0