From 47fc4755db5a7f59f773afa5975a5412baf8e300 Mon Sep 17 00:00:00 2001 From: Liu Lantao Date: Fri, 11 May 2018 22:59:39 +0800 Subject: [PATCH] refactor log, now support same directive parameters as `error_log` Signed-off-by: Liu Lantao --- config | 1 - src/ngx_http_accounting_module.c | 47 +++++++++++++----------- src/ngx_http_accounting_module.h | 2 +- src/ngx_http_accounting_worker_process.c | 16 +++++--- src/ngxta.h | 3 +- src/ngxta_log.c | 32 ---------------- 6 files changed, 38 insertions(+), 63 deletions(-) delete mode 100644 src/ngxta_log.c diff --git a/config b/config index b81479c..8155e08 100644 --- a/config +++ b/config @@ -9,7 +9,6 @@ HTTP_ACCOUNTING_DEPS=" \ HTTP_ACCOUNTING_SRCS=" \ $ngx_addon_dir/src/ngxta_http_statuses.c \ $ngx_addon_dir/src/ngxta_period_metrics.c \ - $ngx_addon_dir/src/ngxta_log.c \ $ngx_addon_dir/src/ngx_http_accounting_module.c \ $ngx_addon_dir/src/ngx_http_accounting_worker_process.c \ " diff --git a/src/ngx_http_accounting_module.c b/src/ngx_http_accounting_module.c index 13cd5e7..95777f5 100644 --- a/src/ngx_http_accounting_module.c +++ b/src/ngx_http_accounting_module.c @@ -23,6 +23,7 @@ static void *ngx_http_accounting_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_accounting_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); static char *ngx_http_accounting_set_accounting_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char * ngx_http_accounting_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_command_t ngx_http_accounting_commands[] = { { ngx_string("http_accounting"), @@ -55,10 +56,10 @@ static ngx_command_t ngx_http_accounting_commands[] = { NULL}, { ngx_string("http_accounting_log"), - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, - ngx_conf_set_str_slot, + NGX_HTTP_MAIN_CONF|NGX_CONF_1MORE, + ngx_http_accounting_set_log, NGX_HTTP_MAIN_CONF_OFFSET, - offsetof(ngx_http_accounting_main_conf_t, log), + 0, NULL}, ngx_null_command @@ -105,24 +106,6 @@ ngx_http_accounting_init(ngx_conf_t *cf) return NGX_OK; } - if (amcf->log.len > 0) { - if (NGX_OK != ngxta_log_open(cf->cycle, &ngxta_log, &amcf->log)) { - // ngxta_log = NULL; - return NGX_ERROR; - } - - if (ngxta_log.file->name.len > 0 - && ngxta_log.file->fd != NGX_INVALID_FILE ) { - ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, - "pid:%i|accounting log to file: %V", - ngx_getpid(), - ngxta_log.file->name ); - } - } - // else { - // // ngxta_log = ngx_cycle->log; - // } - cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers); @@ -240,3 +223,25 @@ ngx_http_accounting_set_accounting_id(ngx_conf_t *cf, ngx_command_t *cmd, void * return NGX_CONF_OK; } + +static char * +ngx_http_accounting_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_accounting_main_conf_t *amcf = conf; + char *rc; + ngx_log_t *log; + + rc = ngx_log_set_log(cf, &amcf->log); + if (rc != NGX_CONF_OK) { return rc; } + + log = amcf->log; + while (log) { + if (log->log_level < NGXTA_LOG_LEVEL) { + log->log_level = NGXTA_LOG_LEVEL; + } + + log = log->next; + } + + return NGX_CONF_OK; +} diff --git a/src/ngx_http_accounting_module.h b/src/ngx_http_accounting_module.h index 9ad3c12..60137ee 100644 --- a/src/ngx_http_accounting_module.h +++ b/src/ngx_http_accounting_module.h @@ -19,7 +19,7 @@ typedef struct { typedef struct { ngx_flag_t enable; - ngx_str_t log; + ngx_log_t *log; time_t interval; ngx_flag_t perturb; } ngx_http_accounting_main_conf_t; diff --git a/src/ngx_http_accounting_worker_process.c b/src/ngx_http_accounting_worker_process.c index 286a16f..3e2d457 100644 --- a/src/ngx_http_accounting_worker_process.c +++ b/src/ngx_http_accounting_worker_process.c @@ -33,8 +33,8 @@ ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle) ngxta_period_init(cycle->pool); - if (ngxta_log.file->fd != NGX_INVALID_FILE) { - ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "pid:%i|worker process start accounting", ngx_getpid()); + if (amcf->log != NULL) { + ngx_log_error(NGX_LOG_NOTICE, amcf->log, 0, "pid:%i|worker process start accounting", ngx_getpid()); } else { openlog((char *)ngx_http_accounting_title, LOG_NDELAY, LOG_SYSLOG); syslog(LOG_INFO, "pid:%i|worker process start accounting", ngx_getpid()); @@ -71,8 +71,8 @@ void ngx_http_accounting_worker_process_exit(ngx_cycle_t *cycle) worker_process_alarm_handler(NULL); - if (ngxta_log.file->fd != NGX_INVALID_FILE) { - ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "pid:%i|worker process stop accounting", ngx_getpid()); + if (amcf->log != NULL) { + ngx_log_error(NGX_LOG_NOTICE, amcf->log, 0, "pid:%i|worker process stop accounting", ngx_getpid()); } else { syslog(LOG_INFO, "pid:%i|worker process stop accounting", ngx_getpid()); } @@ -158,6 +158,8 @@ worker_process_export_metrics(void *val, void *para1, void *para2) ngx_time_t *created_at = para1; ngx_time_t *updated_at = para2; + ngx_http_accounting_main_conf_t *amcf; + ngx_str_t accounting_msg; u_char msg_buf[NGX_MAX_ERROR_STR]; u_char *p, *last; @@ -206,8 +208,10 @@ worker_process_export_metrics(void *val, void *para1, void *para2) accounting_msg.len = p - msg_buf; accounting_msg.data = msg_buf; - if (ngxta_log.file->fd != NGX_INVALID_FILE) { - ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "%V", &accounting_msg); + amcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_accounting_module); + + if (amcf->log != NULL) { + ngx_log_error(NGX_LOG_NOTICE, amcf->log, 0, "%V", &accounting_msg); } else { syslog(LOG_INFO, "%s", msg_buf); } diff --git a/src/ngxta.h b/src/ngxta.h index 5bfe405..19d0072 100644 --- a/src/ngxta.h +++ b/src/ngxta.h @@ -71,8 +71,7 @@ ngx_int_t ngxta_period_rotate(ngx_pool_t *pool); * Log */ -ngx_int_t ngxta_log_open(ngx_cycle_t *cycle, ngx_log_t *log, ngx_str_t *log_path); +#define NGXTA_LOG_LEVEL NGX_LOG_NOTICE -extern ngx_log_t ngxta_log; #endif /* _NGX_TRAFFIC_ACCOUNTING_H_INCLUDED_ */ diff --git a/src/ngxta_log.c b/src/ngxta_log.c deleted file mode 100644 index 8d52618..0000000 --- a/src/ngxta_log.c +++ /dev/null @@ -1,32 +0,0 @@ - -/* - * Copyright (C) Liu Lantao - */ - - -#include "ngxta.h" - - -ngx_log_t ngxta_log; - -ngx_int_t -ngxta_log_open(ngx_cycle_t *cycle, ngx_log_t *log, ngx_str_t *log_path) -{ - log->log_level = NGX_LOG_NOTICE; - - log->file = ngx_conf_open_file(cycle, log_path); - if (log->file == NULL) { - return NGX_ERROR; - } - log->file->fd = ngx_open_file(&log->file->name, NGX_FILE_APPEND, - NGX_FILE_CREATE_OR_OPEN, - NGX_FILE_DEFAULT_ACCESS); - - if (log->file->fd == NGX_INVALID_FILE) { - ngx_log_stderr(ngx_errno, - "[alert] could not open traffic accounting log file: " - ngx_open_file_n " \"%s\" failed", log->file->name.data); - } - - return NGX_OK; -}