Klaus Demo nginx / fcdb465
Access log: the "flush" parameter of the "access_log" directive. Valentin Bartenev 9 years ago
1 changed file(s) with 105 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
4343 u_char *start;
4444 u_char *pos;
4545 u_char *last;
46
47 ngx_event_t *event;
48 ngx_msec_t flush;
4649 } ngx_http_log_buf_t;
4750
4851
8588 ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
8689
8790 static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
91 static void ngx_http_log_flush_handler(ngx_event_t *ev);
8892
8993 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
9094 ngx_http_log_op_t *op);
140144
141145 { ngx_string("access_log"),
142146 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
143 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
147 |NGX_HTTP_LMT_CONF|NGX_CONF_1MORE,
144148 ngx_http_log_set_log,
145149 NGX_HTTP_LOC_CONF_OFFSET,
146150 0,
282286
283287 p = buffer->pos;
284288
289 if (buffer->event && p == buffer->start) {
290 ngx_add_timer(buffer->event, buffer->flush);
291 }
292
285293 for (i = 0; i < log[l].format->ops->nelts; i++) {
286294 p = op[i].run(r, p, &op[i]);
287295 }
291299 buffer->pos = p;
292300
293301 continue;
302 }
303
304 if (buffer->event && buffer->event->timer_set) {
305 ngx_del_timer(buffer->event);
294306 }
295307 }
296308
502514 }
503515
504516 buffer->pos = buffer->start;
517
518 if (buffer->event && buffer->event->timer_set) {
519 ngx_del_timer(buffer->event);
520 }
521 }
522
523
524 static void
525 ngx_http_log_flush_handler(ngx_event_t *ev)
526 {
527 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0,
528 "http log buffer flush handler");
529
530 ngx_http_log_flush(ev->data, ev->log);
505531 }
506532
507533
890916
891917 ssize_t size;
892918 ngx_uint_t i, n;
893 ngx_str_t *value, name;
919 ngx_msec_t flush;
920 ngx_str_t *value, name, s;
894921 ngx_http_log_t *log;
895922 ngx_http_log_buf_t *buffer;
896923 ngx_http_log_fmt_t *fmt;
9771004 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
9781005 {
9791006 log->format = &fmt[i];
980 goto buffer;
981 }
982 }
983
984 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
985 "unknown log format \"%V\"", &name);
986 return NGX_CONF_ERROR;
987
988 buffer:
989
990 if (cf->args->nelts == 4) {
991 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
992 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
993 "invalid parameter \"%V\"", &value[3]);
994 return NGX_CONF_ERROR;
995 }
1007 break;
1008 }
1009 }
1010
1011 if (log->format == NULL) {
1012 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1013 "unknown log format \"%V\"", &name);
1014 return NGX_CONF_ERROR;
1015 }
1016
1017 size = 0;
1018 flush = 0;
1019
1020 for (i = 3; i < cf->args->nelts; i++) {
1021
1022 if (ngx_strncmp(value[i].data, "buffer=", 7) == 0) {
1023 s.len = value[i].len - 7;
1024 s.data = value[i].data + 7;
1025
1026 size = ngx_parse_size(&s);
1027
1028 if (size == NGX_ERROR || size == 0) {
1029 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1030 "invalid buffer size \"%V\"", &s);
1031 return NGX_CONF_ERROR;
1032 }
1033
1034 continue;
1035 }
1036
1037 if (ngx_strncmp(value[i].data, "flush=", 6) == 0) {
1038 s.len = value[i].len - 6;
1039 s.data = value[i].data + 6;
1040
1041 flush = ngx_parse_time(&s, 0);
1042
1043 if (flush == (ngx_msec_t) NGX_ERROR || flush == 0) {
1044 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1045 "invalid flush time \"%V\"", &s);
1046 return NGX_CONF_ERROR;
1047 }
1048
1049 continue;
1050 }
1051
1052 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1053 "invalid parameter \"%V\"", &value[i]);
1054 return NGX_CONF_ERROR;
1055 }
1056
1057 if (flush && size == 0) {
1058 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1059 "no buffer is defined for access_log \"%V\"",
1060 &value[1]);
1061 return NGX_CONF_ERROR;
1062 }
1063
1064 if (size) {
9961065
9971066 if (log->script) {
9981067 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
10001069 return NGX_CONF_ERROR;
10011070 }
10021071
1003 name.len = value[3].len - 7;
1004 name.data = value[3].data + 7;
1005
1006 size = ngx_parse_size(&name);
1007
1008 if (size == NGX_ERROR) {
1009 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1010 "invalid buffer value \"%V\"", &name);
1011 return NGX_CONF_ERROR;
1012 }
1013
10141072 if (log->file->data) {
10151073 buffer = log->file->data;
10161074
1017 if (buffer->last - buffer->start != size) {
1075 if (buffer->last - buffer->start != size
1076 || buffer->flush != flush)
1077 {
10181078 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
10191079 "access_log \"%V\" already defined "
1020 "with different buffer size", &value[1]);
1080 "with conflicting parameters",
1081 &value[1]);
10211082 return NGX_CONF_ERROR;
10221083 }
10231084
10361097
10371098 buffer->pos = buffer->start;
10381099 buffer->last = buffer->start + size;
1100
1101 if (flush) {
1102 buffer->event = ngx_pcalloc(cf->pool, sizeof(ngx_event_t));
1103 if (buffer->event == NULL) {
1104 return NGX_CONF_ERROR;
1105 }
1106
1107 buffer->event->data = log->file;
1108 buffer->event->handler = ngx_http_log_flush_handler;
1109 buffer->event->log = &cf->cycle->new_log;
1110
1111 buffer->flush = flush;
1112 }
10391113
10401114 log->file->flush = ngx_http_log_flush;
10411115 log->file->data = buffer;