Can Laravel 4 log to a MySQL database?

蓝咒 提交于 2019-12-04 08:36:30

问题


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 github page.


回答1:


Yep, You could create a listener to log everything in the 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,

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



回答2:


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.

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.




回答3:


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 composer or directly on GitHub. Might by worth a try.




回答4:


In laravel 5 now it is illuminate.log

Now it will be like

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


来源:https://stackoverflow.com/questions/15593010/can-laravel-4-log-to-a-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!