Authored by xiaowei

记录log

... ... @@ -18,6 +18,7 @@ use Plugin\Cache;
use Plugin\Helpers;
use Hood\Session;
use LibModels\Wap\Passport\LoginData;
use Plugin\UdpLog;
class AbstractAction extends Controller_Abstract
{
... ... @@ -328,6 +329,7 @@ class AbstractAction extends Controller_Abstract
if (isset($userInfo['data']) && $userInfo['data']) {
$uidCookie = $userInfo['data']['profile_name'] . '::' . $userInfo['data']['uid'] . '::' . $userInfo['data']['vip_info']['title'] . '::' . $token;
$this->setCookie('_UID', $uidCookie);
UdpLog::info('【登录】同步登陆cookie生成',$uidCookie);
}
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $uid);
... ...
... ... @@ -254,7 +254,7 @@ class Yohobuy
}
$result = curl_exec($ch);
//log打印
UdpLog::info('get调用接口入参url/出参:', 'in:'.$url.'?'.$str.' out:'.$result);
UdpLog::info('post调用接口入参url/出参:', 'in:'.$url.'?'.$str.' out:'.$result);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
... ...
... ... @@ -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
*/
... ... @@ -96,5 +158,4 @@ class UdpLog
public static function debug($message,$meta='') {
self::procLog(__METHOD__,$message,debug_backtrace(),$meta);
}
}
}
\ No newline at end of file
... ...