laravelÖÐÈÕÖ¾ÊÇÔõÑùʵÏÖµÄ
laravelÊÇÏÖÔÚºÜÊÇÊܽӴýµÄphp¿ò¼Ü£¬ËüµÄÒ»¸ö×îÖ÷ÒªµÄÌØÕ÷¾ÍÊÇËüµÄÇ¿Ê¢µÄÈÕ־ϵͳ¡£laravelÈÕ־ϵͳʮ·ÖÇ¿Ê¢ÇÒÒ×ÓÚά»¤£¬»¹Ö§³Ö¶àÖÖÈÕÖ¾´¦Àí·½·¨£¬ÈçÎļþ¡¢Êý¾Ý¿âµÈ¡£±¾ÎĽ«ÉîÈë̽ÌÖlaravelÖÐÈÕÖ¾ÊÇÔõÑùʵÏֵġ£
ʲôÊÇLaravelÈÕÖ¾
ÔÚÈí¼þ¿ª·¢ÖУ¬ÈÕ־ͨ³£ÊÇÖ¸ÔÚÔËÐÐʱ´úΪÌض¨Ðж¯»òÊÂÎñ£¨ÈçÇëÇó¡¢¹ýʧ»òÒì³££©¼Í¼µÄÐÅÏ¢¡£¼Í¼ÕâЩÊÂÎñÓÐÖúÓÚ¿ª·¢Ö°Ô±ÏàʶϵͳµÄÔËÐÐÇéÐÎÒÔ¼°¶¨Î»¹ýʧ¡£ÔÚLaravelÖУ¬ÈÕÖ¾ÊÇÒ»ÖּͼϵͳµÄÐÐΪµÄ»úÖÆ£¬¿ÉÒÔÌåÏÖΪһ×éÓйØÈÕÖ¾¼Í¼µÄÉèÖúÍʵÏÖ¡£
LaravelÖеÄÈÕÖ¾ÊÇÒ»ÖּͼϵͳÊÂÎñµÄ»úÖÆ£¬Ö¼ÔÚΪ¿ª·¢Ö°Ô±Ìṩ׼ȷ²¢Ò×ÓÚÚ¹Ê͵ÄÊý¾Ý£¬ÒÔ×ÊÖúËûÃÇÔÚ¿ª·¢Ê±´úµ÷ÊÔºÍɨ³ý¹ýʧ¡£LaravelÖ§³Ö¶àÖÖÈÕÖ¾´¦Àí·½·¨£¬ÈçÎļþ¡¢Êý¾Ý¿â¡¢syslogºÍstdout¡£
LaravelÖÐÈÕÖ¾µÄʵÏÖ
LaravelÖеÄÈÕ־ϵͳÓÉÁ½¸öÖ÷Òª²¿·Ö×é³É£º¼Í¼Æ÷(Logger)ºÍ´¦ÀíÆ÷(Handler)¡£ËüÃǵÄ×÷Óû®·ÖÊǼͼÈÕÖ¾ºÍ´¦ÀíÈÕÖ¾µÄÊä³öЧ¹û¡£
¼Í¼Æ÷(Logger)
ÔÚLaravelÖУ¬¼Í¼Æ÷(Logger)ÀàÈÏÕæÈÕÖ¾µÄ¼Í¼¡£Í¨¹ý¼Í¼Æ÷£¬ÎÒÃÇ¿ÉÒÔÔÚLaravelÖмͼÖÖÖÖÀàÐͺͼ¶±ðµÄÈÕÖ¾£¬Èçdebug¡¢info¡¢warning¡¢errorµÈ¡£
¼Í¼Æ÷µÄAPIÊǺÜÊǼòÆÓÒ×Óõģ¬ÏñÏÂÃæÕâÑùµÄʾÀý´úÂë¾Í¿ÉÒÔʹÓüͼÆ÷¼Í¼һÌõÐÅÏ¢£º
Log::info('This is an informational message');
µÇ¼ºó¸´ÖÆ
´¦ÀíÆ÷(Handler)
´¦ÀíÆ÷(Handler)ÀàÊÇLaravelÖеÄÊä³öʵÏÖ£¬ËüÈÏÕæÈÕÖ¾Êä³öµÄÄ¿µÄµØºÍÃûÌá£LaravelÖÐÖ§³Ö¼¸ÖÖ³£¼ûµÄÊäÀ´ÓÉÖÃÆ÷£¬°üÀ¨Îļþ¡¢syslogºÍstderrµÈ¡£´¦ÀíÆ÷µÄÄ¿µÄÊÇΪ¼Í¼Æ÷ÌṩÊä³öÑ¡Ïî¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐ޸Ĵ¦ÀíÆ÷ÉèÖÃÐÅÏ¢À´ÐÞËûÈÕÖ¾µÄÊä³ö·½·¨¡£
´¦ÀíÆ÷ÉèÖÃÐÅϢͨ³£ÓÉÈÕ־ʵÀý¾ÙÐÐÖÎÀí£¬ÈçÏÂÃæµÄ´úÂëƬ¶ÏËùʾ£º
use MonologLogger; $logger = new Logger('name'); $handler = new StreamHandler('path/to/your.log', Logger::WARNING); $logger->pushHandler($handler);
µÇ¼ºó¸´ÖÆ
Õâ¸ö´úÂëƬ¶ÏÑÝʾÁËÔõÑùÉèÖÃLaravelÖеÄÎļþ´¦ÀíÆ÷¡£Õâ¶Î´úÂ뽨ÉèÁËÒ»¸ö¼Í¼Æ÷ʵÀý£¬ÉèÖÃΪWARNING¼¶±ð£¬²¢½«ÈÕÖ¾Êä³öµ½Ö¸¶¨µÄÎļþ¡£
ĬÈÏÇéÐÎÏ£¬Laravel½«ÈÕÖ¾¼Í¼µ½storage/logs/Laravel.logÎļþÖС£È»¶ø£¬ÄãÒ²¿ÉÒÔ½«ÈÕÖ¾¼Í¼µ½ÆäËüÎļþÖлòÕßʹÓÃ×Ô½ç˵µÄÈÕÖ¾Êä³ö¡£
ÉèÖÃÈÕÖ¾ÐÅÏ¢
ÔÚLaravelÖУ¬¿ÉÒÔͨ¹ýconfig/logging.phpÎļþÀ´ÉèÖÃÈÕÖ¾ÐÅÏ¢¡£Ä¬ÈÏÇéÐÎÏ£¬´ËÎļþ°üÀ¨ÒÔÏ´¦ÀíÆ÷:
‘stack’ – ¾ÛºÏ´¦ÀíÆ÷£¬¿ÉÒÔ½«¶à¸ö´¦ÀíÆ÷×éºÏÔÚÒ»Æð¡£
‘single’ – ½«ÈÕÖ¾¼Í¼Ϊһ¸öµ¥¶ÀµÄÎļþ¡£
‘daily’ – ÈÕÖ¾¼Í¼·ÖΪÖðÈյĵ¥¶ÀÎļþ¡£
‘stderr’ – ½«ÈÕÖ¾¼Í¼µ½stderr¶ø²»ÊÇÎļþÖС£
‘syslog’ – ½«ÈÕÖ¾¼Í¼µ½syslog¡£
Äã¿ÉÒÔʹÓÃLaravelÉèÖÃÎļþÀ´×Ô½çËÃ÷ÈÕâЩ´¦ÀíÆ÷µÄÊôÐÔ£¬ÒÔÖª×ãÄãµÄÌض¨ÐèÇó¡£ÀýÈ磬Äã¿ÉÒÔʹÓÃÒÔÏ´úÂ뽫ÈÕÖ¾¼Í¼Ϊһ¸öÿÖܵĵ¥¶ÀÎļþ£º
<?php return [ 'channels' => [ 'weekly' => [ 'driver' => 'daily', 'path' => storage_path('logs/weekly.log'), 'level' => 'debug', 'days' => 7, ], ], ];
µÇ¼ºó¸´ÖÆ
ÔÚLaravelÖУ¬ÕâÑù¾Í¿ÉÒÔͨ¹ýÒÔÏÂÒªÁìÀ´Ê¹ÓÃ×Ô½ç˵µÄ´¦ÀíÆ÷£º
Log::channel('weekly')->info('This is a custom log message');
µÇ¼ºó¸´ÖÆ
Laravel Monolog
ͨ¹ýÉÏÎÄ£¬ÎÒÃÇÒѾÏàʶÁËLaravelÖÐÈÕÖ¾µÄʵÏÖ·½·¨¡£¿ÉÊÇ£¬ÒªÏàʶLaravelÖÐÈÕÖ¾µÄÍêÕûÀú³Ì£¬ÎÒÃÇ»¹ÐèÒªÏàʶMonolog¡£MonologÊÇPHPÖÐÒ»¸öÆÕ±éʹÓõÄÈÕÖ¾¿â£¬Ëü¶ÔÈÕÖ¾¼Í¼¹¦Ð§¾ÙÐÐÁË·â×°£¬²¢ÌṩÁ˶àÖÖ´¦ÀíÆ÷Ñ¡Ïî¡£
Laravelͨ¹ýÒÀÀµ×¢ÈëµÄ·½·¨Ê¹ÓÃMonolog£¬ÌṩµÄĬÈϼͼÆ÷ÀàÐÍÊÇMonologLogger¡£ÈôÊÇÄãÏëÒªÔ½·¢ÉîÈëÏàʶMonolog£¬¿ÉÒÔÈ¥¿´¿´ËüµÄÎĵµ¡£
×ܽá
ͨ¹ýÉÏÎĵÄÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔ¿´³öLaravelÖÐÈÕ־ϵͳµÄÇ¿Ê¢Ö®´¦¡£LaravelÈÕ־ϵͳ½«MonologµÄÈÕÖ¾¹¦Ð§¾ÙÐÐÁË·â×°£¬²¢ÌṩÁ˶àÖÖÈÕÖ¾´¦ÀíÆ÷Ñ¡ÏʹµÃLaravelÔÚÈÕÖ¾·½ÃæÔ½·¢Ò×ÓÚʹÓúÍά»¤¡£
ÔÚÏÖʵӦÓÃÖУ¬ÈÕÖ¾¼Í¼ÊǺÜÊÇÖ÷ÒªµÄÒ»¸ö×é³É²¿·Ö£¬¿ÉÒÔÀû±ãÎÒÃÇ¿ì½ÝµØ¶¨Î»¹ýʧϢÕù¾öÎÊÌâ¡£LaravelµÄÈÕ־ϵͳ¸üÊÇÇ¿Ê¢µ½ÈÃÎÒÃÇ°®²»ÊÍÊÖ£¬×ÊÖúÎÒÃÇÇáËɵض¨Î»ÎÊÌâÏ¢Õù¾öÎÊÌâ¡£
ÒÔÉϾÍÊÇlaravelÖÐÈÕÖ¾ÊÇÔõÑùʵÏֵĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡