“ message”:“调用成员函数warning()时为null”,

时间:2018-08-24 10:46:32

标签: laravel

这是我的laravel模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Library\Log as MailLog;

class SendingServerPhpMail extends SendingServer
{
    protected $table = 'sending_servers';

    public function send($message, $params = array())
    {
        try {
            $transport = \Swift_MailTransport::newInstance();

            // Create the Mailer using your created Transport
            $mailer = \Swift_Mailer::newInstance($transport);

            // Actually send
            $sent = $mailer->send($message);
            if ($sent) {
                MailLog::info('Sent!');

                return array(
                    'status' => self::DELIVERY_STATUS_SENT,
                );
            } else {
                **MailLog::warning('Sending failed');**

                return array(
                    'status' => self::DELIVERY_STATUS_FAILED,
                    'error' => 'Unknown SMTP error',
                );
            }
        } catch (\Exception $e) {
            MailLog::warning('Sending failed');
            MailLog::warning($e->getMessage());

            return array(
                'status' => self::DELIVERY_STATUS_FAILED,
                'error' => $e->getMessage(),
                'message_id' => null,
            );
        }
    }
}
?>

这是我正在用作邮件日志的库

<?php

namespace App\Library;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;


class Log
{
    public static $logger;
    public static $path;


    public static function warning($message)
    {
        self::$logger->warning($message);
    }

}
?>

我在调用邮件日志库中声明的警告功能时遇到此错误。您可以在调用警告功能的地方看到模态 “ message”:“调用成员函数warning()时为null”

在模式中,我使用了邮件日志库,此后,我在模型中调用警告和其他函数,该模型在邮件日志库中定义。 但是对于警告功能,我收到错误消息,在null上调用成员函数警告()

1 个答案:

答案 0 :(得分:0)

我认为这是因为App \ Library \ Log类中的$ logger从未设置

public static function warning($message)
{
    if (! self::$logger) {
        self::$logger = new Logger('name');
        self::$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
    }
    self::$logger->warning($message);
}

希望这会有所帮助