The examples in this article describe the errors and log usages in Laravel. Share to everyone for your reference, specific as follows:
Log
The logs in Laravel are encapsulated based on Monolog. Laravel did a few things on it:
① simplifies functions such as the addinfo in Monolog into a function such as info
② adds Usefiles and usedailyfiles two parameters, making it easy to do log management and cutting
③ if the method you want to call Monolog needs to call the Callmonolog function
OK, look at the following requirements how to achieve:
Store different log information in different logs
This demand is very common, such as the call Order Log, need to record to Order.log, get store information records need to record to shop.log. Can do this:
<?php use
Monolog\logger;
Use Monolog\handler\streamhandler;
Use Illuminate\log\writer;
Class BLogger
{
//All LOG requirements are registered here
const LOG_ERROR = ' ERROR ';
private static $loggers = Array ();
Gets an instance
of the public static function GetLogger ($type = self::log_error, $day =)
{
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;
}
This allows different log data to be stored in different log files. Log data information can also be logged.
Laravel the error log stack is too long, what should I do?
Use the Blogger class above to record the necessary error messages in the start/global.php
Error log information
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 default log does not use split
So you should default to change the default logging of Laravel to a segmented one.
Also in the start/global.php.
Log::usedailyfiles (Storage_path (). ' /logs/laravel.log ', 30);
How to record a requested SQL log
This should be further refined to ask, do you want to record in real time?
If you don't record it in real time, then Laravel has a db::getquerylog to get the SQL request from an app request:
# # in filters.php
app::after (function ($request, $response)
{
//database query log
$queries = Db::getquerylog ();
if (Config::get (' Query.log ', false)) {
Blogger::getlogger (' query ')->info ($queries);
}
If you need to record in real time (that is, when you die out anywhere, and the SQL request for the previous page is logged), you'll need to monitor the Illuminate.query event.
Database real-time request log
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
All of Laravel's mistakes will come out of global app::error.
So if you're designing an interface, and you want to return JSON data even if there's an error, you can do this:
Error log information
app::error (function (Exception $exception, $code)
{
//jump directly to the login page if there is no path
($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 ' => ' server internal error '
];
Return Response::json ($response);
});
If you still want to hold 404 errors:
App::missing (function ($exception)
{
$response = [
' status ' => 0,
' error ' => ' request path error ',
];
Return Response::json ($response);
});
More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "
I hope this article will help you with the PHP program design based on Laravel framework.