3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
14 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
15 ngx_http_log_op_t *op);
17 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
21 struct ngx_http_log_op_s {
23 ngx_http_log_op_getlen_pt getlen;
24 ngx_http_log_op_run_pt run;
32 ngx_array_t *ops; /* array of ngx_http_log_op_t */
37 ngx_array_t formats; /* array of ngx_http_log_fmt_t */
38 ngx_uint_t combined_used; /* unsigned combined_used:1 */
39 } ngx_http_log_main_conf_t;
45 } ngx_http_log_script_t;
49 ngx_open_file_t *file;
50 ngx_http_log_script_t *script;
51 time_t disk_full_time;
52 time_t error_log_time;
53 ngx_http_log_fmt_t *format;
58 ngx_array_t *logs; /* array of ngx_http_log_t */
60 ngx_open_file_cache_t *open_file_cache;
61 time_t open_file_cache_valid;
62 ngx_uint_t open_file_cache_min_uses;
64 ngx_uint_t off; /* unsigned off:1 */
65 } ngx_http_log_loc_conf_t;
71 ngx_http_log_op_run_pt run;
75 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
76 u_char *buf, size_t len);
77 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
78 ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
80 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
81 ngx_http_log_op_t *op);
82 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
83 ngx_http_log_op_t *op);
84 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
85 ngx_http_log_op_t *op);
86 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
87 ngx_http_log_op_t *op);
88 static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
89 ngx_http_log_op_t *op);
90 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
91 ngx_http_log_op_t *op);
92 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
93 ngx_http_log_op_t *op);
94 static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
95 u_char *buf, ngx_http_log_op_t *op);
96 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
97 ngx_http_log_op_t *op);
99 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
100 ngx_http_log_op_t *op, ngx_str_t *value);
101 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
103 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
104 ngx_http_log_op_t *op);
105 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
108 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
109 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
110 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
112 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
114 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
116 static char *ngx_http_log_compile_format(ngx_conf_t *cf,
117 ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
118 static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
120 static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
123 static ngx_command_t ngx_http_log_commands[] = {
125 { ngx_string("log_format"),
126 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
127 ngx_http_log_set_format,
128 NGX_HTTP_MAIN_CONF_OFFSET,
132 { ngx_string("access_log"),
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
134 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
135 ngx_http_log_set_log,
136 NGX_HTTP_LOC_CONF_OFFSET,
140 { ngx_string("open_log_file_cache"),
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
142 ngx_http_log_open_file_cache,
143 NGX_HTTP_LOC_CONF_OFFSET,
151 static ngx_http_module_t ngx_http_log_module_ctx = {
152 NULL, /* preconfiguration */
153 ngx_http_log_init, /* postconfiguration */
155 ngx_http_log_create_main_conf, /* create main configuration */
156 NULL, /* init main configuration */
158 NULL, /* create server configuration */
159 NULL, /* merge server configuration */
161 ngx_http_log_create_loc_conf, /* create location configration */
162 ngx_http_log_merge_loc_conf /* merge location configration */
166 ngx_module_t ngx_http_log_module = {
168 &ngx_http_log_module_ctx, /* module context */
169 ngx_http_log_commands, /* module directives */
170 NGX_HTTP_MODULE, /* module type */
171 NULL, /* init master */
172 NULL, /* init module */
173 NULL, /* init process */
174 NULL, /* init thread */
175 NULL, /* exit thread */
176 NULL, /* exit process */
177 NULL, /* exit master */
178 NGX_MODULE_V1_PADDING
182 static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
185 static ngx_str_t ngx_http_combined_fmt =
186 ngx_string("$remote_addr - $remote_user [$time_local] "
187 "\"$request\" $status $body_bytes_sent "
188 "\"$http_referer\" \"$http_user_agent\"");
191 static ngx_http_log_var_t ngx_http_log_vars[] = {
192 { ngx_string("connection"), NGX_ATOMIC_T_LEN, ngx_http_log_connection },
193 { ngx_string("pipe"), 1, ngx_http_log_pipe },
194 { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
196 { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
197 { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
198 ngx_http_log_request_time },
199 { ngx_string("status"), 3, ngx_http_log_status },
200 { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
201 { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
202 ngx_http_log_body_bytes_sent },
203 { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
204 ngx_http_log_body_bytes_sent },
205 { ngx_string("request_length"), NGX_SIZE_T_LEN,
206 ngx_http_log_request_length },
208 { ngx_null_string, 0, NULL }
213 ngx_http_log_handler(ngx_http_request_t *r)
219 ngx_open_file_t *file;
220 ngx_http_log_op_t *op;
221 ngx_http_log_loc_conf_t *lcf;
223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
226 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
232 log = lcf->logs->elts;
233 for (l = 0; l < lcf->logs->nelts; l++) {
235 if (ngx_time() == log[l].disk_full_time) {
238 * on FreeBSD writing to a full filesystem with enabled softupdates
239 * may block process for much longer time than writing to non-full
240 * filesystem, so we skip writing to a log for one second
246 ngx_http_script_flush_no_cacheable_variables(r, log[l].format->flushes);
249 op = log[l].format->ops->elts;
250 for (i = 0; i < log[l].format->ops->nelts; i++) {
251 if (op[i].len == 0) {
252 len += op[i].getlen(r, op[i].data);
259 len += NGX_LINEFEED_SIZE;
263 if (file && file->buffer) {
265 if (len > (size_t) (file->last - file->pos)) {
267 ngx_http_log_write(r, &log[l], file->buffer,
268 file->pos - file->buffer);
270 file->pos = file->buffer;
273 if (len <= (size_t) (file->last - file->pos)) {
277 for (i = 0; i < log[l].format->ops->nelts; i++) {
278 p = op[i].run(r, p, &op[i]);
289 line = ngx_pnalloc(r->pool, len);
296 for (i = 0; i < log[l].format->ops->nelts; i++) {
297 p = op[i].run(r, p, &op[i]);
302 ngx_http_log_write(r, &log[l], line, p - line);
310 ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
318 if (log->script == NULL) {
319 name = log->file->name.data;
320 n = ngx_write_fd(log->file->fd, buf, len);
324 n = ngx_http_log_script_write(r, log->script, &name, buf, len);
327 if (n == (ssize_t) len) {
336 if (err == NGX_ENOSPC) {
337 log->disk_full_time = now;
340 if (now - log->error_log_time > 59) {
341 ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
342 ngx_write_fd_n " to \"%s\" failed", name);
344 log->error_log_time = now;
350 if (now - log->error_log_time > 59) {
351 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
352 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
355 log->error_log_time = now;
361 ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
362 u_char **name, u_char *buf, size_t len)
367 ngx_open_file_info_t of;
368 ngx_http_log_loc_conf_t *llcf;
369 ngx_http_core_loc_conf_t *clcf;
371 if (!r->root_tested) {
373 /* test root directory existance */
375 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
376 /* simulate successfull logging */
380 path.data[root] = '\0';
382 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
384 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
386 of.valid = clcf->open_file_cache_valid;
387 of.min_uses = clcf->open_file_cache_min_uses;
388 of.errors = clcf->open_file_cache_errors;
389 of.events = clcf->open_file_cache_events;
391 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
395 /* simulate successfull logging */
399 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
400 "testing \"%s\" existence failed", path.data);
402 /* simulate successfull logging */
407 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ENOTDIR,
408 "testing \"%s\" existence failed", path.data);
410 /* simulate successfull logging */
415 if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
416 script->values->elts)
419 /* simulate successfull logging */
423 log.data[log.len - 1] = '\0';
426 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
427 "http log \"%s\"", log.data);
429 llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
431 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
434 of.valid = llcf->open_file_cache_valid;
435 of.min_uses = llcf->open_file_cache_min_uses;
436 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
438 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
441 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
442 ngx_open_file_n " \"%s\" failed", log.data);
443 /* simulate successfull logging */
447 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
448 "http log #%d", of.fd);
450 n = ngx_write_fd(of.fd, buf, len);
457 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
458 ngx_http_log_op_t *op)
467 *buf++ = (u_char) (data & 0xff);
476 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
477 ngx_http_log_op_t *op)
479 return ngx_cpymem(buf, (u_char *) op->data, op->len);
484 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
485 ngx_http_log_op_t *op)
487 return ngx_sprintf(buf, "%ui", r->connection->number);
492 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
505 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
507 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
508 ngx_cached_http_log_time.len);
513 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
517 tp = ngx_timeofday();
519 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
524 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
525 ngx_http_log_op_t *op)
530 tp = ngx_timeofday();
532 ms = (ngx_msec_int_t)
533 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
534 ms = (ms >= 0) ? ms : 0;
536 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
541 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
543 return ngx_sprintf(buf, "%ui",
544 r->err_status ? r->err_status : r->headers_out.status);
549 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
550 ngx_http_log_op_t *op)
552 return ngx_sprintf(buf, "%O", r->connection->sent);
557 * although there is a real $body_bytes_sent variable,
558 * this log operation code function is more optimized for logging
562 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
563 ngx_http_log_op_t *op)
567 length = r->connection->sent - r->header_size;
570 return ngx_sprintf(buf, "%O", length);
580 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
581 ngx_http_log_op_t *op)
583 return ngx_sprintf(buf, "%O", r->request_length);
588 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
593 index = ngx_http_get_variable_index(cf, value);
594 if (index == NGX_ERROR) {
599 op->getlen = ngx_http_log_variable_getlen;
600 op->run = ngx_http_log_variable;
608 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
611 ngx_http_variable_value_t *value;
613 value = ngx_http_get_indexed_variable(r, data);
615 if (value == NULL || value->not_found) {
619 len = ngx_http_log_escape(NULL, value->data, value->len);
621 value->escape = len ? 1 : 0;
623 return value->len + len * 3;
628 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
630 ngx_http_variable_value_t *value;
632 value = ngx_http_get_indexed_variable(r, op->data);
634 if (value == NULL || value->not_found) {
639 if (value->escape == 0) {
640 return ngx_cpymem(buf, value->data, value->len);
643 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
649 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
652 static u_char hex[] = "0123456789ABCDEF";
654 static uint32_t escape[] = {
655 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
657 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
658 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
660 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
661 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
663 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
664 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
666 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
667 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
668 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
669 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
675 /* find the number of the characters to be escaped */
679 for (i = 0; i < size; i++) {
680 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
686 return (uintptr_t) n;
689 for (i = 0; i < size; i++) {
690 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
693 *dst++ = hex[*src >> 4];
694 *dst++ = hex[*src & 0xf];
702 return (uintptr_t) dst;
707 ngx_http_log_create_main_conf(ngx_conf_t *cf)
709 ngx_http_log_main_conf_t *conf;
711 ngx_http_log_fmt_t *fmt;
713 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
715 return NGX_CONF_ERROR;
718 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
721 return NGX_CONF_ERROR;
724 fmt = ngx_array_push(&conf->formats);
726 return NGX_CONF_ERROR;
729 fmt->name.len = sizeof("combined") - 1;
730 fmt->name.data = (u_char *) "combined";
734 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
735 if (fmt->ops == NULL) {
736 return NGX_CONF_ERROR;
744 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
746 ngx_http_log_loc_conf_t *conf;
748 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
750 return NGX_CONF_ERROR;
753 conf->open_file_cache = NGX_CONF_UNSET_PTR;
760 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
762 ngx_http_log_loc_conf_t *prev = parent;
763 ngx_http_log_loc_conf_t *conf = child;
766 ngx_http_log_fmt_t *fmt;
767 ngx_http_log_main_conf_t *lmcf;
769 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
771 conf->open_file_cache = prev->open_file_cache;
772 conf->open_file_cache_valid = prev->open_file_cache_valid;
773 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
775 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
776 conf->open_file_cache = NULL;
780 if (conf->logs || conf->off) {
784 conf->logs = prev->logs;
785 conf->off = prev->off;
787 if (conf->logs || conf->off) {
791 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
792 if (conf->logs == NULL) {
793 return NGX_CONF_ERROR;
796 log = ngx_array_push(conf->logs);
798 return NGX_CONF_ERROR;
801 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
802 if (log->file == NULL) {
803 return NGX_CONF_ERROR;
807 log->disk_full_time = 0;
808 log->error_log_time = 0;
810 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
811 fmt = lmcf->formats.elts;
813 /* the default "combined" format */
814 log->format = &fmt[0];
815 lmcf->combined_used = 1;
822 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
824 ngx_http_log_loc_conf_t *llcf = conf;
828 ngx_str_t *value, name;
830 ngx_http_log_fmt_t *fmt;
831 ngx_http_log_main_conf_t *lmcf;
832 ngx_http_script_compile_t sc;
834 value = cf->args->elts;
836 if (ngx_strcmp(value[1].data, "off") == 0) {
841 if (llcf->logs == NULL) {
842 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
843 if (llcf->logs == NULL) {
844 return NGX_CONF_ERROR;
848 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
850 log = ngx_array_push(llcf->logs);
852 return NGX_CONF_ERROR;
855 ngx_memzero(log, sizeof(ngx_http_log_t));
857 n = ngx_http_script_variables_count(&value[1]);
860 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
861 if (log->file == NULL) {
862 return NGX_CONF_ERROR;
866 if (ngx_conf_full_name(cf->cycle, &value[1], 0) == NGX_ERROR) {
867 return NGX_CONF_ERROR;
870 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
871 if (log->script == NULL) {
872 return NGX_CONF_ERROR;
875 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
878 sc.source = &value[1];
879 sc.lengths = &log->script->lengths;
880 sc.values = &log->script->values;
882 sc.complete_lengths = 1;
883 sc.complete_values = 1;
885 if (ngx_http_script_compile(&sc) != NGX_OK) {
886 return NGX_CONF_ERROR;
890 if (cf->args->nelts >= 3) {
893 if (ngx_strcmp(name.data, "combined") == 0) {
894 lmcf->combined_used = 1;
898 name.len = sizeof("combined") - 1;
899 name.data = (u_char *) "combined";
900 lmcf->combined_used = 1;
903 fmt = lmcf->formats.elts;
904 for (i = 0; i < lmcf->formats.nelts; i++) {
905 if (fmt[i].name.len == name.len
906 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
908 log->format = &fmt[i];
913 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
914 "unknown log format \"%V\"", &name);
915 return NGX_CONF_ERROR;
919 if (cf->args->nelts == 4) {
920 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
921 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
922 "invalid parameter \"%V\"", &value[3]);
923 return NGX_CONF_ERROR;
927 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
928 "buffered logs can not have variables in name");
929 return NGX_CONF_ERROR;
932 name.len = value[3].len - 7;
933 name.data = value[3].data + 7;
935 buf = ngx_parse_size(&name);
937 if (buf == NGX_ERROR) {
938 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
939 "invalid parameter \"%V\"", &value[3]);
940 return NGX_CONF_ERROR;
943 if (log->file->buffer && log->file->last - log->file->pos != buf) {
944 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
945 "access_log \"%V\" already defined "
946 "with different buffer size", &value[1]);
947 return NGX_CONF_ERROR;
950 log->file->buffer = ngx_palloc(cf->pool, buf);
951 if (log->file->buffer == NULL) {
952 return NGX_CONF_ERROR;
955 log->file->pos = log->file->buffer;
956 log->file->last = log->file->buffer + buf;
964 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
966 ngx_http_log_main_conf_t *lmcf = conf;
970 ngx_http_log_fmt_t *fmt;
972 value = cf->args->elts;
974 fmt = lmcf->formats.elts;
975 for (i = 0; i < lmcf->formats.nelts; i++) {
976 if (fmt[i].name.len == value[1].len
977 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
979 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
980 "duplicate \"log_format\" name \"%V\"",
982 return NGX_CONF_ERROR;
986 fmt = ngx_array_push(&lmcf->formats);
988 return NGX_CONF_ERROR;
991 fmt->name = value[1];
993 fmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));
994 if (fmt->flushes == NULL) {
995 return NGX_CONF_ERROR;
998 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
999 if (fmt->ops == NULL) {
1000 return NGX_CONF_ERROR;
1003 return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
1008 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
1009 ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s)
1011 u_char *data, *p, ch;
1013 ngx_str_t *value, var;
1016 ngx_http_log_op_t *op;
1017 ngx_http_log_var_t *v;
1021 for ( /* void */ ; s < args->nelts; s++) {
1023 for (i = 0; i < value[s].len; i++) {
1024 if (value[s].data[i] != '%') {
1028 ch = value[s].data[i + 1];
1030 if ((ch >= 'A' && ch <= 'Z')
1031 || (ch >= 'a' && ch <= 'z')
1034 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1035 "the parameters in the \"%%name\" form are not supported, "
1036 "use the \"$variable\" instead");
1038 return NGX_CONF_ERROR;
1044 while (i < value[s].len) {
1046 op = ngx_array_push(ops);
1048 return NGX_CONF_ERROR;
1051 data = &value[s].data[i];
1053 if (value[s].data[i] == '$') {
1055 if (++i == value[s].len) {
1059 if (value[s].data[i] == '{') {
1062 if (++i == value[s].len) {
1066 var.data = &value[s].data[i];
1070 var.data = &value[s].data[i];
1073 for (var.len = 0; i < value[s].len; i++, var.len++) {
1074 ch = value[s].data[i];
1076 if (ch == '}' && bracket) {
1082 if ((ch >= 'A' && ch <= 'Z')
1083 || (ch >= 'a' && ch <= 'z')
1084 || (ch >= '0' && ch <= '9')
1094 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1095 "the closing bracket in \"%V\" "
1096 "variable is missing", &var);
1097 return NGX_CONF_ERROR;
1104 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
1105 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1106 "use \"$body_bytes_sent\" instead of "
1107 "\"$apache_bytes_sent\"");
1110 for (v = ngx_http_log_vars; v->name.len; v++) {
1112 if (v->name.len == var.len
1113 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
1124 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
1125 return NGX_CONF_ERROR;
1130 flush = ngx_array_push(flushes);
1131 if (flush == NULL) {
1132 return NGX_CONF_ERROR;
1135 *flush = op->data; /* variable index */
1145 while (i < value[s].len && value[s].data[i] != '$') {
1149 len = &value[s].data[i] - data;
1156 if (len <= sizeof(uintptr_t)) {
1157 op->run = ngx_http_log_copy_short;
1162 op->data |= data[len];
1166 op->run = ngx_http_log_copy_long;
1168 p = ngx_pnalloc(cf->pool, len);
1170 return NGX_CONF_ERROR;
1173 ngx_memcpy(p, data, len);
1174 op->data = (uintptr_t) p;
1184 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
1186 return NGX_CONF_ERROR;
1191 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1193 ngx_http_log_loc_conf_t *llcf = conf;
1195 time_t inactive, valid;
1196 ngx_str_t *value, s;
1197 ngx_int_t max, min_uses;
1200 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
1201 return "is duplicate";
1204 value = cf->args->elts;
1211 for (i = 1; i < cf->args->nelts; i++) {
1213 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
1215 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
1216 if (max == NGX_ERROR) {
1223 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
1225 s.len = value[i].len - 9;
1226 s.data = value[i].data + 9;
1228 inactive = ngx_parse_time(&s, 1);
1236 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
1238 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
1239 if (min_uses == NGX_ERROR) {
1246 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
1248 s.len = value[i].len - 6;
1249 s.data = value[i].data + 6;
1251 valid = ngx_parse_time(&s, 1);
1259 if (ngx_strcmp(value[i].data, "off") == 0) {
1261 llcf->open_file_cache = NULL;
1268 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1269 "invalid \"open_log_file_cache\" parameter \"%V\"",
1271 return NGX_CONF_ERROR;
1274 if (llcf->open_file_cache == NULL) {
1279 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1280 "\"open_log_file_cache\" must have \"max\" parameter");
1281 return NGX_CONF_ERROR;
1284 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
1286 if (llcf->open_file_cache) {
1288 llcf->open_file_cache_valid = valid;
1289 llcf->open_file_cache_min_uses = min_uses;
1294 return NGX_CONF_ERROR;
1299 ngx_http_log_init(ngx_conf_t *cf)
1303 ngx_http_handler_pt *h;
1304 ngx_http_log_fmt_t *fmt;
1305 ngx_http_log_main_conf_t *lmcf;
1306 ngx_http_core_main_conf_t *cmcf;
1308 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
1310 if (lmcf->combined_used) {
1311 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
1315 value = ngx_array_push(&a);
1316 if (value == NULL) {
1320 *value = ngx_http_combined_fmt;
1321 fmt = lmcf->formats.elts;
1323 if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
1330 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1332 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
1337 *h = ngx_http_log_handler;