Laravel 4 可以登录到 MySQL 数据库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15593010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:38:09  来源:igfitidea点击:

Can Laravel 4 log to a MySQL database?

phplaravellaravel-4monolog

提问by Nyxynyx

If so how can it be done? By default L4 writes to a text file. I notice that Monolog can Log to Database on its githubpage.

如果是这样怎么办?默认情况下,L4 写入文本文件。我注意到 Monolog 可以在其github页面上登录到数据库。

采纳答案by Pratheek Rebala

Yep, You could create a listener to log everything in the routes.php

是的,您可以创建一个侦听器来记录 routes.php 中的所有内容

Event::listen('laravel.log', function($type,$message)
{
    $log = new Log();
    $log->message = $message;
    $log->type = $type;
    $log->update;
});

Or alternatively if you wanted to only log errors 400 and 500 Larvavel there is a Log event in the Routes.php file which listens to errors 404 and 500, you can write your own code in this event listener. So assuming you have a Model called Log defined,

或者,如果您只想记录错误 400 和 500 Larvavel,Routes.php 文件中有一个 Log 事件可以侦听错误 404 和 500,您可以在此事件侦听器中编写自己的代码。因此,假设您定义了一个名为 Log 的模型,

Event::listen('404', function()
{
    $error = "404: " . URL::full();
    Log::error($error);
    $update = new Log();
    $update->error = $error;
    $update->update;
    return Response::error('404');
});

Event::listen('500', function()
{
    Log::error($error);
    $update = new Log();
    $update->error = $error;
    $update->update;
    return Response::error('500');
});

回答by Seldaek

As you can see if you read further the headline, Monolog natively supports writing to Redis, MongoDB and CouchDB. Those three are all supporting fairly write heavy (and very write heavy in the case of Redis) use-cases. MySQL is not there because logging to MySQL isn't really the best idea in the world.

如果您进一步阅读标题,就会看到,Monolog 本身支持写入 Redis、MongoDB 和 CouchDB。这三个都支持大量写入(在 Redis 的情况下写入量非常大)用例。MySQL 不存在是因为登录到 MySQL 并不是世界上最好的主意。

If you really want to do it though, you can check the docs on creating your own handler, which explains how to create and use a PDO handler to write to a SQL database: https://github.com/Seldaek/monolog/blob/master/doc/extending.md- I still think it's a bad idea, but maybe the use case warrants it.

如果您真的想这样做,您可以查看有关创建自己的处理程序的文档,其中解释了如何创建和使用 PDO 处理程序写入 SQL 数据库:https: //github.com/Seldaek/monolog/blob /master/doc/extending.md- 我仍然认为这是一个坏主意,但也许用例值得。

回答by waza-ari

As I had the same demand in my project, I have created a handler for Monolog to write the log output to MySQL. It is released under MIT license and available through composeror directly on GitHub. Might by worth a try.

由于我在我的项目中有同样的需求,我为 Monolog 创建了一个处理程序,以将日志输出写入 MySQL。它是在 MIT 许可下发布的,可通过composer或直接在GitHub 上获得。可能值得一试。

回答by Niklesh Raut

In laravel 5 now it is illuminate.log

在 Laravel 5 现在是 illuminate.log

Now it will be like

现在它会像

Event::listen('illuminate.log', function($type,$message)
{
    ....
});

回答by 8ctopus

For those who are wondering how to do it with laravel/lumen from 5.6 up to 7.x

对于那些想知道如何使用 laravel/lumen 从 5.6 到 7.x 的人

  1. composer require wazaari/monolog-mysql
  2. Edit file config/logging.php as follows
  1. 作曲家需要 wazaari/monolog-mysql
  2. 编辑文件 config/logging.php 如下
<?php

use MySQLHandler\MySQLHandler;

...

    'channels' => [
        'stack' => [
            'driver'   => 'stack',
            'channels' => ['daily', 'mysql'],
        ],
        ...

        'mysql' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => MySQLHandler::class,
            'with' => [
                'pdo' => app('db')->connection()->getPdo(),
                'table' => 'table-name',
            ],
        ]