laravel中的错误与日志(可以自定义日志目录和log文件名)

来源:互联网 发布:国家电影专项资金算法 编辑:程序博客网 时间:2024/05/10 18:36

laravel中的错误与日志

2014-12-19 10:09 by 轩脉刃, 21252 阅读, 1 评论, 收藏, 编辑

日志

laravel中的日志是基于monolog而封装的。laravel在它上面做了几个事情:

  • 把monolog中的addInfo等函数简化成为了info这样的函数
  • 增加了useFiles和useDailyFiles两个参数,使得做日志管理和切割变的容易了
  • 如果要调用monolog的方法需要调用callMonolog函数

好了,看下下面几个需求怎么实现:

将不同的日志信息存放到不同的日志中去

这个需求很普遍的,比如调用订单的日志,需要记录到order.log,获取店铺信息的记录需要记录到shop.log中去。可以这么做:

<?php use Monolog\Logger;use Monolog\Handler\StreamHandler;use Illuminate\Log\Writer;class BLogger{    // 所有的LOG都要求在这里注册    const LOG_ERROR = 'error';    private static $loggers = array();    // 获取一个实例    public static function getLogger($type = self::LOG_ERROR, $day = 30)    {        if (empty(self::$loggers[$type])) {            self::$loggers[$type] = new Writer(new Logger($type));            self::$loggers[$type]->useDailyFiles(storage_path().'/logs/'. $type .'.log', $day);        }        $log = self::$loggers[$type];        return $log;    }}

这样不同的日志数据会被存储到不同的日志文件中去。还能记录日志数据信息。

laravel的错误日志堆栈太长了,怎么办?

使用上面的BLogger类,在start/global.php记录下必要的错误信息

// 错误日志信息App::error(function(Exception $exception, $code){    Log::error($exception);    $err = [        'message' => $exception->getMessage(),        'file' => $exception->getFile(),        'line' => $exception->getLine(),        'code' => $exception->getCode(),        'url' => Request::url(),        'input' => Input::all(),    ];    BLogger::getLogger(BLogger::LOG_ERROR)->error($err);});

laravel默认的日志没有使用分割

所以应该默认把laravel的默认日志记录改成有分割的。

同样在start/global.php中

Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);

如何记录一个请求的sql日志

这个应该再细化问,你是不是要实时记录?

如果不要实时记录,那么laravel有个DB::getQueryLog可以获取一个app请求获取出来的sql请求:

## 在filters.php中App::after(function($request, $response){    // 数据库查询进行日志    $queries = DB::getQueryLog();    if (Config::get('query.log', false)) {        BLogger::getLogger('query')->info($queries);    }}

如果你是需要实时记录的(也就是你在任何地方die出来的时候,之前的页面的sql请求也有记录)的话,你就需要监听illuminate.query事件了

// 数据库实时请求的日志if (Config::get('database.log', false)){    Event::listen('illuminate.query', function($query, $bindings, $time, $name)    {        $data = compact('query','bindings', 'time', 'name');        BLogger::getLogger(BLogger::LOG_QUERY_REAL_TIME)->info($data);    });}

错误

laravel的所有错误会全部过global的App::error再出来

所以比如你设计的是接口,希望即使有error出现也返回json数据,则可以这么做:

// 错误日志信息App::error(function(Exception $exception, $code){    // 如果没有路径就直接跳转到登录页面    if ($exception instanceof NotFoundHttpException) {        return Redirect::route('login');    }    Log::error($exception);    $err = [        'message' => $exception->getMessage(),        'file' => $exception->getFile(),        'line' => $exception->getLine(),        'code' => $exception->getCode(),        'url' => Request::url(),        'input' => Input::all(),    ];    BLogger::getLogger(BLogger::LOG_ERROR)->error($err);    $response = [        'status' => 0,        'error' => "服务器内部错误",    ];    return Response::json($response);});

如果你还希望将404错误也hold住:

App::missing(function($exception){    $response = [        'status' => 0,        'error' => "请求路径错误",    ];    return Response::json($response);});
0 0
原创粉丝点击