尊龙凯时人生就是博

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Logging¼Í¼ÈÕÖ¾£¿

laravelÊÇÒ»¿î¹ãÊܽӴýµÄphp¿ò¼Ü £¬ÆäÄÚÖÃÁËÐí¶à¹¦Ð§Àû±ãÎÒÃǾÙÐпª·¢ £¬ÆäÖÐÈÕÖ¾¼Í¼ÊÇÆäÖÐÖ®Ò»¡£Í¨¹ý¼Í¼ÈÕÖ¾ £¬ÎÒÃÇ¿ÉÒÔ¿ìËÙ¶¨Î»³ÌÐòÖеÄÎÊÌâ £¬Ìá¸ß³ÌÐòµÄÎȹÌÐԺͿÉά»¤ÐÔ¡£ÔÚ±¾ÎÄÖÐ £¬ÎÒÃǽ«Ì½ÌÖÔõÑùʹÓÃlaravel logging¼Í¼ÈÕÖ¾¡£

Laravel Logging¼ò½é

Laravel LoggingÊÇLaravel¿ò¼ÜÄÚÖõÄÒ»ÖּͼÈÕÖ¾µÄ·½·¨¡£Æäͨ¹ýÖ¸¶¨Çý¶¯³ÌÐò½«ÈÕ־дÈë²î±ðµÄ´æ´¢½éÖÊÖÐ £¬ÀýÈçÎļþ¡¢Êý¾Ý¿â¡¢syslog¡¢redisµÈµÈ¡£ÔÚLaravel¿ò¼ÜÖÐ £¬Ä¬ÈϵÄÇý¶¯³ÌÐòΪµ¥¸öÎļþ £¬µ«ÎÒÃÇÒ²¿ÉÒÔͨ¹ý¼òÆÓµÄÉèÖøü¸ÄĬÈϵÄÇý¶¯³ÌÐò £¬ÒÔ˳Ӧ²î±ðµÄÇéÐΡ£

ÉèÖÃLaravel Logging

ĬÈÏÇéÐÎÏ £¬Laravel LoggingµÄÉèÖÃÎļþλÓÚconfig/logging.php¡£ÔÚ±¾ÎļþÖÐ £¬ÎÒÃÇ¿ÉÒÔ½ç˵ȫ¾ÖµÄÈÕÖ¾ÉèÖà £¬ÀýÈçĬÈϵÄÈÕÖ¾Çý¶¯³ÌÐò¡¢ÈÕÖ¾ÎļþµÄ´æ´¢Î»ÖÃÒÔ¼°²î±ðÈÕÖ¾¼¶±ð¶ÔÓ¦µÄ´¦Àí·½·¨¡£

ĬÈÏÇéÐÎÏ £¬Laravel LoggingÆôÓÃÁ˵¥¸öÎļþµÄÇý¶¯³ÌÐò £¬¸ÃÎļþλÓÚstorage/logs/laravel.log¡£ÔÚLaravel¿ò¼ÜÖÐ £¬ÈÕÖ¾ÐÂÎÅ¿ÉÒÔʹÓÃLogÃÅÃæÒýÈë £¬ÀýÈ磺

use IlluminateSupportFacadesLog;
Log::info('This is an information message.');
Log::error('Something went wrong.');

µÇ¼ºó¸´ÖÆ

ʹÓÃÉÏÊö´úÂë¿ÉÒÔ½«ÐÅÏ¢ºÍ¹ýʧ¼¶±ðµÄÈÕÖ¾ÐÂÎÅдÈëĬÈϵÄÈÕÖ¾Îļþ £¬ÆäËüÈÕÖ¾¼¶±ðÒ²¿É²ÎÕÕÆäд·¨¾ÙÐмͼ¡£

ÉèÖÃÈÕÖ¾Îļþ

³ýÁËĬÈϵÄÈÕÖ¾Çý¶¯³ÌÐòÎļþÍâ £¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÆäËüÇý¶¯³ÌÐò £¬ÀýÈçÈÕÆÚ»¯µÄÎļþ¡¢ÈÕÖ¾ÂÖ»»¡¢Í¨¹ýHTTPÇëÇó½«ÈÕÖ¾·¢Ë͵½Ô¶³ÌЧÀ͵ȵÈ¡£ÔÚconfig/logging.phpÎļþÖÐ £¬ÎÒÃÇ¿ÉÒÔÖ±½Ó½ç˵ÈÕÖ¾µÄÇý¶¯³ÌÐòºÍ²ÎÊý £¬ÀýÈ磺

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 14,
],

'syslog' => [
    'driver' => 'syslog',
    'level' => 'warning',
]

µÇ¼ºó¸´ÖÆ

ÎÒÃÇ¿ÉÒÔʹÓÃdailyÇý¶¯³ÌÐò½«ÈÕÖ¾Îļþƾ֤ÈÕÆÚ¾ÙÐÐÖ§½â £¬Ã¿¸öÎļþµÄÖÜÆÚΪ14Ìì¡£ÎÒÃÇ»¹¿ÉÒÔʹÓÃsyslogÇý¶¯³ÌÐò½«ÈÕÖ¾ÐÂÎÅ·¢Ë͵½ÏµÍ³µÄÈÕ־ЧÀÍÖÐ £¬ÀýÈçLinuxµÄsyslogdÊØ»¤Àú³Ì¡£

¼Í¼ÉÏÏÂÎÄÐÅÏ¢

ÔÚÏÖʵӦÓÃÖÐ £¬ÎÒÃÇÍùÍùÐèҪͬʱ¼Í¼һЩÉÏÏÂÎÄÐÅÏ¢ £¬ÀýÈçÓû§ID¡¢ÇëÇóµÄURI¡¢¿Í»§¶ËIPµØµãµÈµÈ¡£ÔÚLaravel LoggingÖÐ £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÈÕÖ¾ÉÏÏÂÎļͼÕâЩÐÅÏ¢¡£

ÀýÈç £¬ÔÚÒ»¸öHTTPÇëÇóÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ뽫һЩÉÏÏÂÎÄÐÅÏ¢¼Í¼µ½ÈÕÖ¾ÎļþÖУº

use IlluminateSupportFacadesLog;
Log::channel('mylog')->withContext([
    'user_id' => $request->user()->id,
    'ip' => $request->getClientIp(),
    'uri' => $request->getUri(),
])->info('An information message with context.');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖÐ £¬ÎÒÃÇʹÓÃÁËLogÃÅÃæµÄchannelÒªÁì £¬Ö¸¶¨ÁËÒ»¸öÃû³ÆΪmylogµÄÈÕ־ͨµÀ¡£È»ºóʹÓÃwithContextÒªÁ콫һЩÉÏÏÂÎÄÐÅϢת´ïµ½ÈÕÖ¾¼Í¼Æ÷ÖÐ £¬×îºóʹÓÃinfoÒªÁ콫ÈÕÖ¾ÐÂÎżÍ¼µ½ÎļþÖС£

×Ô½ç˵Laravel Logging

³ýÁËʹÓÃLaravel LoggingµÄĬÈÏÉèÖÃÍâ £¬ÎÒÃÇ»¹¿ÉÒÔͨ¹ý±àд×Ô½ç˵µÄÈÕÖ¾¼Í¼Æ÷×é¼þÀ´ÊµÏÖ¸üΪÖØ´óµÄÈÕ־ϵͳ¡£

Ê×ÏÈ £¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öеÄÈÕ־ͨµÀÉèÖà £¬ÀýÈ磺

'custom' => [
    'driver' => 'custom',
    'via' => AppLoggingCustomLogger::class,
    'level' => 'debug',
],

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊöÉèÖÃÖÐ £¬ÎÒÃÇÖ¸¶¨ÁËÒ»¸ö×Ô½ç˵µÄÇý¶¯³ÌÐò £¬Ê¹ÓÃAppLoggingCustomLogger::classʵÀý»¯Ò»¸ö×Ô½ç˵µÄÈÕ־дÈëÆ÷¡£

È»ºó £¬ÎÒÃÇ¿ÉÒÔ±àдһ¸ö×Ô½ç˵µÄÈÕ־дÈëÆ÷×é¼þ £¬ÀýÈ磺

<?php

namespace AppLogging;

use MonologFormatterHtmlFormatter;
use MonologHandlerStreamHandler;
use MonologLogger;

class CustomLogger
{
    static public function __invoke(array $config)
    {
        $logger = new Logger('custom');

        $handler = new StreamHandler($config['path']);
        $handler->setFormatter(new HtmlFormatter());

        $logger->pushHandler($handler);

        return $logger;
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊöʾÀýÖÐ £¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪCustomLoggerµÄÀà £¬¸ÃÀàʹÓÃÁËMonolog×é¼þÖеÄһЩ¹¦Ð§ £¬½«ÈÕ־дÈëµ½Ö¸¶¨ÎļþÖÐ £¬²¢Ê¹ÓÃHTMLÃûÌþÙÐмͼ¡£

×îºó £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ·½·¨Ê¹ÓÃ×Ô½ç˵µÄÈÕÖ¾¼Í¼Æ÷£º

use IlluminateSupportFacadesLog;
Log::channel('custom')->info('A custom information message.');

µÇ¼ºó¸´ÖÆ

ͨ¹ýÕâÖÖ·½·¨ £¬ÎÒÃÇ¿ÉÒÔÍêÈ«ÕÆ¿ØLaravel LoggingµÄʵÏÖ £¬²¢ÊµÏÖ¸üΪ¶¨ÖÆ»¯µÄÈÕÖ¾¼Í¼ÐèÇó¡£

×ܽá

Laravel LoggingÊÇLaravel¿ò¼ÜÄÚÖõÄÒ»ÖּͼÈÕÖ¾µÄ·½·¨ £¬¿ÉÒÔ½«ÈÕÖ¾ÐÂÎżÍ¼µ½Îļþ¡¢Êý¾Ý¿â¡¢syslogµÈ¶àÖÖ´æ´¢½éÖÊÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÎļþºÍÉÏÏÂÎļͼµÈ·½·¨ £¬¿ØÖÆÈÕÖ¾µÄ¼Í¼·½·¨ºÍÈÕÖ¾ÐÂÎŵÄÄÚÈÝ¡£±ðµÄ £¬ÎÒÃÇ»¹¿ÉÒÔͨ¹ý±àд×Ô½ç˵ÈÕÖ¾¼Í¼Æ÷×é¼þ £¬ÎÞаµØ¶¨ÖÆLaravel LoggingµÄʵÏÖ¡£Í¨¹ýLaravel Logging¼Í¼ÈÕÖ¾ £¬ÓÐÖúÓÚ³ÌÐòµÄÎȹÌÐԺͿÉά»¤ÐÔ £¬ÊÇLaravel¿ª·¢ÖкÜÊÇÖ÷ÒªµÄÒ»»·¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Logging¼Í¼ÈÕÖ¾£¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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