laravel 错误与日志

Debug 模式(调试/开发模式)

配置文件: config/app.php

  • 开发时, 建议打开这个模式,既设置 APP_DEBUG = true
  • 上线时, 建议关闭调试模式,既设置 APP_DEBUG = false

http异常及自定义异常页面

  • 常见的 http错误码

    • 404 页面未找到
    • 500 服务器内部错误
  • 自定义出现错误是的模板

    • 在控制器中向客户端抛出一个异常,使用 abort 方法
public function customErrorPage(){
    $test = null;
    if ($test == null) {
        abort('500');
    }     
}
  • 自定义模板

resources/views 目录下新建一个 errors 目录, 在这个目录下自定义错误页面模板,定义模板的名称必须和 abort 方法中的 参数一致

日志

  • 日志的配置文件是: /config/app.php
  • laravel 提供了 single daily syslog error 这几种日志模式, 默认single
  • debug info noteice warning error criticalalert 七个错误级别
  • 生产的日志文件存放在 /storage/logs 这个目录中
  • 日志有什么用?

日志记录着程序运行过程中所有的错误, 如果运行过程中出现了错误,而你又不知道是什么原因,错误还无法重现, 此时,你就可以去查看错误

  • 如何记录日志
<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesLog;

class TestController extends Controller{
    public function log(){
        // 错误有七个级别
        Log::info('test log');
        Log::warning('test log222');
        Log::error('这是一个是error级别的日志信息', [
            'error message' => 'error info.....',
            'error code'    => 1234,
        ]);
    }
}
原文地址:https://www.cnblogs.com/liaohui5/p/10581636.html