Laravel 发送电子邮件进行日志记录而不是写入存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19458154/
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 08:34:53  来源:igfitidea点击:

Laravel to send email for logging instead of writing to storage

phplaravellaravel-4

提问by Wizzard

I've looked through the documentation and can't find a way to change the logging for any errors, which normally write to /app/storage/logs/logs/log-apache2handler-xxxx-xx-xx.txt

我查看了文档,但找不到任何方法来更改任何错误的日志记录,这些错误通常写入 /app/storage/logs/logs/log-apache2handler-xxxx-xx-xx.txt

but on the production server, I want any of those to be emailed to me instead.

但是在生产服务器上,我希望将其中任何一个通过电子邮件发送给我。

Is there a way to do this?

有没有办法做到这一点?

回答by Rufhausen

I still log the error, and if it's production, send an email with the exception:

我仍然记录错误,如果是生产,发送一封电子邮件,但有异常:

App::error(function(Exception $exception, $code)
{
Log::error($exception);

if (Config::getEnvironment() == 'production')
{
    $data = array('exception' => $exception);
    Mail::send('emails.error', $data, function($message)
    {
        $message->from($email_app);
        $message->to(Config::get('settings.error_email'))->subject(Config::get('settings.app_name') . ' Error');
    });
    Log::info('Error Email sent to ' . Config::get('settings.error_email'));
    return Response::view('errors.500', array(), 500);
}
});

回答by philsch

Due Laravel uses the Monolog library internally, I think it is a better solution to use the intended log handlers. So every uncatched exception and manual calls of Log will be send to your mail address.

由于 Laravel 在内部使用 Monolog 库,我认为使用预期的日志处理程序是更好的解决方案。因此,每个未捕获的异常和日志的手动调用都将发送到您的邮件地址。

if you're using mail()

如果您使用的是 mail()

Add NativeMailerHandler to your app/start/global.php:

将 NativeMailerHandler 添加到您的 app/start/global.php:

$logFile = 'log-'.php_sapi_name().'.txt';

Log::useDailyFiles(storage_path().'/logs/'.$logFile);
Log::getMonolog()->pushHandler(
    new Monolog\Handler\NativeMailerHandler(
        '[email protected]',
        '[Log] Some Subject',
        '[email protected]'
        Logger::ERROR, // set minimal log lvl for mail
        true, // bubble to next handler?
        70 // max column width in your mail
    )
);

if you've configured Laravel SMTP Mailing

如果您已配置 Laravel SMTP 邮件

Add SwiftMailerHandler to your app/start/global.php:

将 SwiftMailerHandler 添加到您的 app/start/global.php:

(Swift Mailer is also used internally by Laravel)

(Laravel 内部也使用 Swift Mailer)

$logFile = 'log-'.php_sapi_name().'.txt';

Log::useDailyFiles(storage_path().'/logs/'.$logFile);
Log::getMonolog()->pushHandler(
    new Monolog\Handler\SwiftMailerHandler(
        Mail::getSwiftMailer(),
        Swift_Message::newInstance('[Log] Some Subject')->setFrom('[email protected]')->setTo('[email protected]'),
        Logger::ERROR, // set minimal log lvl for mail
        true // bubble to next handler?
    )
);

Add environment check as described in the other post if you want to. For many other handlers, also have a look at https://github.com/seldaek/monolog

如果需要,请按照另一篇文章中的描述添加环境检查。对于许多其他处理程序,还可以查看https://github.com/seldaek/monolog

回答by Glad To Help

App::error(function(Exception $exception)
{
    //Log::error($exception);
    // send email
});

Check the docs here: http://laravel.com/docs/errors#handling-errors

在此处检查文档:http: //laravel.com/docs/errors#handling-errors

Basically it is up to you what logic to put in the handler. I think it may also be possible to create a separate global.php for production, without putting if-else statements to find out which environment you are on.

基本上,在处理程序中放入什么逻辑取决于您。我认为也可以为生产创建一个单独的 global.php,而不用放置 if-else 语句来找出您所在的环境。

回答by Dinesh Rabara

use package like

使用像这样的包

Begin by installing this package through Composer. Edit your project's composer.json file to require dinesh/bugonemail.

首先通过 Composer 安装这个包。编辑项目的 composer.json 文件以要求使用 dinesh/bugonemail。

Or visit

或访问

http://dineshrabara.github.io/bugonemail/

http://dineshrabara.github.io/bugonemail/