PHP Small Code---separate log records from slow log files and store them in an Excel file

Source: Internet
Author: User
Tags fread

<?phpheader ("Content-type:text/html; charset=utf-8"); Ini_set (' Max_execution_time ',  ' 10000 '); ini_ Set (' Memory_limit ', ' 1024M '); Function dealslowcontent ($content)  {     $data  =  array ();     preg_match ('/#\[email protected]:(. +) \n#\sthread_id/',  $ content,  $match);     $data [' [email protected] '] =  $match [1];     preg_match ('/#\squery_time:\s (. +) \slock_time/',  $content,  $match);      $data [' query_time '] =  $match [1];    preg_match ('/\slock_time:\s (. +) \ srows_sent/',  $content,  $match);     $data [' lock_time '] =  $match [1];     preg_match ('/\srows_examined:\s (. +) \srows_affected/',  $content,  $match);      $data [' rows_examined '] =  $match [1];    preg_match ('/\ srows_read:\s (. +) \n#\sbytes_sent/',  $content,  $match);     $data [' Rows_read '] =  $match [1];     preg_match ('/timestamp= (\d+); \s/',  $content,  $match);     $data [' Timestamp '] =  $match [1];    preg_match ('/#\sbytes_sent:\s (. +) \sset/',  $ content,  $match);     $data [' bytes_sent '] =  $match [1];      $sqlPos  = strpos ($content,  '; ');      $data [' SQL '] = substr ($content,  $sqlPos  + 1, strlen ($ Content));    return  $data;} $slowData  = array (); $handle  = fopen (' mysql-slow.log.2 ',  ' R '); $content  =  "; $middlerCha  =  '; $identifier  =  ' # [email protected] '; $identifierLength  =  strlen ($identifier);while  (!feof ($handle))  {     $oneTakeCha  =  Fread ($handle,  1);     if  ($oneTakeCha  ===  ' # ')  {        $ middlercha =  ' # '  . fread ($handle,  $identifierLength  - 1);         if  ($middlerCha  ===  $identifier)  {             if  ($content  !=  ")  {                  $slowData [] =  Dealslowcontent ($content);                  $content  =  $middlerCha;             } else {                  $content  =  $middlerCha;             }        } else {              $content. = $middlerCha;        }    }  else {         $content .=  $oneTakeCha;     }} $slowData [] = dealslowcontent ($content); fclose ($handle);include  ' libraries/phpexcel.php ';include  ' libraries/phpexcel/iofactory.php '; $objPHPExcel  = new phpexcel (); $objPHPExcel- >getproperties ()->settitle ("Export")->setdescription ("none"); $objPHPExcel->setactivesheetindex (0); $objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth, $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (+), $objPHPExcel->getactivesheet ()->getcolumndimension (' C ')- >setwidth, $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth ($objPHPExcel- >getactivesheet ()->getcolumndimension (' E ')->setwidth (+), $objPHPExcel->getactivesheet ()->getcolumndimension (' F ')- >setwidth, $objPHPExcel->getactivesheet ()->getcolumndimension (' G ')->setwidth, $objPHPExcel- >getactivesheet ()->getcolumndimension (' H ')->setwidth (), $titlearray  = array (      "host Information"  =>  ' [email protected] ',  "Query Time"  =>  ' Query_time ',  " Lock table Time " =>  ' Lock_time '     , " check row count on query " =>  ' rows_examined ',   "Read rows"  =>  ' Rows_read ',  "time"  =>  ' timestamp ',     "Bytes sent " =>  ' bytes_sent ', " executed SQL " =>  ' sql '); $col  = 0;foreach  ($ titlearray as  $key  =>  $title)  {     $objPHPExcel Getactivesheet ()->setcellvaluebycolumnandrow ($col, 1,  $key);     $col + +;} $j  = 2;foreach  ($slowData  as  $key  =>  $sd)  {     $col  = 0;     foreach  ($titlearray  as  $key 2 =>  $title)  {          $objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col,  $j,  $ sd[$title]);         $col ++;    }      $j + +;} $objPHPExcel->setactivesheetindex (0); $objWriter  = iofactory::createwriter ($objPHPExcel,  ' Excel5 ');// sending headers to force the user to download the  fileheader (' Content-type: application/vnd.ms-excel '); header (' content-disposition: attachment; Filename= "Slowsql.xls"); header (' cache-control: max-age=0 '); $objWriter->save (' php://output ');


This article is from my It career blog, so be sure to keep this source http://quietnight.blog.51cto.com/7163892/1694964

PHP Small Code---separate log records from slow log files and store them in an Excel file

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.