monolog使用

网上文章一大抄,搜索下monolog,打开前10个站点的页面,都是同一篇。。。。。

下面记录一个,调用pdo写入数据库的例子

    <?php

    use Monolog\Logger;
    use Monolog\Handler\AbstractProcessingHandler;

    class PDOHandler extends AbstractProcessingHandler
    {
        private $initialized = false;
        private $pdo;
        private $statement;

        public function __construct(PDO $pdo, $level = Logger::DEBUG, $bubble = false)
        {
            $this->pdo = $pdo;
            parent::__construct($level, $bubble);
        }
        
        protected function write(array $record)
        {   
             if (!$this->initialized) {
                 $this->initialize();
             }

           $this->statement->execute(array(
                'channel' => $record['channel'],
                'level' => $record['level'],
                'message' => $record['formatted'],
                'time' => $record['datetime']->format('U'),             
           ));
        }
        
        private function initialize()
        {
            $this->pdo->exec(
                'CREATE TABLE IF NOT EXISTS monolog ' .'(channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED)'
            );
            $this->statement = $this->pdo->prepare(
                'INSERT INTO monolog (channel, level, message, time) VALUES (:channel, :level, :message, :time)'
            );

            $this->initialized = true;
        }
    }

文章参考:http://www.tuicool.com/articles/INFRvqU

发表评论

电子邮件地址不会被公开。 必填项已用*标注