3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 static u_char
*ngx_http_log_addr(ngx_http_request_t
*r
, u_char
*buf
,
15 static u_char
*ngx_http_log_connection(ngx_http_request_t
*r
, u_char
*buf
,
17 static u_char
*ngx_http_log_pipe(ngx_http_request_t
*r
, u_char
*buf
,
19 static u_char
*ngx_http_log_time(ngx_http_request_t
*r
, u_char
*buf
,
21 static u_char
*ngx_http_log_msec(ngx_http_request_t
*r
, u_char
*buf
,
23 static u_char
*ngx_http_log_request(ngx_http_request_t
*r
, u_char
*buf
,
25 static u_char
*ngx_http_log_status(ngx_http_request_t
*r
, u_char
*buf
,
27 static u_char
*ngx_http_log_length(ngx_http_request_t
*r
, u_char
*buf
,
29 static u_char
*ngx_http_log_apache_length(ngx_http_request_t
*r
, u_char
*buf
,
31 static u_char
*ngx_http_log_header_in(ngx_http_request_t
*r
, u_char
*buf
,
33 static u_char
*ngx_http_log_connection_header_out(ngx_http_request_t
*r
,
34 u_char
*buf
, uintptr_t data
);
35 static u_char
*ngx_http_log_transfer_encoding_header_out(ngx_http_request_t
*r
,
38 static u_char
*ngx_http_log_unknown_header_in(ngx_http_request_t
*r
,
39 u_char
*buf
, uintptr_t data
);
40 static u_char
*ngx_http_log_header_out(ngx_http_request_t
*r
, u_char
*buf
,
42 static u_char
*ngx_http_log_unknown_header_out(ngx_http_request_t
*r
, u_char
*buf
,
45 static ngx_int_t
ngx_http_log_pre_conf(ngx_conf_t
*cf
);
46 static void *ngx_http_log_create_main_conf(ngx_conf_t
*cf
);
47 static void *ngx_http_log_create_loc_conf(ngx_conf_t
*cf
);
48 static char *ngx_http_log_merge_loc_conf(ngx_conf_t
*cf
, void *parent
,
50 static char *ngx_http_log_set_log(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
52 static char *ngx_http_log_set_format(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
54 static ngx_int_t
ngx_http_log_parse_format(ngx_conf_t
*cf
, ngx_array_t
*ops
,
58 static ngx_command_t ngx_http_log_commands
[] = {
60 {ngx_string("log_format"),
61 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_2MORE
,
62 ngx_http_log_set_format
,
63 NGX_HTTP_MAIN_CONF_OFFSET
,
67 {ngx_string("access_log"),
68 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE12
,
70 NGX_HTTP_LOC_CONF_OFFSET
,
78 ngx_http_module_t ngx_http_log_module_ctx
= {
79 ngx_http_log_pre_conf
, /* pre conf */
81 ngx_http_log_create_main_conf
, /* create main configuration */
82 NULL
, /* init main configuration */
84 NULL
, /* create server configuration */
85 NULL
, /* merge server configuration */
87 ngx_http_log_create_loc_conf
, /* create location configration */
88 ngx_http_log_merge_loc_conf
/* merge location configration */
92 ngx_module_t ngx_http_log_module
= {
94 &ngx_http_log_module_ctx
, /* module context */
95 ngx_http_log_commands
, /* module directives */
96 NGX_HTTP_MODULE
, /* module type */
97 NULL
, /* init module */
102 static ngx_str_t http_access_log
= ngx_string(NGX_HTTP_LOG_PATH
);
105 static ngx_str_t ngx_http_combined_fmt
=
106 ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
107 "\"%{Referer}i\" \"%{User-Agent}i\"");
110 ngx_http_log_op_name_t ngx_http_log_fmt_ops
[] = {
111 { ngx_string("addr"), INET_ADDRSTRLEN
- 1, ngx_http_log_addr
},
112 { ngx_string("conn"), NGX_INT32_LEN
, ngx_http_log_connection
},
113 { ngx_string("pipe"), 1, ngx_http_log_pipe
},
114 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00") - 1,
116 { ngx_string("msec"), TIME_T_LEN
+ 4, ngx_http_log_msec
},
117 { ngx_string("request"), 0, ngx_http_log_request
},
118 { ngx_string("status"), 3, ngx_http_log_status
},
119 { ngx_string("length"), NGX_OFF_T_LEN
, ngx_http_log_length
},
120 { ngx_string("apache_length"), NGX_OFF_T_LEN
, ngx_http_log_apache_length
},
121 { ngx_string("i"), NGX_HTTP_LOG_ARG
, ngx_http_log_header_in
},
122 { ngx_string("o"), NGX_HTTP_LOG_ARG
, ngx_http_log_header_out
},
123 { ngx_null_string
, 0, NULL
}
127 ngx_int_t
ngx_http_log_handler(ngx_http_request_t
*r
)
134 ngx_http_log_op_t
*op
;
135 ngx_http_log_loc_conf_t
*lcf
;
140 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
143 lcf
= ngx_http_get_module_loc_conf(r
, ngx_http_log_module
);
149 log
= lcf
->logs
->elts
;
150 for (l
= 0; l
< lcf
->logs
->nelts
; l
++) {
153 op
= log
[l
].ops
->elts
;
154 for (i
= 0; i
< log
[l
].ops
->nelts
; i
++) {
155 if (op
[i
].len
== 0) {
156 len
+= (size_t) op
[i
].op(r
, NULL
, op
[i
].data
);
169 if (!(line
= ngx_palloc(r
->pool
, len
))) {
175 for (i
= 0; i
< log
[l
].ops
->nelts
; i
++) {
176 if (op
[i
].op
== NGX_HTTP_LOG_COPY_SHORT
) {
180 *p
++ = (char) (data
& 0xff);
184 } else if (op
[i
].op
== NGX_HTTP_LOG_COPY_LONG
) {
185 p
= ngx_cpymem(p
, (void *) op
[i
].data
, op
[i
].len
);
188 p
= op
[i
].op(r
, p
, op
[i
].data
);
193 *p
++ = CR
; *p
++ = LF
;
194 WriteFile(log
[l
].file
->fd
, line
, p
- line
, &written
, NULL
);
197 write(log
[l
].file
->fd
, line
, p
- line
);
205 static u_char
*ngx_http_log_addr(ngx_http_request_t
*r
, u_char
*buf
,
208 return ngx_cpymem(buf
, r
->connection
->addr_text
.data
,
209 r
->connection
->addr_text
.len
);
213 static u_char
*ngx_http_log_connection(ngx_http_request_t
*r
, u_char
*buf
,
216 return ngx_sprintf(buf
, "%ui", r
->connection
->number
);
220 static u_char
*ngx_http_log_pipe(ngx_http_request_t
*r
, u_char
*buf
,
233 static u_char
*ngx_http_log_time(ngx_http_request_t
*r
, u_char
*buf
,
236 return ngx_cpymem(buf
, ngx_cached_http_log_time
.data
,
237 ngx_cached_http_log_time
.len
);
241 static u_char
*ngx_http_log_msec(ngx_http_request_t
*r
, u_char
*buf
,
246 ngx_gettimeofday(&tv
);
248 return ngx_sprintf(buf
, "%l.%03l", tv
.tv_sec
, tv
.tv_usec
/ 1000);
252 static u_char
*ngx_http_log_request(ngx_http_request_t
*r
, u_char
*buf
,
256 /* find the request line length */
257 return (u_char
*) r
->request_line
.len
;
260 return ngx_cpymem(buf
, r
->request_line
.data
, r
->request_line
.len
);
264 static u_char
*ngx_http_log_status(ngx_http_request_t
*r
, u_char
*buf
,
267 return ngx_sprintf(buf
, "%ui",
268 r
->err_status
? r
->err_status
: r
->headers_out
.status
);
272 static u_char
*ngx_http_log_length(ngx_http_request_t
*r
, u_char
*buf
,
275 return ngx_sprintf(buf
, "%O", r
->connection
->sent
);
279 static u_char
*ngx_http_log_apache_length(ngx_http_request_t
*r
, u_char
*buf
,
282 return ngx_sprintf(buf
, "%O", r
->connection
->sent
- r
->header_size
);
286 static u_char
*ngx_http_log_header_in(ngx_http_request_t
*r
, u_char
*buf
,
292 ngx_http_log_op_t
*op
;
295 h
= *(ngx_table_elt_t
**) ((char *) &r
->headers_in
+ data
);
309 /* find the header length */
310 return (u_char
*) h
->value
.len
;
313 return ngx_cpymem(buf
, h
->value
.data
, h
->value
.len
);
316 /* find an offset while a format string compilation */
318 op
= (ngx_http_log_op_t
*) buf
;
319 s
= (ngx_str_t
*) data
;
323 for (i
= 0; ngx_http_headers_in
[i
].name
.len
!= 0; i
++) {
324 if (ngx_http_headers_in
[i
].name
.len
!= s
->len
) {
328 if (ngx_strncasecmp(ngx_http_headers_in
[i
].name
.data
, s
->data
, s
->len
)
331 op
->op
= ngx_http_log_header_in
;
332 op
->data
= ngx_http_headers_in
[i
].offset
;
337 op
->op
= ngx_http_log_unknown_header_in
;
338 op
->data
= (uintptr_t) s
;
344 static u_char
*ngx_http_log_unknown_header_in(ngx_http_request_t
*r
,
345 u_char
*buf
, uintptr_t data
)
349 ngx_list_part_t
*part
;
352 s
= (ngx_str_t
*) data
;
354 part
= &r
->headers_in
.headers
.part
;
357 for (i
= 0; /* void */; i
++) {
359 if (i
>= part
->nelts
) {
360 if (part
->next
== NULL
) {
369 if (h
[i
].key
.len
!= s
->len
) {
373 if (ngx_strncasecmp(h
[i
].key
.data
, s
->data
, s
->len
) == 0) {
375 /* find the header length */
376 return (u_char
*) h
[i
].value
.len
;
379 return ngx_cpymem(buf
, h
[i
].value
.data
, h
[i
].value
.len
);
393 static u_char
*ngx_http_log_header_out(ngx_http_request_t
*r
, u_char
*buf
,
399 ngx_http_log_op_t
*op
;
403 /* run-time execution */
405 if (r
->http_version
< NGX_HTTP_VERSION_10
) {
413 h
= *(ngx_table_elt_t
**) ((char *) &r
->headers_out
+ data
);
418 * No header pointer was found.
419 * However, some headers: "Date", "Server", "Content-Length",
420 * and "Last-Modified" have a special handling in the header filter
421 * but we do not set up their pointers in the filter because
422 * they are too seldom needed to be logged.
425 if (data
== offsetof(ngx_http_headers_out_t
, date
)) {
427 return (u_char
*) ngx_cached_http_time
.len
;
429 return ngx_cpymem(buf
, ngx_cached_http_time
.data
,
430 ngx_cached_http_time
.len
);
433 if (data
== offsetof(ngx_http_headers_out_t
, server
)) {
435 return (u_char
*) (sizeof(NGINX_VER
) - 1);
437 return ngx_cpymem(buf
, NGINX_VER
, sizeof(NGINX_VER
) - 1);
440 if (data
== offsetof(ngx_http_headers_out_t
, content_length
)) {
441 if (r
->headers_out
.content_length_n
== -1) {
449 return (u_char
*) NGX_OFF_T_LEN
;
451 return ngx_sprintf(buf
, "%O", r
->headers_out
.content_length_n
);
454 if (data
== offsetof(ngx_http_headers_out_t
, last_modified
)) {
455 if (r
->headers_out
.last_modified_time
== -1) {
464 sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
466 return ngx_http_time(buf
, r
->headers_out
.last_modified_time
);
477 /* find the header length */
478 return (u_char
*) h
->value
.len
;
481 return ngx_cpymem(buf
, h
->value
.data
, h
->value
.len
);
484 /* find an offset while a format string compilation */
486 op
= (ngx_http_log_op_t
*) buf
;
487 s
= (ngx_str_t
*) data
;
491 for (i
= 0; ngx_http_headers_out
[i
].name
.len
!= 0; i
++) {
492 if (ngx_http_headers_out
[i
].name
.len
!= s
->len
) {
496 if (ngx_strncasecmp(ngx_http_headers_out
[i
].name
.data
, s
->data
, s
->len
)
499 op
->op
= ngx_http_log_header_out
;
500 op
->data
= ngx_http_headers_out
[i
].offset
;
505 if (s
->len
== sizeof("Connection") - 1
506 && ngx_strncasecmp(s
->data
, "Connection", s
->len
) == 0)
508 op
->op
= ngx_http_log_connection_header_out
;
509 op
->data
= (uintptr_t) NULL
;
513 if (s
->len
== sizeof("Transfer-Encoding") - 1
514 && ngx_strncasecmp(s
->data
, "Transfer-Encoding", s
->len
) == 0) {
515 op
->op
= ngx_http_log_transfer_encoding_header_out
;
516 op
->data
= (uintptr_t) NULL
;
520 op
->op
= ngx_http_log_unknown_header_out
;
521 op
->data
= (uintptr_t) s
;
527 static u_char
*ngx_http_log_connection_header_out(ngx_http_request_t
*r
,
528 u_char
*buf
, uintptr_t data
)
531 return (u_char
*) ((r
->keepalive
) ? sizeof("keep-alive") - 1:
532 sizeof("close") - 1);
536 return ngx_cpymem(buf
, "keep-alive", sizeof("keep-alive") - 1);
539 return ngx_cpymem(buf
, "close", sizeof("close") - 1);
544 static u_char
*ngx_http_log_transfer_encoding_header_out(ngx_http_request_t
*r
,
549 return (u_char
*) ((r
->chunked
) ? sizeof("chunked") - 1 : 1);
553 return ngx_cpymem(buf
, "chunked", sizeof("chunked") - 1);
562 static u_char
*ngx_http_log_unknown_header_out(ngx_http_request_t
*r
,
568 ngx_list_part_t
*part
;
571 s
= (ngx_str_t
*) data
;
573 part
= &r
->headers_out
.headers
.part
;
576 for (i
= 0; /* void */; i
++) {
578 if (i
>= part
->nelts
) {
579 if (part
->next
== NULL
) {
588 if (h
[i
].key
.len
!= s
->len
) {
592 if (ngx_strncasecmp(h
[i
].key
.data
, s
->data
, s
->len
) == 0) {
594 /* find the header length */
595 return (u_char
*) h
[i
].value
.len
;
598 return ngx_cpymem(buf
, h
[i
].value
.data
, h
[i
].value
.len
);
612 static ngx_int_t
ngx_http_log_pre_conf(ngx_conf_t
*cf
)
614 ngx_http_log_op_name_t
*op
;
616 for (op
= ngx_http_log_fmt_ops
; op
->name
.len
; op
++) { /* void */ }
623 static void *ngx_http_log_create_main_conf(ngx_conf_t
*cf
)
625 ngx_http_log_main_conf_t
*conf
;
630 if (!(conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_log_main_conf_t
)))) {
631 return NGX_CONF_ERROR
;
634 ngx_init_array(conf
->formats
, cf
->pool
, 5, sizeof(ngx_http_log_fmt_t
),
639 if (!(value
= ngx_push_array(cf
->args
))) {
640 return NGX_CONF_ERROR
;
643 if (!(value
= ngx_push_array(cf
->args
))) {
644 return NGX_CONF_ERROR
;
647 value
->len
= sizeof("combined") - 1;
648 value
->data
= (u_char
*) "combined";
650 if (!(value
= ngx_push_array(cf
->args
))) {
651 return NGX_CONF_ERROR
;
654 *value
= ngx_http_combined_fmt
;
656 rc
= ngx_http_log_set_format(cf
, NULL
, conf
);
657 if (rc
!= NGX_CONF_OK
) {
665 static void *ngx_http_log_create_loc_conf(ngx_conf_t
*cf
)
667 ngx_http_log_loc_conf_t
*conf
;
669 if (!(conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_log_loc_conf_t
)))) {
670 return NGX_CONF_ERROR
;
677 static char *ngx_http_log_merge_loc_conf(ngx_conf_t
*cf
, void *parent
,
680 ngx_http_log_loc_conf_t
*prev
= parent
;
681 ngx_http_log_loc_conf_t
*conf
= child
;
684 ngx_http_log_fmt_t
*fmt
;
685 ngx_http_log_main_conf_t
*lmcf
;
687 if (conf
->logs
== NULL
) {
694 conf
->logs
= prev
->logs
;
699 conf
->off
= prev
->off
;
703 conf
->logs
= ngx_array_create(cf
->pool
, 2, sizeof(ngx_http_log_t
));
704 if (conf
->logs
== NULL
) {
705 return NGX_CONF_ERROR
;
708 if (!(log
= ngx_array_push(conf
->logs
))) {
709 return NGX_CONF_ERROR
;
712 log
->file
= ngx_conf_open_file(cf
->cycle
, &http_access_log
);
713 if (log
->file
== NULL
) {
714 return NGX_CONF_ERROR
;
717 lmcf
= ngx_http_conf_get_module_main_conf(cf
, ngx_http_log_module
);
718 fmt
= lmcf
->formats
.elts
;
720 /* the default "combined" format */
721 log
->ops
= fmt
[0].ops
;
729 static char *ngx_http_log_set_log(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
732 ngx_http_log_loc_conf_t
*llcf
= conf
;
735 ngx_str_t
*value
, name
;
737 ngx_http_log_fmt_t
*fmt
;
738 ngx_http_log_main_conf_t
*lmcf
;
740 value
= cf
->args
->elts
;
742 if (ngx_strcmp(value
[1].data
, "off") == 0) {
747 if (llcf
->logs
== NULL
) {
748 llcf
->logs
= ngx_array_create(cf
->pool
, 2, sizeof(ngx_http_log_t
));
749 if (llcf
->logs
== NULL
) {
750 return NGX_CONF_ERROR
;
754 lmcf
= ngx_http_conf_get_module_main_conf(cf
, ngx_http_log_module
);
756 if (!(log
= ngx_array_push(llcf
->logs
))) {
757 return NGX_CONF_ERROR
;
760 if (!(log
->file
= ngx_conf_open_file(cf
->cycle
, &value
[1]))) {
761 return NGX_CONF_ERROR
;
764 if (cf
->args
->nelts
== 3) {
767 name
.len
= sizeof("combined") - 1;
768 name
.data
= (u_char
*) "combined";
771 fmt
= lmcf
->formats
.elts
;
772 for (i
= 0; i
< lmcf
->formats
.nelts
; i
++) {
773 if (fmt
[i
].name
.len
== name
.len
774 && ngx_strcasecmp(fmt
[i
].name
.data
, name
.data
) == 0)
776 log
->ops
= fmt
[i
].ops
;
785 static char *ngx_http_log_set_format(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
788 ngx_http_log_main_conf_t
*lmcf
= conf
;
790 ngx_uint_t s
, f
, invalid
;
791 u_char
*data
, *p
, *fname
;
792 size_t i
, len
, fname_len
;
793 ngx_str_t
*value
, arg
, *a
;
794 ngx_http_log_op_t
*op
;
795 ngx_http_log_fmt_t
*fmt
;
796 ngx_http_log_op_name_t
*name
;
798 value
= cf
->args
->elts
;
800 fmt
= lmcf
->formats
.elts
;
801 for (f
= 0; f
< lmcf
->formats
.nelts
; f
++) {
802 if (fmt
[f
].name
.len
== value
[1].len
803 && ngx_strcmp(fmt
->name
.data
, value
[1].data
) == 0)
805 return "duplicate \"log_format\" name";
809 if (!(fmt
= ngx_push_array(&lmcf
->formats
))) {
810 return NGX_CONF_ERROR
;
813 fmt
->name
= value
[1];
815 if (!(fmt
->ops
= ngx_create_array(cf
->pool
, 20,
816 sizeof(ngx_http_log_op_t
)))) {
817 return NGX_CONF_ERROR
;
823 for (s
= 2; s
< cf
->args
->nelts
&& !invalid
; s
++) {
827 while (i
< value
[s
].len
) {
829 if (!(op
= ngx_push_array(fmt
->ops
))) {
830 return NGX_CONF_ERROR
;
833 data
= &value
[s
].data
[i
];
835 if (value
[s
].data
[i
] == '%') {
838 if (i
== value
[s
].len
) {
843 if (value
[s
].data
[i
] == '{') {
846 arg
.data
= &value
[s
].data
[i
];
848 while (i
< value
[s
].len
&& value
[s
].data
[i
] != '}') {
852 arg
.len
= &value
[s
].data
[i
] - arg
.data
;
854 if (i
== value
[s
].len
|| arg
.len
== 0) {
865 fname
= &value
[s
].data
[i
];
867 while (i
< value
[s
].len
868 && ((value
[s
].data
[i
] >= 'a' && value
[s
].data
[i
] <= 'z')
869 || value
[s
].data
[i
] == '_'))
874 fname_len
= &value
[s
].data
[i
] - fname
;
876 if (fname_len
== 0) {
881 for (name
= ngx_http_log_fmt_ops
; name
->op
; name
++) {
882 if (name
->name
.len
== 0) {
883 name
= (ngx_http_log_op_name_t
*) name
->op
;
886 if (name
->name
.len
== fname_len
887 && ngx_strncmp(name
->name
.data
, fname
, fname_len
) == 0)
889 if (name
->len
!= NGX_HTTP_LOG_ARG
) {
891 fname
[fname_len
] = '\0';
892 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
893 "\"%s\" must not have argument",
895 return NGX_CONF_ERROR
;
906 fname
[fname_len
] = '\0';
907 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
908 "\"%s\" requires argument",
910 return NGX_CONF_ERROR
;
913 if (!(a
= ngx_palloc(cf
->pool
, sizeof(ngx_str_t
)))) {
914 return NGX_CONF_ERROR
;
918 name
->op(NULL
, (u_char
*) op
, (uintptr_t) a
);
924 if (name
->name
.len
== 0) {
932 while (i
< value
[s
].len
&& value
[s
].data
[i
] != '%') {
936 len
= &value
[s
].data
[i
] - data
;
942 if (len
<= sizeof(uintptr_t)) {
943 op
->op
= NGX_HTTP_LOG_COPY_SHORT
;
948 op
->data
|= data
[len
];
952 op
->op
= NGX_HTTP_LOG_COPY_LONG
;
954 if (!(p
= ngx_palloc(cf
->pool
, len
))) {
955 return NGX_CONF_ERROR
;
958 ngx_memcpy(p
, data
, len
);
959 op
->data
= (uintptr_t) p
;
967 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
968 "invalid parameter \"%s\"", data
);
969 return NGX_CONF_ERROR
;