尊龙凯时人生就是搏

laravel±¨´íÔõôÅŲé

ÔÚʹÓÃlaravel¿ò¼Ü¾ÙÐпª·¢Àú³ÌÖУ¬ÄÑÃâ»á·ºÆðÖÖÖÖ¹ýʧ»òÒì³£¡£ÔõÑù¿ìËÙÅŲé¹ýʧ²¢½â¾öÎÊÌ⣬ÊÇ»ù´¡¿ª·¢ÕßÃæÁÙµÄÌôÕ½¡£±¾ÎĽ«ÎªÄúÏÈÈÝlaravel±¨´íµÄÅŲéÒªÁ죬×ÊÖúÄúÔÚ¿ª·¢Àú³ÌÖÐÊ°빦±¶¡£

Ò»¡¢ÈÕÖ¾¼Í¼

Laravel»á½«ÈÕÖ¾ÐÅϢƾ֤²î±ðÆ·¼¶£¨debug¡¢info¡¢notice¡¢warning¡¢error¡¢critical¡¢alert¡¢emergency£©»®·Ö¼Í¼ÔÚ²î±ðÎļþÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýÉó²éÈÕÖ¾ÎļþÀ´¿ìËÙÕÒ³ö´úÂëÖзºÆðµÄ¹ýʧ¡£

1.1 Éó²éÈÕÖ¾Îļþ

·­¿ªÏîÄ¿¸ùĿ¼ÖеÄstorage/logsĿ¼£¬¿ÉÒÔ¿´µ½laravel.logÎļþ£¬Õâ¸öÊÇϵͳËùÓ춱ðÈÕÖ¾ µÄ×ÜÈÕÖ¾Îļþ£¬ÈôÊÇÏëÒÔijÖÖ¼¶±ðÀ´·Ö¼¯ÈÕÖ¾¼Í¼£¬¿ÉÔÚconfig/logging.php ÎļþÖоÙÐÐÉèÖã»

ÔÚÈÕÖ¾ÎļþÖУ¬¿ÉÒÔÕë¶Ôʱ¼ä¡¢Àú³Ì¡¢¼¶±ðµÈÌõ¼þ¾ÙÐÐɸѡÉó²é¹ýʧÐÅÏ¢¡£ÏêϸʹÓ÷½·¨ÈçÏ£º

tail -f storage/logs/laravel.log // ¼à¿Ø×îеÄÈÕÖ¾ÄÚÈÝ£¬°´Ctrl+C×èÖ¹
cat storage/logs/laravel.log | grep 'Òªº¦×Ö' // ËÑË÷°üÀ¨"Òªº¦×Ö"µÄÈÕÖ¾ÄÚÈÝ
tail -n 100 storage/logs/laravel.log | grep 'Òªº¦×Ö' // Éó²é×îºó100ÐÐÈÕÖ¾ÊÇ·ñ°üÀ¨"Òªº¦×Ö"

µÇ¼ºó¸´ÖÆ

1.2 ¼¶±ðÉèÖÃ

ͨ¹ý Laravel ×Ô´øµÄĿ¼ app/Exceptions/Handler.php À࣬¿ÉÒÔ¿´µ½ÈÕÖ¾µÄ¼¶±ðÉèÖÃÔÚÁË report() º¯ÊýµÄµÚÒ»¸ö²ÎÊýÖУ¬Laravel ĬÈϵÄÈÕÖ¾¼¶±ðΪ error£¬ÕâÒâζ×Å£¬Ö»ÓÐ error ¼¶±ð¼°ÒÔÉϵÄÈÕÖ¾»á±»¼Í¼ÔÚ app/public/storage/logs/laravel.log ÈÕÖ¾ÎļþÖС£

ÈôÊÇÏëÒª½« warning ¼¶±ð¼°ÒÔÉϵÄÈÕÖ¾¶¼Ð´ÈëÈÕÖ¾Îļþ£¬¿ÉÒÔÔÚreport() º¯ÊýµÄµÚÒ»¸ö²ÎÊýÖÐÌí¼ÓÆðʼ¼¶±ð£¬ÈçÏÂËùʾ£º

public function report(Exception $exception)
{
    if ($this->shouldReport($exception)) {
        Log::channel('daily')->warning('Óöµ½¹ýʧÀ²£¡', [
            'error' => $exception->getMessage()
        ]);
    }

    parent::report($exception);
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýLog::channel(‘daily’)->warning()²Ù×÷£¬½«¹ýʧÐÅÏ¢Ó뼶±ðÒ»ÆðдÈëÈÕÖ¾£¬²¢Í¨¹ýshouldReport() ÒªÁìÅжÏÊÇ·ñÐèҪдÈëÈÕÖ¾¡£

¶þ¡¢Òì³£´¦Àí

ÔÚLaravelÓ¦ÓÃÖУ¬Å׳ö×Ô½ç˵Òì³£µÄ·½·¨ºÜÊǼòÆÓ£¬Ö»Ðè¾ÙÐÐÒÔϲÙ×÷¼´¿É£º

2.1 ×Ô½ç˵Òì³£

½¨ÉèÒ»¸öеÄÒì³£À࣬²¢¼ÌÐøLaravelĬÈϵÄExceptionÀ࣬ÈçÏÂËùʾ£º

namespace AppExceptions;
use Exception;

class CustomException extends Exception
{
    public function __construct($message = null, $code = 0)
    {
        parent::__construct($message, $code); // ŲÓø¸ÀàµÄ½á¹¹ÒªÁì
    }
     public function report()
    {
        // ¼Í¼¹ýʧÈÕÖ¾ 
        Log::error('CustomException£º'.$this->getMessage());
    }

    public function render($request)
    {
        return response()->json(['message' => $this->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
    }
}

µÇ¼ºó¸´ÖÆ

ÔڽṹҪÁìÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵Òì³£ÐÅÏ¢¼°×´Ì¬Â룬²¢ÔÚ report() ÒªÁìÖмͼ¹ýʧÈÕÖ¾£¬ÔÚ render() ÒªÁìÖзµ»ØJSONÃûÌõÄÒì³£ÐÅÏ¢¼°×´Ì¬Âë¡£

µ±Ó¦ÓóÌÐòÖзºÆðÁË CustomExceptionʱ£¬Laravel»á×Ô¶¯Ö´ÐÐ report() ÒªÁ콫¹ýʧÐÅÏ¢¼Í¼µ½ÈÕÖ¾ÎļþÖУ¬²¢Í¨¹ý render() ÒªÁì·µ»ØÒì³£ÐÅÏ¢¸øÇ°¶ËÏÔʾ¡£

2.2 ´¥·¢Òì³£

ÔÚ´úÂëÖÐʹÓà throw new CustomException ()ÒªÁì¼´¿ÉÅ׳öÒì³££¬ÈçÏÂËùʾ£º

public function test(Request $request)
{
    // ...
    if (! check_validate($request->all())) {
        throw new CustomException('ÇëÇó²ÎÊý²»×¼È·');
    } 
}

µÇ¼ºó¸´ÖÆ

Èý¡¢¹ýʧµ÷ÊÔ

ÔÚ¾ÙÐпª·¢»ò²âÊÔʱ£¬ÓÐʱÐèÒªÔÚ´úÂëÖдò³öµ÷ÊÔÐÅÏ¢À´Àû±ã²éÕÒÎÊÌâËùÔÚ£¬¶øÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý dump() ºÍ dd() Á½¸öº¯ÊýÀ´¿ìËÙÊä³öµ÷ÊÔÐÅÏ¢ºÍÖÐÖ¹³ÌÐòÖ´ÐÐЧ¹û¡£

3.1 dump()ºÍdd()

dump() º¯ÊýÓÃÓÚ´òÓ¡±äÁ¿»òÕßÊý×éµÄÄÚÈÝ£¬²»»áÖÐÖ¹³ÌÐòµÄÔËÐС£ÀàËÆÓÚPHPÄÚÖõÄvar_dump()º¯Êý¡£

dd() º¯Êý²»µ«»á´òÓ¡±äÁ¿»òÕßÊý×éµÄÄÚÈÝ£¬»¹»áÖÐÖ¹³ÌÐòµÄÖ´ÐУ¬²¢ÇÒ°Ñ´¦Àíµ½Ä¿½ñλÖõĴúÂë¿ÍÕ»ÒÔ¼°Òì³£µÄÏêϸÐÅÏ¢¶¼ÏÔʾ³öÀ´¡£ÀàËÆÓÚPHPÄÚÖÃdie() »òexit()º¯Êý¡£

Route::get('/test', function () {
    dump('Hello Laravel!');
    return response()->json(['key' => 'value']);
});

Route::get('/test2', function () {
    $array = ['key_one' => 'value_one', 'key_two' => 'value_two'];
    dd($array);
    return response()->json(['key' => 'value']);
});

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ý dump() ºÍ dd() ÒªÁìÀ´´òÓ¡×Ö·û´®ºÍÊý×éµÄÄÚÈÝ£¬Ê¹Ó÷½·¨ÀàËÆÓÚPHPÔ­Éúº¯Êý£¬ºÜÊÇÀû±ã¡£

ËÄ¡¢½áÓï

ÔÚLaravel¿ª·¢Àú³ÌÖУ¬ÓÉÓÚÖÖÖÖÔµ¹ÊÔ­ÓÉ£¬×᷺ܻÆðÖÖÖÖ¸÷ÑùµÄ±¨´íÐÅÏ¢¡£ÔõÑù¸ßЧµØÅŲ鲢½â¾öÕâЩ¹ýʧ£¬ÊÇÿ¸ö¿ª·¢Õß±ØÐèÃæÁÙµÄÎÊÌâ¡£±¾ÎÄÖÐÎÒÃÇÏÈÈÝÁËLaravelÈÕÖ¾¼Í¼¡¢Òì³£´¦Àí¡¢¹ýʧµ÷ÊԵȼ¸¸ö³£ÓõÄÅŲ鷽·¨£¬Ï£Íû¶ÔÄúÔÚLaravelÖпª·¢ÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇlaravel±¨´íÔõôÅŲéµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ