10000 新增日志请求批次号request_id by chenhakim · Pull Request #134 · SeasX/SeasLog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

新增日志请求批次号request_id #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CodeTips/SeasLogCodeTips.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ static public function getBasePath()
return 'the base_path';
}

/**
* 设置本次请求标识
* @param string
* @return bool
*/
static public function setRequestID($request_id){
return TRUE;
}
/**
* 获取本次请求标识
* @return string
*/
static public function getRequestID(){
return uniqid();
}

/**
* 设置模块目录
* @param $module
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ class SeasLog
{
return 'the base_path';
}

/**
* Set The requestID
* @param string
* @return bool
*/
static public function setRequestID($request_id){
return TRUE;
}
/**
* Get The requestID
* @return string
*/
static public function getRequestID(){
return uniqid();
}

/**
* Set The logger
Expand Down
16 changes: 16 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ class SeasLog
{
return 'the base_path';
}

/**
* 设置本次请求标识
* @param string
* @return bool
*/
static public function setRequestID($request_id){
return TRUE;
}
/**
* 获取本次请求标识
* @return string
*/
static public function getRequestID(){
return uniqid();
}

/**
* 设置模块目录
Expand Down
4 changes: 4 additions & 0 deletions php_seaslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ PHP_METHOD(SEASLOG_RES_NAME, __construct);
PHP_METHOD(SEASLOG_RES_NAME, __destruct);
PHP_METHOD(SEASLOG_RES_NAME, setBasePath);
PHP_METHOD(SEASLOG_RES_NAME, getBasePath);
PHP_METHOD(SEASLOG_RES_NAME, setRequestID);
PHP_METHOD(SEASLOG_RES_NAME, getRequestID);
PHP_METHOD(SEASLOG_RES_NAME, setLogger);
PHP_METHOD(SEASLOG_RES_NAME, getLastLogger);
PHP_METHOD(SEASLOG_RES_NAME, setDatetimeFormat);
Expand Down Expand Up @@ -164,6 +166,7 @@ ZEND_BEGIN_MODULE_GLOBALS(seaslog)
int current_datetime_format_len;
char *base_path;
char *host_name;
char *request_id;

logger_entry_t *tmp_logger;
logger_entry_t *last_logger;
Expand All @@ -185,6 +188,7 @@ ZEND_BEGIN_MODULE_GLOBALS(seaslog)
char *remote_host;
int remote_port;


#if PHP_VERSION_ID >= 70000
zval buffer;
zval logger_list;
Expand Down
68 changes: 66 additions & 2 deletions seaslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ZEND_GET_MODULE(seaslog)
static void seaslog_init_logger(TSRMLS_D);
static void seaslog_init_default_last_logger(TSRMLS_D);
static void seaslog_init_buffer(TSRMLS_D);
static void seaslog_init_request(TSRMLS_D);
static void seaslog_clear_buffer(TSRMLS_D);
static void seaslog_shutdown_buffer(int re_init TSRMLS_DC);
static void seaslog_clear_logger(TSRMLS_D);
Expand Down Expand Up @@ -133,6 +134,9 @@ static zend_bool use_buffer = 0;
static int buffer_size = 0;
static int level = 0;

// 自定义添加
static char *request_id = "00001001";

const zend_function_entry seaslog_functions[] =
{
PHP_FE(seaslog_get_version, NULL)
Expand All @@ -155,6 +159,10 @@ ZEND_BEGIN_ARG_INFO_EX(seaslog_setBasePath_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, base_path)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(seaslog_setRequestID_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, request_id)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(seaslog_setLogger_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, logger)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -200,6 +208,10 @@ const zend_function_entry seaslog_methods[] =
PHP_ME(SEASLOG_RES_NAME, getBasePath, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SEASLOG_RES_NAME, setLogger, seaslog_setLogger_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SEASLOG_RES_NAME, getLastLogger, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
// ---------自定义---------
PHP_ME(SEASLOG_RES_NAME, setRequestID, seaslog_setRequestID_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SEASLOG_RES_NAME, getRequestID, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
// ---------以上!---------
PHP_ME(SEASLOG_RES_NAME, setDatetimeFormat, seaslog_setDatetimeFormat_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SEASLOG_RES_NAME, getDatetimeFormat, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(SEASLOG_RES_NAME, analyzerCount, seaslog_analyzerCount_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
Expand Down Expand Up @@ -311,8 +323,11 @@ PHP_RINIT_FUNCTION(seaslog)
seaslog_init_host_name(TSRMLS_C);
seaslog_init_logger_list(TSRMLS_C);
seaslog_init_logger(TSRMLS_C);
// 自定义添加批次号
seaslog_init_request(TSRMLS_C);
seaslog_init_buffer(TSRMLS_C);


return SUCCESS;
}

Expand Down Expand Up @@ -647,6 +662,24 @@ static void seaslog_init_buffer(TSRMLS_D)
}
}

// 自定义批次号
static char *get_request_id(){
char *uniqid;
struct timeval now;

timerclear(&now);
gettimeofday(&now, NULL);

spprintf(&uniqid, 0, "%ld%ld", (long)time(NULL), (long)now.tv_usec / 1000);
return uniqid;
}

// 自定义批次号
static void seaslog_init_request(TSRMLS_D)
{
SEASLOG_G(request_id) = get_request_id();
}

static void seaslog_clear_buffer(TSRMLS_D)
{
if (SEASLOG_G(use_buffer))
Expand Down Expand Up @@ -1505,6 +1538,37 @@ PHP_METHOD(SEASLOG_RES_NAME, getDatetimeFormat)
SEASLOG_RETURN_STRINGL(str, len);
}

// 自定义批次号
PHP_METHOD(SEASLOG_RES_NAME, setRequestID)
{
zval *_request_id;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc TSRMLS_CC, "z", &_request_id) == FAILURE)
return;

if (argc > 0 && (Z_TYPE_P(_request_id) == IS_STRING || Z_STRLEN_P(_request_id) > 0))
{
if (SEASLOG_G(request_id))
{
efree(SEASLOG_G(request_id));

SEASLOG_G(request_id) = estrdup(Z_STRVAL_P(_request_id));
}

RETURN_TRUE;
}

RETURN_FALSE;
}

// 自定义添加批次号
PHP_METHOD(SEASLOG_RES_NAME, getRequestID)
{
SEASLOG_RETURN_STRINGL(SEASLOG_G(request_id), strlen(SEASLOG_G(request_id)));
}


PHP_METHOD(SEASLOG_RES_NAME, analyzerCount)
{
int argc = ZEND_NUM_ARGS();
Expand Down Expand Up @@ -2050,7 +2114,7 @@ static int appender_handle_file(char *message, int message_len, char *level, log
log_file_path_len = spprintf(&log_file_path, 0, "%s/%s.log", logger->logger_path,real_date);
}

log_len = spprintf(&log_info, 0, "%s | %d | %s | %s | %s \n", level, getpid(), current_time, real_time, message);
log_len = spprintf(&log_info, 0, "%s | %d | %s | %s | %s | %s \n", level, getpid(), SEASLOG_G(request_id), current_time, real_time, message);

if (_php_log_ex(log_info, log_len, log_file_path, log_file_path_len + 1, ce TSRMLS_CC) == FAILURE)
{
Expand All @@ -2076,7 +2140,7 @@ static int appender_handle_tcp_udp(char *message, int message_len, char *level,
current_time = mic_time();
real_time = mk_real_time(TSRMLS_C);

log_len = spprintf(&log_info, 0, "%s | %s | %s | %d | %s | %s | %s \n", SEASLOG_G(host_name), logger->logger, level, getpid(), current_time, real_time, message);
log_len = spprintf(&log_info, 0, "%s | %s | %s | %d | %s | %s | %s | %s \n", SEASLOG_G(host_name), logger->logger, level, getpid(), SEASLOG_G(request_id), current_time, real_time, message);

if (_php_log_ex(log_info, log_len, logger->logger, logger->logger_len, ce TSRMLS_CC) == FAILURE)
{
Expand Down
0