thinkphp5使用monolog

swiftmailer使用说明:http://blog.csdn.net/edu_enth/article/details/53114818

因为monolog这么说的:This library implements the PSR-3 interface that you can type-hint against in your own libraries to keep a maximum of interoperability. You can also use it in your applications to make sure you can always use another compatible logger at a later time. As of 1.11.0 Monolog public APIs will also accept PSR-3 log levels. Internally Monolog still uses its own level scheme since it predates PSR-3.

啥意思?此库实现了PSR-3 接口,既PHP规范PSR-3日志接口,反正就是日志接口,自己百度详细了解。所以步骤如下:

1、下载PSR3: https://github.com/php-fig/log    log-master.zip解压后只要Psr文件夹

2、下载monolog: https://github.com/Seldaek/monolog  解压后只需要monolog-master\src\下的Monolog文件夹

3、将这两个文件夹放在thinkphp5的extend目录下,让这个类被thinkphp自动加载(就是和application和public并列的那个extend)

4、thinkphp在任意一个控制器调用:

namespace app\index\controller;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SwiftMailerHandler;
use Monolog\Handler\MysqlHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\HtmlFormatter;
use Monolog\Processor\MemoryUsageProcessor;
require_once 'swiftmainler/swift_required.php';
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
use Swift_Attachment;
class Index
{

    public function index()
    {
        $transport = Swift_SmtpTransport::newInstance('smtp.163.com', 25);
        $transport->setUsername('[email protected]');
        $transport->setPassword('163的授权码');
        $transport->setEncryption('tls');
        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance();
        $message->setFrom(array('[email protected]' => 'w'));
        $message->setTo(array('[email protected]' => 'Mr.Right', '[email protected]' => 'Mr.Wrong'));
        $message->setSubject("任务反馈");
        $message->setBody('这是一个测试', 'text/html', 'utf-8');
        // the default date format is "Y-m-d H:i:s"
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $formatter = new LineFormatter("%datetime% > %level_name% > %message% %context% %extra%\n", "Y n j, g:i a");
//        $formatter = new HtmlFormatter();
//        $sql = new MysqlHandler('logger', Logger::DEBUG);
        $swiftHandler = new SwiftMailerHandler($mailer,$message,Logger::DEBUG);
        $stream = new StreamHandler('/log/logger.log', Logger::DEBUG);
        $stream->setFormatter($formatter);
        // bind it to a logger object
        $logger = new Logger('security');
        $logger->pushHandler($stream);
//        $logger->pushHandler($sql);
        $logger->pushHandler($swiftHandler);
        $logger->pushProcessor(new MemoryUsageProcessor());
        $logger->pushProcessor(function ($record) {
            $record['extra']['dummy'] = 'Hello world!';
            return $record;
        });

        $logger->debug('it is a error');
    }

你可能感兴趣的:(thinkPHP5)