PHP Framework added logging function class

Source: Internet
Author: User

<?php/* idea: Given file, write read (fopen, fwrite ...)  If more than 1M is to rewrite the backup to    a content,  determine the size, if greater than 1M, backup  is less than write */class log{//define a constant, create a file name const LOGFILE = ' curr.log ';/ Write file public static function write ($cont) {$cont. = "\ r \ n"; $log = Self::isbak ();//Calculate the address of the file, judge the size $fh = fopen ($log, ' ab ');//Open, Append mode fwrite ($FH, $cont); fclose ($FH);} Backup log public static function bak () {//give the path to the file, and change the original log//to date. bak form $log = ROOT. data/log/'. self::logfile; $bak = ROOT. ' data/log/'. Date (' Ymd '). Mt_rand (10000,99999). '. bak '; return rename ($log, $bak);} Determine if the log is larger than 1Mpublic static function Isbak () {//Determine if the file exists $log = ROOT. ' data/log/'. Self::logfile;if (!file_exists ($log)) {//If not present, create the file Touch ($log);//Touch also has this command on Linux, which is to quickly build a file return $log;} Judge Size Clearstatcache (true, $log); When the cache is cleared, the. bak file is created $size = FileSize ($log), if ($size <= 1024*1024) {//If <=1m is written to return $log;}//To this line, stating greater than 1Mif (!self:: Bak ()) {return $log;} else {touch ($log); return $log;}}          }? >

Extreme method Test Call, loop execution 10,000 times

Another page//introduction log function Class Mysql{public functions query ($sql) {log::write ($sql);}} $mysql = new MySQL ();/**/for ($i =0; $i <10000; $i + +) {$sql = ' select Goods_id,goods_name,shop_price from Goodsselect Goods_id,goods_name,shop_price from Goodsselect goods_id,goods_name,shop_price from Goodsselect goods_id,goods_name, Shop_price from Goodsselect Goods_id,goods_name,shop_price to goodsselect goods_id,goods_name,shop_price from goods Where goods_id= '. rand (10000,99999); $mysql->query ($sql);} Log::write (' record '); echo "Execution complete";

PHP Framework added logging function class

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.