Advertisement
rfv123

PHP Interface: Control mysql.general_log access

Sep 13th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.24 KB | None | 0 0
  1. <?php //  P:\developer\xampp\htdocs\testmysql\Q32451215\MysqlGeneralLogging.php
  2.  
  3. /**
  4.  * IMysqlGeneralLogging
  5.  *
  6.  * Control the MySQL General Logging Facilities.
  7.  *
  8.  * 1) Control whether to log trace information to the `mysql.general_log` table
  9.  * 2) Control whether to log trace information to a `trace file`
  10.  *
  11.  * Once tracing has started then ALL activity on ANY connection will be logged!
  12.  *
  13.  * Trace file:
  14.  *
  15.  * o When you create the instance you need to supply a directory to store the trace files in.
  16.  *
  17.  * o The default trace file name has the following format:
  18.  *     mysql-log-trace-pdo- <YYYYMMDD-HHMM-SS-nnnnnn> ['-', <User supplied text>] '.txt';
  19.  *     Note: `nnnnnn` is microtime to make the trace files unique.
  20.  *
  21.  *     You can provide your own trace file if you wish (see setLogFile($fullPath).
  22.  *
  23.  * o Creating an instance:
  24.  *    1) MysqlGeneralLogging::newInstanceFromPDO(\PDO $pdoConnection,
  25.                                                   $logFileDirectory);
  26.  *
  27.  *       where: The connection must have the privilege to set `Global Variable`.
  28.  *
  29.  *              The connection is only used to control logging do not use it
  30.  *              for anything else.
  31.  *
  32.  *              The `logFileDirectory` is where the trace files will be generated.
  33.  *
  34.  *     2) MysqlGeneralLogging::newInstanceFromParams($host, $username, $password, $dbname,
  35.  *                                                  $logFileDirectory);
  36.  *
  37.  *       where: The params are the normal details to create a PDO connection.
  38.  *
  39.  * Tracing:
  40.  *   The easiest way is to trace to the table `mysql`.`general_log`...
  41.  *
  42.  *   1) Create connection:
  43.  *      $logging = MysqlGeneralLogging::newInstanceFromPDO(\PDO $pdoConnection,
  44.  *                                                         $logFileDirectory);
  45.  *
  46.  *   2) $logging->startLoggingToTable(); // this will truncate the table by default
  47.  *
  48.  *   3) Run queries with  a separate connection.
  49.  *
  50.  *   4) $logging->stopLogging();
  51.  *
  52.  *   5) $traceContents = $logging->extractLogTable(); // trace table to PHP array.
  53.  *
  54.  *  // ---------------------------------------------------
  55.  *
  56.  *   Using a Trace file...
  57.  *
  58.  *   1) Create connection:
  59.  *      $logging = MysqlGeneralLogging::newInstanceFromPDO(\PDO $pdoConnection,
  60.  *                                                         $logFileDirectory);
  61.  *
  62.  *   2) Create a new Trace file:
  63.  *      $logFile = $logging->newTraceFilename();
  64.  *
  65.  *   3) set the new log file
  66.  *       $logging->setLogFile($logFile);
  67.  *
  68.  *       Note: use $logging->logFile() to get the current log file name
  69.  *
  70.  *
  71.  *   4) $logging->startLoggingToFile(); // this will truncate the file by default
  72.  *
  73.  *   5) Run queries with a separate connection.
  74.  *
  75.  *   6) $logging->stopLogging();
  76.  *
  77.  *   7) Examine the Trace file
  78.  *
  79.  *  // ---------------------------------------------------
  80.  *
  81.  * Documentation Links:
  82.  *
  83.  *  5.1.4 Server System Variables - https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html
  84.  *
  85.  *  5.2 MySQL Server Logs         - https://dev.mysql.com/doc/refman/5.5/en/server-logs.html
  86.  *
  87.  *  5.2.3 The General Query Log   - https://dev.mysql.com/doc/refman/5.5/en/query-log.html
  88.  *
  89.  *  Useful Stackoverflow Questions and Answers:
  90.  *
  91.  *  How to show the last queries executed on MySQL? -  http://stackoverflow.com/a/678310/3184785
  92.  *
  93.  *
  94.  * @author rfv
  95.  */
  96.  
  97. interface IMysqlGeneralLogging {
  98.  
  99.     /**
  100.      * Create an instance from an existing PDO connection.
  101.      * Need privilege to: set global variables`
  102.      *
  103.      * @param \PDO $pdoConnection
  104.      * @param type $logFileDirectory
  105.      * @return instance
  106.      */
  107.     public static function newInstanceFromPDO(\PDO $pdoConnection,
  108.                                                    $logFileDirectory);
  109.  
  110.     /**
  111.      * Create an instance fron user login details.
  112.      * Need privilege to: set global variables`
  113.      *
  114.      * @param type $host
  115.      * @param type $username
  116.      * @param type $password
  117.      * @param type $dbname
  118.      * @param type $logFileDirectory
  119.      * @return new instance
  120.      */
  121.     public static function newInstanceFromParams($host, $username, $password, $dbname,
  122.                                                       $logFileDirectory);
  123.  
  124.  
  125.     /* -----------------------------------------------------------------------
  126.      * `mysql`.`general_log` table routines
  127.      */
  128.  
  129.     /**
  130.      * Tell the server to start logging to the `mysql.general_log` table
  131.      * see: variable: `log_output`
  132.      *                `general_log_file`
  133.      *                `general_log`
  134.      *
  135.      * @param boolean $truncate
  136.      * @return boolean
  137.      */
  138.     public function startLoggingToTable($truncate = true);
  139.  
  140.  
  141.     /**
  142.      * Stop tracing completely.
  143.      * see: variable: `log_output`
  144.      *                `general_log`
  145.      *
  146.      * @return boolean
  147.      */
  148.     public function stopLogging();
  149.  
  150.     /**
  151.      * Truncate the `mysql.general_log` table.
  152.      *
  153.      * @return boolean
  154.      */
  155.     public function clearTableLog();
  156.  
  157.     /**
  158.      * Extract all the current contents of the `mysql.general_log` table
  159.      *
  160.      * @return  array
  161.      */
  162.     public function extractLogTable();
  163.  
  164.  
  165.     /* -----------------------------------------------------------------------
  166.      *  Tracefile routines
  167.      */
  168.  
  169.     /**
  170.      * Created a new Trace filename
  171.      *
  172.      * @param type $id
  173.      * @return string
  174.      * @throws ErrorException
  175.      */
  176.     public function newTraceFilename($id = '');
  177.  
  178.  
  179.     /**
  180.      * Sets the trace file in the MySQL server
  181.      * see: variable:  `general_log_file`
  182.      *
  183.      * @param type $fullPath
  184.      * @return type
  185.      * @throws \ErrorException
  186.      */
  187.     public function setLogFile($fullPath = '');
  188.  
  189.  
  190.     /**
  191.      * Tell the server to start logging to the trace file.
  192.      * see: variable: `log_output`
  193.      *                `general_log_file`
  194.      *                `general_log`
  195.      *
  196.      * @param  boolean $truncate - truncate the file - default
  197.      * @return boolean
  198.      * @throws \Exception\InvalidArgumentException
  199.      */
  200.     public function startLoggingToFile($truncate = true);
  201.  
  202.  
  203.     /**
  204.      * The current Trace Filename
  205.      *
  206.      * @return string
  207.      */
  208.     public function logFile();
  209.  
  210.  
  211.     /**
  212.      * Truncate the current trace file.
  213.      *
  214.      * @return boolean
  215.      */
  216.     public function clearFileLog();
  217.  
  218.     /**
  219.      * set the trace file directory
  220.      *
  221.      * @param type $dir
  222.      */
  223.     public function setLogFileDirectory($dir);
  224.  
  225.  
  226.     /**
  227.      * Are we currently logging?
  228.      *
  229.      * @return boolean
  230.      */
  231.     public function isLogging();
  232.  
  233.     /**
  234.      *  Is the trace destination a File?
  235.      *
  236.      * @return boolean
  237.      */
  238.     public function isLoggingToFile();
  239.  
  240.     /**
  241.      *  Is the trace destination the `mysql.general_log` table?
  242.      *
  243.      * @return boolean
  244.      */
  245.     public function isLoggingToTable();
  246.  
  247.     /**
  248.      * Truncate Table and Trace file.
  249.      */
  250.     public function clearLogs();
  251.  
  252.     /**
  253.      * Extract the current server `logging` variables.
  254.      * @return array
  255.      */
  256.     public function currentSettings();
  257.  
  258.     /**
  259.      * Only delete files ending in 'txt';
  260.      */
  261.     public function deleteAllTraceFiles($fail = false);
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement