Laravel 4可以登录到MySQL数据库吗? [英] Can Laravel 4 log to a MySQL database?

查看:87
本文介绍了Laravel 4可以登录到MySQL数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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

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

这篇关于Laravel 4可以登录到MySQL数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆