Phalcon:plugin combined with manager event management, dispatcher scheduling controller monitors SQL logging or other interception

Source: Internet
Author: User

Classes that are likely to be used

Phalcon\mvc\use\plugin

Phalcon\mvc\dispatcher as Mvcdispatcher

Phalcon\events\manager as Eventsmanager

Code section

public/index.php:

$di [' db '] = function () use ($di) {        //log       //Event Management       $eventManager = new \phalcon\events\manager ();        Database SQL Operation class inherited from plugins        $eventQSql = new \mydatabase ();        Attach a listener        $eventManager->attach ("db", $EVENTQSQL);        $db =new Dbadapter (            "host" +     "localhost",            "username" and "root", "            password" and "=",            "dbname"   = "Demo",            "CharSet" and "  UTF8"        );        Bind event        $db->seteventsmanager ($eventManager);        return $db;        };

  

app/plugins/mydatabase.php

Use Phalcon\db\profiler,    Phalcon\logger,    phalcon\logger\adapter\file as Loggerfile;class Mydatabase extends \phalcon\mvc\user\plugin {    protected $_logger;    protected $_profiler;    Public function __construct ()    {        $this->_profiler = $this->profiler;//new  \phalcon\db\profiler ();        $this->_logger = new Loggerfile (".. /log/sql.log ");    }    Public Function BeforeQuery ()    {        $this->_logger->log ($this->db->getsqlstatement   (), Logger::info);        $this->_profiler->startprofile ($this->db->getsqlstatement ());    }    Public Function Afterquery ()    {       $this->_logger->log ($this->db->getsqlvariables  (), Logger::info);        $this->_profiler->stopprofile ();    }}

Each time the SQL statement is executed, the SQL sent will be logged into the log file.

Phalcon:plugin combined with manager event management, dispatcher scheduling controller monitors SQL logging or other interception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.