log分析基类
2015-12-01 10:57
603 查看
以下是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);
}
}
相关文章推荐
- 一个关于if else容易迷惑的问题
- Android Native 绘图方法
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- ruby 单态方法 分析
- C#、ASP.NET通用扩展工具类之TypeParse
- Lua编程示例(二):面向对象、metatable对表进行扩展
- Windows Powershell扩展类型系统
- 典型入侵日志分析
- C#中struct和class的区别详解
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP