...
|
...
|
@@ -23,6 +23,13 @@ class UdpLog |
|
|
public static $port = '4444';
|
|
|
//influxdb measurement
|
|
|
public static $measurement = 'php_log';
|
|
|
|
|
|
public static $filePath = '/Data/logs/';
|
|
|
|
|
|
const RECORD_MODE_FILE = 'FILE';
|
|
|
const RECORD_MODE_UDP = 'UDP';
|
|
|
|
|
|
const RECORD_MODE = 'FILE';//mode: FILE | UDP
|
|
|
|
|
|
/**
|
|
|
* proc line and send log to influxdb
|
...
|
...
|
@@ -30,27 +37,82 @@ class UdpLog |
|
|
* @param $message
|
|
|
* @param $meta
|
|
|
*/
|
|
|
private static function procLog($level,$message,$debugInfo,$meta='') {
|
|
|
private static function procLog($level, $message, $debugInfo, $meta = '') {
|
|
|
date_default_timezone_set('PRC');
|
|
|
$level = str_replace(__CLASS__.'::','',$level);
|
|
|
$file = $debugInfo[0]["file"];
|
|
|
$line = $debugInfo[0]["line"];
|
|
|
|
|
|
$string = '';
|
|
|
//make tags
|
|
|
$tags = array(
|
|
|
'host='.gethostname(),
|
|
|
'level='.$level,
|
|
|
'file='.$file,
|
|
|
'line='.$line
|
|
|
'time' => date('Y-m-d H:i:s',time()),
|
|
|
'level' => $level,
|
|
|
'host'=> gethostname(),
|
|
|
'file'=> $file,
|
|
|
'line'=> $line,
|
|
|
'message' => $message,
|
|
|
'meta' => serialize($meta)
|
|
|
);
|
|
|
|
|
|
//make a line
|
|
|
$tags = implode(',',$tags);
|
|
|
|
|
|
$string = self::$measurement .','.$tags.' message="'.$message.'",meta="'.var_export($meta,true).'"';
|
|
|
self::send($string);
|
|
|
$string = implode('|', $tags);//format: time|level|host|file|line|message|meta
|
|
|
if(self::RECORD_MODE == self::RECORD_MODE_UDP) {
|
|
|
self::send($string);
|
|
|
}
|
|
|
else if(self::RECORD_MODE == self::RECORD_MODE_FILE) {
|
|
|
self::fileLog($level, $string);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 文件日志记录
|
|
|
*
|
|
|
* @param string $level
|
|
|
* @param string $message
|
|
|
*/
|
|
|
private static function fileLog($level, $message)
|
|
|
{
|
|
|
$filename = $level . '.log'; //日志文件
|
|
|
$logFile = self::createPath(self::$filePath, $filename);
|
|
|
if (!file_exists(self::$filePath))//判断文件路径是否存在
|
|
|
{
|
|
|
if (!empty(self::$filePath))//判断路径是否为空
|
|
|
{
|
|
|
if (!(self::createDir(self::$filePath))) {
|
|
|
return false;
|
|
|
}
|
|
|
if (!is_writable($logFile)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@error_log($message."\r\n", 3, $logFile);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 作用:创建目录
|
|
|
* 输入:要创建的目录
|
|
|
* 输出:true | false
|
|
|
*/
|
|
|
private static function createDir($dir)
|
|
|
{
|
|
|
return is_dir($dir) or (self::createDir(dirname($dir)) and mkdir($dir, 0777));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 作用:构建路径
|
|
|
* 输入:文件的路径,要写入的文件名
|
|
|
* 输出:构建好的路径字串
|
|
|
*/
|
|
|
private static function createPath($dir, $filename)
|
|
|
{
|
|
|
if (empty($dir)) {
|
|
|
return $filename;
|
|
|
} else {
|
|
|
return $dir . "/" . $filename;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* send by udp
|
|
|
* @param $string
|
|
|
*/
|
...
|
...
|
|