mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
modified 优化Log格式
This commit is contained in:
parent
5a73c00e9b
commit
3c98e6a3f3
@ -7,87 +7,91 @@
|
|||||||
namespace Home\ORG;
|
namespace Home\ORG;
|
||||||
|
|
||||||
|
|
||||||
class ApiLog{
|
class ApiLog {
|
||||||
|
|
||||||
private static $appInfo = 'null';
|
private static $appInfo = 'null';
|
||||||
private static $apiInfo = 'null';
|
private static $apiInfo = 'null';
|
||||||
private static $request = 'null';
|
private static $request = 'null';
|
||||||
private static $requestAfterFilter = 'null';
|
private static $requestAfterFilter = 'null';
|
||||||
private static $response = 'null';
|
private static $response = 'null';
|
||||||
private static $header = 'null';
|
private static $header = 'null';
|
||||||
private static $userInfo = 'null';
|
private static $userInfo = 'null';
|
||||||
private static $separator = '###';
|
private static $separator = '###';
|
||||||
|
|
||||||
public static function setAppInfo($data){
|
public static function setAppInfo($data) {
|
||||||
self::$appInfo = $data['app_id'] . "({$data['app_name']}) {$data['device_id']}";
|
self::$appInfo = $data['app_id'] . self::$separator . $data['app_name'] . self::$separator . $data['device_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setHeader($data){
|
public static function setHeader($data) {
|
||||||
$userToken = (isset($data['USER-TOKEN']) && !empty($data['USER-TOKEN'])) ? $data['USER-TOKEN'] : 'null';
|
$userToken = (isset($data['USER-TOKEN']) && !empty($data['USER-TOKEN'])) ? $data['USER-TOKEN'] : 'null';
|
||||||
$accessToken = (isset($data['ACCESS-TOKEN']) && !empty($data['ACCESS-TOKEN'])) ? $data['ACCESS-TOKEN'] : 'null';
|
$accessToken = (isset($data['ACCESS-TOKEN']) && !empty($data['ACCESS-TOKEN'])) ? $data['ACCESS-TOKEN'] : 'null';
|
||||||
self::$header = $accessToken . self::$separator . $userToken . self::$separator . $data['VERSION'] . self::$separator . $cas;
|
$cas = (isset($data['CAS']) && !empty($data['CAS'])) ? $data['CAS'] : 'null';
|
||||||
}
|
self::$header = $accessToken . self::$separator . $userToken . self::$separator . $data['VERSION'] . self::$separator . $cas;
|
||||||
|
}
|
||||||
|
|
||||||
public static function setApiInfo($data){
|
public static function setApiInfo($data) {
|
||||||
self::$apiInfo = $data['apiName'] . self::$separator . $data['hash'];
|
self::$apiInfo = $data['apiName'] . self::$separator . $data['hash'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setUserInfo($data){
|
public static function setUserInfo($data) {
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$data = json_encode($data);
|
$data = json_encode($data);
|
||||||
}
|
}
|
||||||
self::$userInfo = $data;
|
self::$userInfo = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setRequest($data){
|
public static function setRequest($data) {
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$data = json_encode($data);
|
$data = json_encode($data);
|
||||||
}
|
}
|
||||||
self::$request = $data;
|
self::$request = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setRequestAfterFilter($data){
|
public static function setRequestAfterFilter($data) {
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$data = json_encode($data);
|
$data = json_encode($data);
|
||||||
}
|
}
|
||||||
self::$requestAfterFilter = $data;
|
self::$requestAfterFilter = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setResponse($data){
|
public static function setResponse($data, $code) {
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$data = json_encode($data);
|
$data = json_encode($data);
|
||||||
}
|
}
|
||||||
self::$response = $data;
|
self::$response = $code . self::$separator . $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function save(){
|
public static function save() {
|
||||||
$logPath = APP_PATH . '/ApiLog/' . date('YmdH') . '.log';
|
$logPath = APP_PATH . '/ApiLog/' . date('YmdH') . '.log';
|
||||||
$logStr = implode(self::$separator, array(
|
if (self::$appInfo == 'null') {
|
||||||
self::$apiInfo,
|
self::$appInfo = 'null' . self::$separator . 'null' . self::$separator . 'null';
|
||||||
date('H:i:s'),
|
}
|
||||||
self::$request,
|
$logStr = implode(self::$separator, array(
|
||||||
self::$header,
|
self::$apiInfo,
|
||||||
self::$response,
|
date('Y-m-d H:i:s'),
|
||||||
self::$requestAfterFilter,
|
self::$request,
|
||||||
self::$appInfo,
|
self::$header,
|
||||||
self::$userInfo
|
self::$response,
|
||||||
));
|
self::$requestAfterFilter,
|
||||||
|
self::$appInfo,
|
||||||
|
self::$userInfo
|
||||||
|
));
|
||||||
|
|
||||||
@file_put_contents($logPath, $logStr . "\n", FILE_APPEND);
|
@file_put_contents($logPath, $logStr . "\n", FILE_APPEND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $log 被记录的内容
|
* @param string $log 被记录的内容
|
||||||
* @param string $type 日志文件名称
|
* @param string $type 日志文件名称
|
||||||
* @param string $filePath
|
* @param string $filePath
|
||||||
*/
|
*/
|
||||||
public static function writeLog($log, $type = 'sql', $filePath = './Application/Runtime/'){
|
public static function writeLog($log, $type = 'sql', $filePath = './Application/Runtime/') {
|
||||||
$filename = $filePath . date("Ymd") . '_' . $type . ".log";
|
$filename = $filePath . date("Ymd") . '_' . $type . ".log";
|
||||||
@$handle = fopen($filename, "a+");
|
@$handle = fopen($filename, "a+");
|
||||||
@fwrite($handle, date('Y-m-d H:i:s') . "\t" . $log . "\r\n");
|
@fwrite($handle, date('Y-m-d H:i:s') . "\t" . $log . "\r\n");
|
||||||
@fclose($handle);
|
@fclose($handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user