Files.php 1.58 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 16/1/8
 * Time: 11:36
 */

namespace Hood\Utils\Statistic;


class Files extends WorkerAbstract
{

    static public function logs(Data $data)
    {
        self::write($data);
    }

    static public function write(Data $data)
    {
        $statistData = self::makeData($data);
        $uri = str_replace('/', '_', $data->uri);
        if (empty($uri)) $uri = 'system';
        if (!is_dir($data->statistic_path)) {
            umask(0);
            mkdir($data->statistic_path, 0777, true);
        }
        file_put_contents($data->statistic_path . $uri . "." . date('Y-m-d') . '.log', $statistData, FILE_APPEND | LOCK_EX);
    }

    static public function error($errno, $errstr, $errfile, $errline)
    {
        $data = new Data();
        $data->code = 500;
        $data->type = 'error';
        $data->data = array(
            'errno' => $errno,
            'errstr' => $errstr,
            'errfile' => $errfile,
            'errline' => $errline,
            'backtrace' => debug_backtrace()
        );
        self::write($data);
    }

    static public function exception(\Exception $exception)
    {
        $data = new Data();
        $data->code = 500;
        $data->type = 'exception';
        $data->data = array(
            'code' => $exception->getCode(),
            'message' => $exception->getMessage(),
            'file' => $exception->getFile(),
            'line' => $exception->getLine(),
            'trace' => $exception->getTrace(),
            'backtrace' => debug_backtrace()
        );
        self::write($data);
    }
}