一,相关文档:
https://learnku.com/docs/laravel/10.x/errors/14857#9e8f93
二,php代码:
1,代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class NewsController extends Controller
{
//启用事务
public function trans(Request $request ) {
//启动事务
DB::beginTransaction();
$model = new News();
try {
//insert
$row1 = [ 'title' => '1title' , 'url' => '1url' ];
$model ->add( $row1 );
//news_id为1的主键记录已存在,会引发异常
$row2 = [ 'news_id' =>1, 'title' => '2title' , 'url' => '2url' ];
$model ->add( $row2 );
//commit
DB::commit();
return '已提交' ;
} catch (\Throwable $e ) {
Log::channel( 'business' )->error( $e ->__toString);
//report($e);
DB::rollback();
return '已回滚' ;
}
}
|
2,说明:
捕捉到异常后需要我们自己记录,
report($e); 是用助手函数report把异常记录到laravel默认的日志文件,通常位于storage/logs/laravel.log文件
Log::channel(‘business’)->error($e->__toString); 可以自己指定日志的channel,从而记录到相应的日志中
三,查看效果
1,查看默认日志:
liuhongdi@lhdpc:/data/laravel/dignews/storage/logs$ tail -100 laravel.log
#16 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#17 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#18 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#19 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#20 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#21 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#22 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
2,查看自定义日志
liuhongdi@lhdpc:/data/laravel/logs$ tail -100 image-2023-10-19.log
#15 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Routing/Router.php(799): Illuminate\Routing\Route->run()
#16 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#17 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#18 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\Routing\Middleware\SubstituteBindings->handle()
#19 /data/laravel/dignews/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
...
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/19/laravel-bu-zhuo-yi-chang-ji-lu-dao-ri-zhi/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
四,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
标签:laravel,Pipeline,Illuminate,10.27,dignews,日志,php,data
From: https://www.cnblogs.com/architectforest/p/17780034.html