Home php教程 PHP源码 高并发下PHP写文件日志丢失

高并发下PHP写文件日志丢失

May 23, 2016 pm 05:09 PM

php代码:

<?php
/**
 * Created by PhpStorm.
 * User: andyfeng
 * Date: 2015/6/24
 * Time: 13:31
 */
class LogFileUtil {

    public static $fileHandlerCache;
    private static $initFlag = false;
    private static $MAX_LOOP_COUNT = 3;

    private static function init() {
        self::$initFlag = true;
        register_shutdown_function(array("LogFileUtil", "shutdown_func"));
    }

    /**
     * 输出到文件日志
     * @param $filePath 文件路径
     * @param $msg  日志信息
     * @return int
     */
    public static function out($filePath, $msg) {
        if (!self::$initFlag) {
            self::init();
        }
        return self::internalOut($filePath, $msg);
    }

    /**
     * @param $filePath
     * @param $msg
     * @param $loop
     * @return int
     */
    private static function internalOut($filePath, $msg, $loop = 0) {
        //以防一直添加失败造成死循环
        if ($loop > self::$MAX_LOOP_COUNT) {
            $result = 0;
        } else {
            $loop++;
            $fp = self::$fileHandlerCache["$filePath"];
            if (empty($fp)) {
                $fp = fopen($filePath, "a+");
                self::$fileHandlerCache[$filePath] = $fp;
            }
            if (flock($fp, LOCK_EX)) {
                $result = fwrite($fp, $msg);
                flock($fp, LOCK_UN);
            } else {
                $result = self::internalOut($filePath, $msg, $loop);
            }
        }
        return $result;
    }

    function shutdown_func() {
        if (!empty(LogFileUtil::$fileHandlerCache)) {
            if (is_array(LogFileUtil::$fileHandlerCache)) {
                foreach (LogFileUtil::$fileHandlerCache as $k => $v) {
                    if (is_resource($v))
                        //file_put_contents("close.txt",$k);
                        fclose($v);
                }
            }
        }
    }
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)