Skip to content

Commit

Permalink
refactor log, now support same directive parameters as error_log
Browse files Browse the repository at this point in the history
Signed-off-by: Liu Lantao <liulantao@gmail.com>
  • Loading branch information
Lax committed May 11, 2018
1 parent a622bc4 commit 47fc475
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 63 deletions.
1 change: 0 additions & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
"
Expand Down
47 changes: 26 additions & 21 deletions src/ngx_http_accounting_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/ngx_http_accounting_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions src/ngx_http_accounting_worker_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/ngxta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
32 changes: 0 additions & 32 deletions src/ngxta_log.c

This file was deleted.

0 comments on commit 47fc475

Please sign in to comment.