<?php function write_log($log_content) { $log_file = '/logs/error.log'; if(is_file($log_file)) { // 检测log文件大小,将每个log文件控制在2m以内 $log_filesize = filesize($log_file); $max_size = 2 * 1024 * 1024; // 可以接受的最大的文件大小 if($log_filesize >= $max_size) { $new_log_file = '/logs/error_' . date('YmdHis') . '.log'; rename($log_file, $new_log_file); } } $fp=fopen($log_file,'a+'); if($fp){ $startTime=microtime(); do{ // 这个循环可以保证进程在尝试1m后,如果未能锁定文件,则放弃写入日志的操作 $canWrite=flock($fp,LOCK_EX); if(!$canWrite){ usleep(round(rand(0,100)*1000)); } }while((!$canWrite)&&((microtime()-$startTime)<1000)); if($canWrite){ $content = date('Y-m-d H:i:s') . ' ' . $log_content . "\r\n"; fwrite($fp,$content); } fclose($fp); } }
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号