源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

laravel中的错误与日志用法详解

  • 时间:2020-05-10 21:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:laravel中的错误与日志用法详解
本文实例讲述了laravel中的错误与日志用法。分享给大家供大家参考,具体如下: [b]日志[/b] 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);
  });
}

[b]错误[/b] 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);
});

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/583.htm]Laravel框架入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/155.htm]php优秀开发框架总结[/url]》、《[url=http://www.1sucai.cn/Special/26.htm]smarty模板入门基础教程[/url]》、《[url=http://www.1sucai.cn/Special/96.htm]php日期与时间用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部