laravelдÈÕÖ¾ÒªÁì
laravel ÊÇÒ»¿îºÜÊÇÊ¢ÐÐµÄ php ¿ª·¢¿ò¼Ü£¬ÆäÎÞаºÍÇ¿Ê¢µÄ¹¦Ð§»ñµÃÁË¿í´ó¿ª·¢ÕßµÄÇàíù¡£ÔÚÓ¦Óÿª·¢Àú³ÌÖУ¬¼Í¼ÈÕÖ¾ÊǺÜÊÇÖ÷ÒªµÄÒ»ÏîÊÂÇ飬ÕâÑù¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¿ìËÙ¶¨Î»Ï¢Õù¾öÎÊÌâ¡£±¾ÎĽ«ÏÈÈÝ laravel дÈÕÖ¾µÄÒªÁì¡£
ÉèÖÃÈÕÖ¾ÐÅÏ¢
Laravel ÖеÄÈÕÖ¾ÉèÖÃÐÅÏ¢´æ·ÅÔÚ config Ŀ¼Ï嵀 logging.php ÎļþÖС£ÔÚ¸ÃÎļþÖнç˵ÁËĬÈϵÄÈÕÖ¾Çý¶¯ºÍ Log channel£¨ÈÕ־ͨµÀ£©¡£
¿ÉÒÔƾ֤ÐèÒª£¬×Ô½ç˵ÈÕÖ¾Çý¶¯ºÍ Log channel¡£×î³£¼ûµÄÈÕÖ¾Çý¶¯ÀàÐÍ°üÀ¨£ºµ¥¸öÎļþºÍÖðÈÕÈÕÖ¾Îļþ¡£ÆäÖУ¬µ¥¸öÎļþģʽ½«ËùÓÐÈÕÖ¾¼Í¼µ½Ò»¸öÎļþÖУ¬¶øÖðÈÕÈÕÖ¾ÎļþģʽÔò»áÓÉÓÚÌìÌìµÄÈÕÆÚ¶ø½¨ÉèеÄÈÕÖ¾Îļþ¡£
ÔÚÉèÖÃÎļþÖÐÑ¡ÔñÈÕÖ¾Çý¶¯ÀàÐÍÖ®ºó£¬ÎÒÃÇ»¹ÐèÒªÉèÖà Log channel£¬ÒÔ±ãÔÚÓ¦ÓóÌÐòÖмͼÈÕ־ʱʹÓá£
ʹÓà Log Facade дÈÕÖ¾
Laravel ÌṩÁËÒ»¸ö Log Facade£¬ÌṩÁ˼ͼÈÕÖ¾µÄÒªÁ죬ʹÎÒÃÇ¿ÉÒÔÇáËɵؽ«ÈÕÖ¾ÐÅÏ¢¼Í¼µ½ÈÕÖ¾ÎļþÖС£Ö±½ÓʹÓà Log:: À´Å²Óà Log Facade£¬Ê¹Óà info() »òÕß debug() ¼Í¼ÈÕÖ¾ÐÅÏ¢¼´¿É¡£ÈçÏÂËùʾ£º
use IlluminateSupportFacadesLog; // ¼Í¼ Info ¼¶±ðµÄÈÕÖ¾ÐÅÏ¢ Log::info('This is an info level message.'); // ¼Í¼ Debug ¼¶±ðµÄÈÕÖ¾ÐÅÏ¢ Log::debug('This is a debug level message.');
µÇ¼ºó¸´ÖÆ
¿ÉÒÔÔÚÈÕÖ¾ÉèÖÃÎļþÖнç˵²î±ðµÄ Log channel£¬²¢ÉèÖòî±ðµÄ´¦ÀíÆ÷£¨Handlers£©£¬ÒԱ㽫ÈÕÖ¾ÐÅÏ¢´æ´¢µ½²î±ðµÄλÖá£
ÈçÏÂËùʾ£º
use IlluminateSupportFacadesLog; // ʹÓà MyLog ͨµÀ¼Í¼ Info ¼¶±ðµÄÈÕÖ¾ÐÅÏ¢ Log::channel('MyLog')->info('This is an info level message.'); // ʹÓà MyLog ͨµÀ¼Í¼ Debug ¼¶±ðµÄÈÕÖ¾ÐÅÏ¢ Log::channel('MyLog')->debug('This is a debug level message.');
µÇ¼ºó¸´ÖÆ
ʹÓà Monolog дÈÕÖ¾
Monolog ÊÇ PHP ÖеÄÒ»¿îÇ¿Ê¢µÄÈÕÖ¾¹¤¾ß£¬Laravel ʹÓà Monolog ×÷ΪÆäÈÕÖ¾×é¼þ¡£Monolog ÌṩÁ˶àÖÖ´¦ÀíÆ÷ºÍÃûÌÃÆ÷£¬ÔÊÐíÎÒÃǶÔÈÕÖ¾¾ÙÐÐÔ½·¢Ï꾡µÄÉèÖá£
ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Monolog À´´¦Àí²¢¼Í¼ÈÕÖ¾ÐÅÏ¢¡£Laravel ͨ¹ýÈÝÆ÷°ó¶¨ÊµÏÖÁË Monolog µÄ·â×°¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÈÝÆ÷°ó¶¨×Ô½ç˵ Monolog ʵÀý£¬²¢ÃüÃûÿ¸öʵÀý£¬ÒÔ±ãÔÚÓ¦ÓóÌÐòÖÐÒýÓÃËü¡£
ÈçÏÂËùʾ£¬ÎÒÃÇ¿ÉÒÔÔÚ AppServiceProvider Öаó¶¨Ò»¸öÐ嵀 Monolog ʵÀý£º
use MonologLogger; use MonologHandlerStreamHandler; public function register() { $this->app->bind('myLogger', function () { $log = new Logger('myLog'); $log->pushHandler(new StreamHandler(storage_path('logs/myLog.log')), Logger::INFO); return $log; }); }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÔÚÓ¦ÓóÌÐòÖÐʹÓøÃʵÀý¼Í¼ÈÕÖ¾ÐÅÏ¢¡£ÈçÏÂËùʾ£º
use IlluminateSupportFacadesLog; Log::channel('myLogger')->info('This is an info level message.');
µÇ¼ºó¸´ÖÆ
³ýÁËʹÓÃĬÈϵÄÈÕÖ¾ÉèÖÃÎļþ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃ×Ô½ç˵µÄÈÕÖ¾ÉèÖÃÎļþÀ´ÉèÖà Monolog¡£ÈçÏÂËùʾ£¬ÔÚ×Ô½ç˵ÈÕÖ¾ÉèÖÃÎļþÖÐʹÓà Monolog µÄ addRecord() ÒªÁìÌí¼ÓÈÕÖ¾ÐÅÏ¢£º
use MonologLogger; return [ 'myLog' => [ 'driver' => 'monolog', 'level' => 'debug', 'handler_with' => [ [ 'handler' => StreamHandler::class, 'options' => [ 'level' => Logger::INFO, 'stream' => storage_path('logs/mylog.log'), 'bubble' => true ] ] ], 'tap' => [MyLogChannel::class] ] ];
µÇ¼ºó¸´ÖÆ
ÐèҪעÖØ£¬ÕâÀïµÄ tap ÉèÖÃΪ×Ô½ç˵ Log channel ʵÀý¡£ÎÒÃDZØÐèÔÚÓ¦ÓóÌÐòÖÐ×¢²á¸ÃʵÀý£¬ÒÔ±ã¿ÉÒÔʹÓøÃʵÀýÀ´¼Í¼ÈÕÖ¾ÐÅÏ¢¡£
×ܽá
ÔÚ Laravel ÖУ¬¼Í¼ÈÕÖ¾ÊÇÓ¦ÓóÌÐò¿ª·¢µÄÐëÒªÊÂÇ顣ͨ¹ý¶ÔÈÕÖ¾ÉèÖÃÎļþµÄÉèÖúÍʹÓà Log Facade ºÍ Monolog£¬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ׵ؼͼÈÕÖ¾ÐÅÏ¢²¢¶ÔÆä¾ÙÐд¦Àí¡£
ËäÈ»£¬ÕâÀïÖ»ÏÈÈÝÁË Laravel дÈÕÖ¾µÄ×î»ù±¾ÒªÁ죬ÈôÊÇÐèÒª¸üÉîÈëµÄÏàʶ£¬¿ÉÒÔÉó²é Laravel ¹Ù·½Îĵµ»òËÑË÷Ïà¹Ø×ÊÁÏ¡£
ÒÔÉϾÍÊÇlaravelдÈÕÖ¾ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡