<?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