×ðÁú¿­Ê±¹ÙÍøµÇ¼

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
ÍøÕ¾µØͼ