Laravel 中的自定义错误日志 - 针对不同的错误类型使用不同的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24484301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:43:21  来源:igfitidea点击:

Custom Error Logging in Laravel - Using different files for different error types

phplaravelmonolog

提问by Vikalp Jain

I am using laravel 4 for my project. I would like to log different kind of error to different files. For example, I am using PayPal to accept payments. Any error during payment processing, I will like to log to a different file (other than generic daily log file I am using). Is it doable ? I've tried a lot to figure it out. Everyone talks about creating your own error handler, but how do I specify that error handler to use a different log file ?

我正在为我的项目使用 laravel 4。我想将不同类型的错误记录到不同的文件中。例如,我使用 PayPal 接受付款。付款处理过程中出现任何错误,我想记录到不同的文件(我正在使用的通用每日日志文件除外)。可行吗?我已经尝试了很多来弄清楚。每个人都在谈论创建自己的错误处理程序,但如何指定该错误处理程序以使用不同的日志文件?

回答by Sebastian Kraft

Here http://laravel.io/forum/02-09-2014-laraverl-custom-logsthats how you can create a new Logger.

这里http://laravel.io/forum/02-09-2014-laraverl-custom-logs这就是你如何创建一个新的 Logger。

回答by lennondtps

If you want to keep Daily Logs in seperated files / folder you can doing it using RotatingFileHandler.

如果您想将每日日志保存在单独的文件/文件夹中,您可以使用 RotatingFileHandler 来完成。

use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;

$log = new Logger('name');
$log->pushHandler(new RotatingFileHandler(storage_path().'/logs/folder_name/custom_log.log',2, Logger::INFO)); 

$log->info("This is a log");

In this example it creates a file in app/storage/logs/folder_name , and keep daily files for 2 days.

在此示例中,它在 app/storage/logs/folder_name 中创建一个文件,并将每日文件保留 2 天。