The following tutorial column will introduce you to the errors and logs in laravel (you can customize the log directory and log file name). I hope it will be helpful to friends in need!
LogThe log in laravel is encapsulated based on monolog. laravel has done several things on it:
Simplified functions such as addInfo in monolog into functions like info Added two parameters useFiles and useDailyFiles, making it possible to do Log management and cutting have become easierThis requirement is very common. For example, the log of calling an order needs to be recorded in order.log, and the record of obtaining store information needs to be recorded in shop.log. go. You can do this:
<?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; } }
Use the above Blogger class to record the necessary error information in 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’s default log does not use segmentation
So laravel’s default logging should be changed to split by default. Also in start/global.php
Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);
How to record the sql log of a request
This should be more detailed Hua asked, do you want to record in real time? If you don’t want real-time recording, then laravel has DB::getQueryLog to get the sql request obtained by an app request:
## 在filters.php中 App::after(function($request, $response) { // 数据库查询进行日志 $queries = DB::getQueryLog(); if (Config::get('query.log', false)) { BLogger::getLogger('query')->info($queries); } }
If you need real-time recording (that is, you are in any When the local die comes out, the sql request of the previous page is also recorded), you need to monitor the illuminate.query event
// 数据库实时请求的日志 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); }); }
Error
laravel All errors will pass through the global App::error before appearing. So if you design an interface and hope that json data will be returned even if an error occurs, you can do this:
// 错误日志信息 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); });
If you still want to hold the 404 error:
App::missing(function($exception) { $response = [ 'status' => 0, 'error' => "请求路径错误", ]; return Response::json($response); });
The above is the detailed content of About errors and logs in laravel. For more information, please follow other related articles on the PHP Chinese website!