log分析基类

以下是log分析的基类 可以自定义扩展。需要实现以下方法

formatData($data, $type) 
<?php
abstract class log {
    //文本
    public $text = '';
    //删除的条件含有的内容删除当前条
    protected $removeCondition = array();
    //删除的数据
    public $removeData = array();
    //分组条件
    protected $group = '';
    //分条条件
    protected $line = '';
    //替换条件 替换为空
    protected $replace = array();
    //调用formatData表示全局调用
    protected $typeAll = 'all';
    //调用formatData表示分组调用
    protected $typeGroup = 'group';

    //需要实现的数据格式化方法
    abstract public function formatData($data, $type);

    /**
     * 设置文本内容
     * @param $file 文件地址
     * @return $this 自己的指针
     */
    public function setTextByFiles($files) {
        if (is_array($files)) {
            foreach ($files as $item) {
                $this->text .= file_get_contents($item);
            }
        } else {
            $this->text .= file_get_contents($files);
        }

        return $this;
    }

    /**
     * 设置段落分割符
     * @param $paragraph 分割符
     * @return $this 自己的指针
     */
    public function setGroup($group) {
        $this->group = $group;

        return $this;
    }

    /**
     * 设置行分割符
     * @param $line 分割符
     * @return $this 自己的指针
     */
    public function setLine($line) {
        $this->line = $line;

        return $this;
    }

    /**
     * 设置替换内容
     * @param array|string $replace 内容
     * @return $this 自己的指针
     */
    public function setReplaces($replace) {
        $this->replace = $replace;

        return $this;
    }

    /**
     * 设置删除行的条件
     * @param array $condition 删除行的条件 例array('abc' => '%', 'bcd'=> '=') %表示包含, =表示等于
     * @return $this 自己的指针
     */
    public function setRemoveCondition(array $condition) {
        $this->removeCondition = $condition;

        return $this;
    }

    /**
     * 运行
     */
    public function run() {
        if (count($this->replace) > 0) {
            $this->text = preg_replace($this->replace, '', $this->text);
        }

        $data = $this->getData();

        if ($this->group != '') {
            $data = $this->getDataByGroup($data);
        }

        return $data;
    }

    /**
     * 获取数据
     * @return mixed 调用指定方法返回数据
     */
    protected function getData() {
        $data = explode($this->line, $this->text);

        return $this->formatData($data, $this->typeAll);
    }

    /**
     * 获取分组数据
     * @param $data 数据
     * @return array 结果
     */
    protected function getDataByGroup($data) {
        $list = $this->group($data);

        foreach ($list as $key => $item) {
            $list[$key] = $this->formatData($item, $this->typeGroup);
        }

        return $list;
    }

    protected function group($data) {
        $list = array();
        $j = $i = 0;
        foreach ($data as $key => $item) {
            $tmp = $this->getLine($item);
            if ($tmp != null) {
                if (stristr($tmp, $this->group) !== false) {
                    $i++;
                    $j = 0;
                    continue;
                }

                if (is_numeric($key)) {
                    $list[$i][$j] = $tmp;
                    $j++;
                } else {
                    $list[$i][$key] = $tmp;
                }
            }
        }

        return $list;
    }

    /**
     * 获取第行数据
     * @param $data 数据
     * @return null|string 如果符合条件返回null
     */
    protected function getLine($data) {
        $condition = false;
        if (count($this->removeCondition) > 0) {
            foreach ($this->removeCondition as $key => $item) {
                if ($item == '%') {
                    if (stristr($data, $key) !== false) {
                        $condition = true;
                        break;
                    }
                } else {
                    if ($key == trim($data)) {
                        $condition = true;
                        break;
                    }
                }
            }
        }

        if ($condition) {
            $this->removeData[] = $data;
            return null;
        }

        return trim($data);
    }
}

以下为实例方法

<?php

include("log.php");

class videoLog extends log {

    public $files = array();

    public function formatData($data, $type) {
        if ($type == $this->typeAll) {
            return $data;
        }

        $list = $data;
        foreach ($data as $key => $item) {
            $tmp = $this->getExtendedData($item, $key);
            if ($tmp != null) {
                $list = array_merge($list, $tmp);
            }
        }

        return $list;
    }

    protected function getWidthxHeight($w, $h) {
        $ret = array();
        if ($w / $h >= 1.5) {
            if ($w >= 1920 && $h >= 1080) {
                $ret['height'] = 1080;
                $ret['width'] = 1920;
                $ret['videoRate'] = 25;
                $ret['videoBitRate'] = 1024;
                $ret['key'] = "ed";
            } else if ($w >= 1280 && $h >= 720) {
                $ret['height'] = 720;
                $ret['width'] = 1280;
                $ret['videoRate'] = 25;
                $ret['videoBitRate'] = 512;
                $ret['key'] = "hd";
            } else {
                $ret['height'] = $h;
                $ret['width'] = $w;
                if (($w >= 854 && $h >= 480)) {
                    $ret['height'] = 480;
                    $ret['width'] = 854;
                }
                $ret['videoRate'] = 15;
                $ret['videoBitRate'] = 256;
                $ret['key'] = "sd";
            }
            $ret['bili'] = '16:9';
        } else {

            if ($w >= 1440 && $h >= 1080) {
                $ret['height'] = 1440;
                $ret['width'] = 1920;
                $ret['videoRate'] = 25;
                $ret['videoBitRate'] = 1024;
                $ret['key'] = "ed";
            } else if ($w >= 960 && $h >= 720) {
                $ret['height'] = 720;
                $ret['width'] = 960;
                $ret['videoRate'] = 25;
                $ret['videoBitRate'] = 512;
                $ret['key'] = "hd";
            } else {
                $ret['height'] = $h;
                $ret['width'] = $w;
                if (($w >= 640 && $h >= 480)) {
                    $ret['height'] = 480;
                    $ret['width'] = 640;
                }
                $ret['videoRate'] = 15;
                $ret['videoBitRate'] = 256;
                $ret['key'] = "sd";
            }
            $ret['bili'] = '4:3';
        }
        return $ret;
    }


    protected function formatSize($size, $dec = 2) {
        $format = array("B", "KB", "MB", "GB", "TB", "PB");
        $pos = 0;
        while ($size >= 1024) {
            $size /= 1024;
            $pos++;
        }
        return round($size, $dec) . $format[$pos];
    }

    protected function getVideoInfo($text) {
        $tmp = explode("video_rate", $text);
        $duration = explode("duration: ", $text);
        $tmp = explode(":", $tmp[0]);

        $list = array();
        if (count($tmp) > 1) {
            $h = explode(" ", $tmp[1]);
            $list = $this->getWidthxHeight(trim($tmp[2]), $h[1]);
            $list['VideoW'] = trim($tmp[2]);
            $list['VideoH'] = trim($h[1]);
        }
        if (isset($duration[1])) {
            $list['duration'] = $duration[1];
        }
        return $list;
    }

    protected function getExtendedData($text, $num) {
        if ($num == 0) {
            $tmp = json_decode($text, true);
            if (is_array($tmp)) {
                if (isset($tmp['sourceFile'])) {
                    $tmp['fileSize'] = $this->getFileSize($tmp['sourceFile']);
                    $this->files[$tmp['sourceFile']] = $tmp['fileSize'];
                    if ($tmp['fileSize'] == '0B') {
                        $this->files[$tmp['sourceFile']] = 'No files found';
                    }

                }
                return $tmp;
            }
        }

        if ($num == 3) {
            return $this->getVideoInfo($text);
        }

        return null;
    }

    protected function getFileSize($file) {
        $size = 0;

        if (is_file($file)) {
            $size = filesize($file);
        }
        return $this->formatSize($size);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值