Log.php 5.28 KB
<?php

namespace common\exts\wechat;

use yii\helpers\FileHelper;
use yii\log\Logger;

/**
 * Class Log
 * @package common\exts\wechat
 */
class Log
{
    /*@inheritdoc*/
	private static $instance = null;

    /*@inheritdoc*/
	private function __construct(){}

    /*@inheritdoc*/
	private function __clone(){}

    /**
     * @var LogFileHandler
     */
    private $handler;

    private $level = 15;

    public $dirMode = 0755;

    /**
     * 一个文件最大容量 10M
     * @var int
     */
    public $maxFileSize = 5120; // in  KB

    /**
     * 最大文件数目 100
     * @var int
     */
    public $maxLogFiles = 10;

    /**
     * @var
     */
    public $logFile;

    /**
     * @param int $level
     * @return Log|null
     */
	public static function init($level = 15)
	{
		if(false == self::$instance instanceof self) {
			self::$instance = new self();
            if (self::$instance->maxLogFiles < 1) {
                self::$instance->maxLogFiles = 1;
            }
            if (self::$instance->maxFileSize < 1) {
                self::$instance->maxFileSize = 1;
            }

            /**
             * 获取当前logFile
             */
            $logFile = self::$instance->getLogFile();
            $handler = new LogFileHandler($logFile);
            self::$instance->__setHandle($handler);
            self::$instance->__setLevel($level);
		}

		return self::$instance;
	}

    /**
	 * 获取当前日志文件
     * @return bool|string
     * @throws \yii\base\Exception
     */
    protected function getLogFile()
    {
        if(null !==$this->logFile){
            return $this->logFile;
        }

        // runtime app 位置
        $root = \yii::$app->getRuntimePath();
        $wxlogs = $root . '/wxlogs';
        if (!is_dir($wxlogs)) {
            FileHelper::createDirectory($wxlogs, $this->dirMode, true);
        }

        // wxlogs/201603/03/18
        $logFile = $wxlogs . '/'. date('Ymd');

        // clear stat cache to ensure getting the real current file size and not a cached one
        // this may result in rotating twice when cached file size is used on subsequent calls
        // 先找数值最大的(最后创建的)
        for ($i = $this->maxLogFiles; $i >= 0; --$i) {

            // wxlogs/201601/03/18_9.log
            $file = $logFile . '_' . $i . '.log';

            // 如果log文件已经存在
            if (is_file($file)) {
                // 删除多出来的文件, 防止文件数量过多
                if ($i === $this->maxLogFiles) {
                    @unlink($file);
                } else {
                    // 清除文件计算缓存
                    clearstatcache();

                    // 如果当前文件大于 最大限制,而且已经是第大文件数,就到系统错误 log中
                    if (@filesize($file) > $this->maxFileSize * 1024) {
                        if($i === $this->maxLogFiles - 1){
                            \yii::getLogger()->log(
                                "微信LOG文件溢出,已经超出最大文件数:" . $this->maxLogFiles . ", 每个文件最大:" . $this->maxFileSize . "K",
                                Logger::LEVEL_ERROR
                            );
                        } else {
                            return $this->logFile = $logFile . '_' . ($i + 1) . '.log';
                        }
                    } else {
                        // 如果文件存在,又没有超过最大文件限制
                        return $this->logFile = $file;
                    }
                }
            } else {
                if($i === 0){
                    return $this->logFile = $file;
                }
            }
        }

        return $this->logFile;
    }

    /**
     * @param $handler
     */
	private function __setHandle($handler){
		$this->handler = $handler;
	}

    /**
     * @param $level
     */
	private function __setLevel($level)
	{
		$this->level = $level;
	}

    /**
     * @param $msg
     */
	public static function DEBUG($msg)
	{
		self::write(1, $msg);
	}

    /**
     * @param $msg
     */
	public static function WARN($msg)
	{
		self::write(4, $msg);
	}

    /**
     * @param $msg
     */
	public static function ERROR($msg)
	{
		$debugInfo = debug_backtrace();
		$stack = "[";
		foreach($debugInfo as $key => $val){
			if(array_key_exists("file", $val)){
				$stack .= ", file:" . $val["file"];
			}
			if(array_key_exists("line", $val)){
				$stack .= ", line:" . $val["line"];
			}
			if(array_key_exists("function", $val)){
				$stack .= ", function:" . $val["function"];
			}
		}
		$stack .= "]";
		self::write(8, $stack . $msg);
	}

    /**
     * @param $msg
     */
	public static function INFO($msg)
	{
		self::write(2, $msg);
	}

    /**
     * @param $level
     * @return string
     */
	private function getLevelStr($level)
	{
		switch ($level)
		{
		case 1:
			return 'debug';
		break;
		case 2:
			return 'info';	
		break;
		case 4:
			return 'warn';
		break;
		case 8:
			return 'error';
		break;
		default:
				
		}
	}

    /**
     * @param $level
     * @param $msg
     */
	protected function write($level, $msg)
	{
		// 初始化
		self::init();
		if(($level & self::$instance->level) == $level ) {
			$msg = '['.date('Y-m-d H:i:s').'][' . self::getLevelStr($level).'] '.$msg."\n";
			self::$instance->handler->write($msg);
		}
	}
}