blob: 93ff905685ce0c3934ca810cf9e511ff77de0eba [file] [log] [blame]
Igor Sysoev74e95c22003-11-09 20:03:38 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev74e95c22003-11-09 20:03:38 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9#include <ngx_http.h>
Igor Sysoev74e95c22003-11-09 20:03:38 +000010
11
Igor Sysoeva8c54c02006-11-27 14:46:15 +000012typedef struct ngx_http_log_op_s ngx_http_log_op_t;
13
14typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
15 ngx_http_log_op_t *op);
16
17typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
18 uintptr_t data);
19
20
21struct ngx_http_log_op_s {
22 size_t len;
23 ngx_http_log_op_getlen_pt getlen;
24 ngx_http_log_op_run_pt run;
25 uintptr_t data;
26};
27
Igor Sysoev09c684b2005-11-09 17:25:55 +000028
29typedef struct {
30 ngx_str_t name;
Igor Sysoev2eec1e12008-09-27 15:08:02 +000031 ngx_array_t *flushes;
Igor Sysoev09c684b2005-11-09 17:25:55 +000032 ngx_array_t *ops; /* array of ngx_http_log_op_t */
33} ngx_http_log_fmt_t;
34
Igor Sysoevef809b82006-06-28 16:00:26 +000035
Igor Sysoev09c684b2005-11-09 17:25:55 +000036typedef struct {
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;
40
Igor Sysoevef809b82006-06-28 16:00:26 +000041
Igor Sysoev09c684b2005-11-09 17:25:55 +000042typedef struct {
Igor Sysoevb8821542008-06-30 12:27:24 +000043 ngx_array_t *lengths;
44 ngx_array_t *values;
45} ngx_http_log_script_t;
46
47
48typedef struct {
Igor Sysoev09c684b2005-11-09 17:25:55 +000049 ngx_open_file_t *file;
Igor Sysoevb8821542008-06-30 12:27:24 +000050 ngx_http_log_script_t *script;
Igor Sysoev8a2b2fb2006-04-14 09:53:38 +000051 time_t disk_full_time;
Igor Sysoevd0863c12007-01-21 19:01:01 +000052 time_t error_log_time;
Igor Sysoev2eec1e12008-09-27 15:08:02 +000053 ngx_http_log_fmt_t *format;
Igor Sysoev09c684b2005-11-09 17:25:55 +000054} ngx_http_log_t;
55
Igor Sysoevef809b82006-06-28 16:00:26 +000056
Igor Sysoev09c684b2005-11-09 17:25:55 +000057typedef struct {
58 ngx_array_t *logs; /* array of ngx_http_log_t */
Igor Sysoevb8821542008-06-30 12:27:24 +000059
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;
63
Igor Sysoev09c684b2005-11-09 17:25:55 +000064 ngx_uint_t off; /* unsigned off:1 */
65} ngx_http_log_loc_conf_t;
66
67
68typedef struct {
69 ngx_str_t name;
70 size_t len;
71 ngx_http_log_op_run_pt run;
72} ngx_http_log_var_t;
73
74
Igor Sysoevd0863c12007-01-21 19:01:01 +000075static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
76 u_char *buf, size_t len);
Igor Sysoevb8821542008-06-30 12:27:24 +000077static 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);
Igor Sysoevd0863c12007-01-21 19:01:01 +000079
Igor Sysoev10a543a2004-03-16 07:10:12 +000080static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000081 ngx_http_log_op_t *op);
Igor Sysoev10a543a2004-03-16 07:10:12 +000082static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000083 ngx_http_log_op_t *op);
Igor Sysoev10a543a2004-03-16 07:10:12 +000084static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000085 ngx_http_log_op_t *op);
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +000086static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000087 ngx_http_log_op_t *op);
88static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
89 ngx_http_log_op_t *op);
Igor Sysoev10a543a2004-03-16 07:10:12 +000090static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000091 ngx_http_log_op_t *op);
Igor Sysoev09c684b2005-11-09 17:25:55 +000092static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000093 ngx_http_log_op_t *op);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000094static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +000095 u_char *buf, ngx_http_log_op_t *op);
Igor Sysoev6a12fc92004-12-06 14:45:08 +000096static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +000097 ngx_http_log_op_t *op);
Igor Sysoev02025fd2005-01-18 13:03:58 +000098
Igor Sysoevc1571722005-03-19 12:38:37 +000099static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
100 ngx_http_log_op_t *op, ngx_str_t *value);
101static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
102 uintptr_t data);
103static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
104 ngx_http_log_op_t *op);
Igor Sysoev5f3521c2008-05-15 15:09:39 +0000105static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
Igor Sysoevc1571722005-03-19 12:38:37 +0000106
107
Igor Sysoev74e95c22003-11-09 20:03:38 +0000108static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
109static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
110static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
Igor Sysoev11d75322005-03-01 15:20:36 +0000111 void *child);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000112static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev11d75322005-03-01 15:20:36 +0000113 void *conf);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000114static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev11d75322005-03-01 15:20:36 +0000115 void *conf);
Igor Sysoev09c684b2005-11-09 17:25:55 +0000116static char *ngx_http_log_compile_format(ngx_conf_t *cf,
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000117 ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
Igor Sysoevb8821542008-06-30 12:27:24 +0000118static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
119 void *conf);
Igor Sysoev09c684b2005-11-09 17:25:55 +0000120static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000121
122
123static ngx_command_t ngx_http_log_commands[] = {
124
Igor Sysoev11d75322005-03-01 15:20:36 +0000125 { 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,
129 0,
130 NULL },
Igor Sysoev74e95c22003-11-09 20:03:38 +0000131
Igor Sysoev11d75322005-03-01 15:20:36 +0000132 { ngx_string("access_log"),
Igor Sysoevac72bd12006-05-04 15:32:46 +0000133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
Igor Sysoev3fc6ee42007-10-09 18:45:49 +0000134 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
Igor Sysoev11d75322005-03-01 15:20:36 +0000135 ngx_http_log_set_log,
136 NGX_HTTP_LOC_CONF_OFFSET,
137 0,
138 NULL },
Igor Sysoev74e95c22003-11-09 20:03:38 +0000139
Igor Sysoevb8821542008-06-30 12:27:24 +0000140 { 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,
144 0,
145 NULL },
146
Igor Sysoev11d75322005-03-01 15:20:36 +0000147 ngx_null_command
Igor Sysoev74e95c22003-11-09 20:03:38 +0000148};
149
150
Igor Sysoev8f125582006-07-28 15:16:17 +0000151static ngx_http_module_t ngx_http_log_module_ctx = {
Igor Sysoeva8c54c02006-11-27 14:46:15 +0000152 NULL, /* preconfiguration */
Igor Sysoev09c684b2005-11-09 17:25:55 +0000153 ngx_http_log_init, /* postconfiguration */
Igor Sysoev78329332003-11-10 17:17:31 +0000154
Igor Sysoev74e95c22003-11-09 20:03:38 +0000155 ngx_http_log_create_main_conf, /* create main configuration */
156 NULL, /* init main configuration */
157
158 NULL, /* create server configuration */
159 NULL, /* merge server configuration */
160
161 ngx_http_log_create_loc_conf, /* create location configration */
162 ngx_http_log_merge_loc_conf /* merge location configration */
163};
164
165
166ngx_module_t ngx_http_log_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000167 NGX_MODULE_V1,
Igor Sysoev74e95c22003-11-09 20:03:38 +0000168 &ngx_http_log_module_ctx, /* module context */
169 ngx_http_log_commands, /* module directives */
170 NGX_HTTP_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000171 NULL, /* init master */
Igor Sysoev09c684b2005-11-09 17:25:55 +0000172 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000173 NULL, /* init process */
174 NULL, /* init thread */
175 NULL, /* exit thread */
176 NULL, /* exit process */
177 NULL, /* exit master */
178 NGX_MODULE_V1_PADDING
Igor Sysoev74e95c22003-11-09 20:03:38 +0000179};
180
181
Igor Sysoev3a58cc92007-01-24 09:15:25 +0000182static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000183
184
Igor Sysoev09c684b2005-11-09 17:25:55 +0000185static ngx_str_t ngx_http_combined_fmt =
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000186 ngx_string("$remote_addr - $remote_user [$time_local] "
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000187 "\"$request\" $status $body_bytes_sent "
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000188 "\"$http_referer\" \"$http_user_agent\"");
Igor Sysoev74e95c22003-11-09 20:03:38 +0000189
190
Igor Sysoev09c684b2005-11-09 17:25:55 +0000191static 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 },
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000194 { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
Igor Sysoev09c684b2005-11-09 17:25:55 +0000195 ngx_http_log_time },
196 { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000197 { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
198 ngx_http_log_request_time },
Igor Sysoev09c684b2005-11-09 17:25:55 +0000199 { ngx_string("status"), 3, ngx_http_log_status },
200 { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000201 { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
202 ngx_http_log_body_bytes_sent },
Igor Sysoev09c684b2005-11-09 17:25:55 +0000203 { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000204 ngx_http_log_body_bytes_sent },
Igor Sysoev09c684b2005-11-09 17:25:55 +0000205 { ngx_string("request_length"), NGX_SIZE_T_LEN,
206 ngx_http_log_request_length },
207
208 { ngx_null_string, 0, NULL }
209};
210
211
Igor Sysoev11d75322005-03-01 15:20:36 +0000212ngx_int_t
213ngx_http_log_handler(ngx_http_request_t *r)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000214{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000215 u_char *line, *p;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000216 size_t len;
Igor Sysoevd0863c12007-01-21 19:01:01 +0000217 ngx_uint_t i, l;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000218 ngx_http_log_t *log;
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000219 ngx_open_file_t *file;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000220 ngx_http_log_op_t *op;
221 ngx_http_log_loc_conf_t *lcf;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000222
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
224 "http log handler");
Igor Sysoev74e95c22003-11-09 20:03:38 +0000225
226 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
227
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000228 if (lcf->off) {
229 return NGX_OK;
230 }
231
Igor Sysoev74e95c22003-11-09 20:03:38 +0000232 log = lcf->logs->elts;
233 for (l = 0; l < lcf->logs->nelts; l++) {
234
Igor Sysoev8a2b2fb2006-04-14 09:53:38 +0000235 if (ngx_time() == log[l].disk_full_time) {
236
237 /*
Igor Sysoevd0863c12007-01-21 19:01:01 +0000238 * on FreeBSD writing to a full filesystem with enabled softupdates
Igor Sysoev8a2b2fb2006-04-14 09:53:38 +0000239 * may block process for much longer time than writing to non-full
Igor Sysoevd0863c12007-01-21 19:01:01 +0000240 * filesystem, so we skip writing to a log for one second
Igor Sysoev8a2b2fb2006-04-14 09:53:38 +0000241 */
242
243 continue;
244 }
245
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000246 ngx_http_script_flush_no_cacheable_variables(r, log[l].format->flushes);
247
Igor Sysoev74e95c22003-11-09 20:03:38 +0000248 len = 0;
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000249 op = log[l].format->ops->elts;
250 for (i = 0; i < log[l].format->ops->nelts; i++) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000251 if (op[i].len == 0) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000252 len += op[i].getlen(r, op[i].data);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000253
254 } else {
255 len += op[i].len;
256 }
257 }
258
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000259 len += NGX_LINEFEED_SIZE;
260
261 file = log[l].file;
262
Igor Sysoevb8821542008-06-30 12:27:24 +0000263 if (file && file->buffer) {
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000264
265 if (len > (size_t) (file->last - file->pos)) {
266
Igor Sysoevd0863c12007-01-21 19:01:01 +0000267 ngx_http_log_write(r, &log[l], file->buffer,
268 file->pos - file->buffer);
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000269
270 file->pos = file->buffer;
271 }
272
273 if (len <= (size_t) (file->last - file->pos)) {
274
275 p = file->pos;
276
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000277 for (i = 0; i < log[l].format->ops->nelts; i++) {
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000278 p = op[i].run(r, p, &op[i]);
279 }
280
281 ngx_linefeed(p);
282
283 file->pos = p;
284
285 continue;
286 }
287 }
Igor Sysoev74e95c22003-11-09 20:03:38 +0000288
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000289 line = ngx_pnalloc(r->pool, len);
Igor Sysoevc1571722005-03-19 12:38:37 +0000290 if (line == NULL) {
Igor Sysoev42b12b32004-12-02 18:40:46 +0000291 return NGX_ERROR;
292 }
293
Igor Sysoev74e95c22003-11-09 20:03:38 +0000294 p = line;
295
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000296 for (i = 0; i < log[l].format->ops->nelts; i++) {
Igor Sysoev02025fd2005-01-18 13:03:58 +0000297 p = op[i].run(r, p, &op[i]);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000298 }
299
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000300 ngx_linefeed(p);
301
Igor Sysoevd0863c12007-01-21 19:01:01 +0000302 ngx_http_log_write(r, &log[l], line, p - line);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000303 }
304
305 return NGX_OK;
306}
307
308
Igor Sysoevd0863c12007-01-21 19:01:01 +0000309static void
310ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
311 size_t len)
312{
Igor Sysoevb8821542008-06-30 12:27:24 +0000313 u_char *name;
314 time_t now;
315 ssize_t n;
316 ngx_err_t err;
Igor Sysoevd0863c12007-01-21 19:01:01 +0000317
Igor Sysoevb8821542008-06-30 12:27:24 +0000318 if (log->script == NULL) {
319 name = log->file->name.data;
320 n = ngx_write_fd(log->file->fd, buf, len);
321
322 } else {
323 name = NULL;
324 n = ngx_http_log_script_write(r, log->script, &name, buf, len);
325 }
Igor Sysoevd0863c12007-01-21 19:01:01 +0000326
327 if (n == (ssize_t) len) {
328 return;
329 }
330
331 now = ngx_time();
332
333 if (n == -1) {
334 err = ngx_errno;
335
336 if (err == NGX_ENOSPC) {
337 log->disk_full_time = now;
338 }
339
Igor Sysoevda4b6602007-04-03 21:54:13 +0000340 if (now - log->error_log_time > 59) {
Igor Sysoevd0863c12007-01-21 19:01:01 +0000341 ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
Igor Sysoevb8821542008-06-30 12:27:24 +0000342 ngx_write_fd_n " to \"%s\" failed", name);
Igor Sysoevd0863c12007-01-21 19:01:01 +0000343
344 log->error_log_time = now;
345 }
346
347 return;
348 }
349
Igor Sysoevda4b6602007-04-03 21:54:13 +0000350 if (now - log->error_log_time > 59) {
Igor Sysoevd0863c12007-01-21 19:01:01 +0000351 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
Igor Sysoevb8821542008-06-30 12:27:24 +0000352 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
353 name, n, len);
Igor Sysoevd0863c12007-01-21 19:01:01 +0000354
355 log->error_log_time = now;
356 }
357}
358
359
Igor Sysoevb8821542008-06-30 12:27:24 +0000360static ssize_t
361ngx_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)
363{
364 size_t root;
365 ssize_t n;
366 ngx_str_t log, path;
367 ngx_open_file_info_t of;
368 ngx_http_log_loc_conf_t *llcf;
369 ngx_http_core_loc_conf_t *clcf;
370
Igor Sysoevedf1c8c2008-07-07 09:26:13 +0000371 if (!r->root_tested) {
Igor Sysoevb8821542008-06-30 12:27:24 +0000372
373 /* test root directory existance */
374
375 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
376 /* simulate successfull logging */
377 return len;
378 }
379
380 path.data[root] = '\0';
381
382 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
383
384 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
385
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;
390
391 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
Igor Sysoevedf1c8c2008-07-07 09:26:13 +0000392 != NGX_OK)
Igor Sysoevb8821542008-06-30 12:27:24 +0000393 {
Igor Sysoevedf1c8c2008-07-07 09:26:13 +0000394 if (of.err == 0) {
395 /* simulate successfull logging */
396 return len;
397 }
398
399 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
400 "testing \"%s\" existence failed", path.data);
401
402 /* simulate successfull logging */
403 return len;
404 }
405
406 if (!of.is_dir) {
407 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ENOTDIR,
408 "testing \"%s\" existence failed", path.data);
409
410 /* simulate successfull logging */
Igor Sysoevb8821542008-06-30 12:27:24 +0000411 return len;
412 }
413 }
414
415 if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
416 script->values->elts)
417 == NULL)
418 {
419 /* simulate successfull logging */
420 return len;
421 }
422
423 log.data[log.len - 1] = '\0';
424 *name = log.data;
425
426 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
427 "http log \"%s\"", log.data);
428
429 llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
430
431 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
432
433 of.log = 1;
434 of.valid = llcf->open_file_cache_valid;
435 of.min_uses = llcf->open_file_cache_min_uses;
Igor Sysoevad1e1232008-10-16 13:31:00 +0000436 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
Igor Sysoevb8821542008-06-30 12:27:24 +0000437
438 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
439 != NGX_OK)
440 {
441 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
442 ngx_open_file_n " \"%s\" failed", log.data);
Igor Sysoev504efff2008-06-30 15:32:57 +0000443 /* simulate successfull logging */
444 return len;
Igor Sysoevb8821542008-06-30 12:27:24 +0000445 }
446
447 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
448 "http log #%d", of.fd);
449
450 n = ngx_write_fd(of.fd, buf, len);
451
452 return n;
453}
454
455
Igor Sysoev11d75322005-03-01 15:20:36 +0000456static u_char *
457ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
458 ngx_http_log_op_t *op)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000459{
460 size_t len;
461 uintptr_t data;
462
463 len = op->len;
464 data = op->data;
465
466 while (len--) {
467 *buf++ = (u_char) (data & 0xff);
468 data >>= 8;
469 }
470
471 return buf;
472}
473
474
Igor Sysoev11d75322005-03-01 15:20:36 +0000475static u_char *
476ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
477 ngx_http_log_op_t *op)
Igor Sysoev02025fd2005-01-18 13:03:58 +0000478{
479 return ngx_cpymem(buf, (u_char *) op->data, op->len);
480}
481
482
Igor Sysoev11d75322005-03-01 15:20:36 +0000483static u_char *
Igor Sysoev11d75322005-03-01 15:20:36 +0000484ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
485 ngx_http_log_op_t *op)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000486{
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000487 return ngx_sprintf(buf, "%ui", r->connection->number);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000488}
489
490
Igor Sysoev11d75322005-03-01 15:20:36 +0000491static u_char *
492ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000493{
494 if (r->pipeline) {
495 *buf = 'p';
496 } else {
497 *buf = '.';
498 }
499
500 return buf + 1;
501}
502
503
Igor Sysoev11d75322005-03-01 15:20:36 +0000504static u_char *
505ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000506{
Igor Sysoevd59a0472003-11-10 21:09:22 +0000507 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
508 ngx_cached_http_log_time.len);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000509}
510
511
Igor Sysoev11d75322005-03-01 15:20:36 +0000512static u_char *
513ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000514{
Igor Sysoevc2068d02005-10-19 12:33:58 +0000515 ngx_time_t *tp;
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000516
Igor Sysoevc2068d02005-10-19 12:33:58 +0000517 tp = ngx_timeofday();
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000518
Igor Sysoevc2068d02005-10-19 12:33:58 +0000519 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000520}
521
522
Igor Sysoev11d75322005-03-01 15:20:36 +0000523static u_char *
524ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
525 ngx_http_log_op_t *op)
526{
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000527 ngx_time_t *tp;
528 ngx_msec_int_t ms;
Igor Sysoev11d75322005-03-01 15:20:36 +0000529
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000530 tp = ngx_timeofday();
Igor Sysoev11d75322005-03-01 15:20:36 +0000531
Igor Sysoev1d04b142007-11-15 14:26:36 +0000532 ms = (ngx_msec_int_t)
533 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
Igor Sysoevb4ccb9f2007-04-21 07:50:19 +0000534 ms = (ms >= 0) ? ms : 0;
535
536 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
Igor Sysoev11d75322005-03-01 15:20:36 +0000537}
538
539
Igor Sysoev11d75322005-03-01 15:20:36 +0000540static u_char *
541ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000542{
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000543 return ngx_sprintf(buf, "%ui",
544 r->err_status ? r->err_status : r->headers_out.status);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000545}
546
547
Igor Sysoev11d75322005-03-01 15:20:36 +0000548static u_char *
Igor Sysoev09c684b2005-11-09 17:25:55 +0000549ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
550 ngx_http_log_op_t *op)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000551{
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000552 return ngx_sprintf(buf, "%O", r->connection->sent);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000553}
554
555
Igor Sysoev28eafa52007-07-06 11:28:20 +0000556/*
557 * although there is a real $body_bytes_sent variable,
558 * this log operation code function is more optimized for logging
559 */
560
Igor Sysoev11d75322005-03-01 15:20:36 +0000561static u_char *
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000562ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
Igor Sysoev11d75322005-03-01 15:20:36 +0000563 ngx_http_log_op_t *op)
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000564{
Igor Sysoev11d75322005-03-01 15:20:36 +0000565 off_t length;
566
567 length = r->connection->sent - r->header_size;
568
569 if (length > 0) {
570 return ngx_sprintf(buf, "%O", length);
571 }
572
573 *buf = '0';
574
575 return buf + 1;
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000576}
577
578
Igor Sysoev11d75322005-03-01 15:20:36 +0000579static u_char *
580ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
581 ngx_http_log_op_t *op)
Igor Sysoev6a12fc92004-12-06 14:45:08 +0000582{
Igor Sysoev1765f472006-07-07 16:33:19 +0000583 return ngx_sprintf(buf, "%O", r->request_length);
Igor Sysoev6a12fc92004-12-06 14:45:08 +0000584}
585
586
Igor Sysoev11d75322005-03-01 15:20:36 +0000587static ngx_int_t
Igor Sysoevc1571722005-03-19 12:38:37 +0000588ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
589 ngx_str_t *value)
590{
Igor Sysoev899b44e2005-05-12 14:58:06 +0000591 ngx_int_t index;
Igor Sysoevc1571722005-03-19 12:38:37 +0000592
Igor Sysoev899b44e2005-05-12 14:58:06 +0000593 index = ngx_http_get_variable_index(cf, value);
594 if (index == NGX_ERROR) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000595 return NGX_ERROR;
596 }
597
598 op->len = 0;
599 op->getlen = ngx_http_log_variable_getlen;
600 op->run = ngx_http_log_variable;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000601 op->data = index;
Igor Sysoevc1571722005-03-19 12:38:37 +0000602
603 return NGX_OK;
604}
605
606
607static size_t
608ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
609{
Igor Sysoev5f3521c2008-05-15 15:09:39 +0000610 uintptr_t len;
Igor Sysoevc1571722005-03-19 12:38:37 +0000611 ngx_http_variable_value_t *value;
612
613 value = ngx_http_get_indexed_variable(r, data);
614
Igor Sysoev09c684b2005-11-09 17:25:55 +0000615 if (value == NULL || value->not_found) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000616 return 1;
617 }
618
Igor Sysoev5f3521c2008-05-15 15:09:39 +0000619 len = ngx_http_log_escape(NULL, value->data, value->len);
620
621 value->escape = len ? 1 : 0;
622
623 return value->len + len * 3;
Igor Sysoevc1571722005-03-19 12:38:37 +0000624}
625
626
627static u_char *
628ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
629{
630 ngx_http_variable_value_t *value;
631
632 value = ngx_http_get_indexed_variable(r, op->data);
633
Igor Sysoev09c684b2005-11-09 17:25:55 +0000634 if (value == NULL || value->not_found) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000635 *buf = '-';
636 return buf + 1;
637 }
638
Igor Sysoev5f3521c2008-05-15 15:09:39 +0000639 if (value->escape == 0) {
640 return ngx_cpymem(buf, value->data, value->len);
641
642 } else {
643 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
644 }
645}
646
647
648static uintptr_t
649ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
650{
651 ngx_uint_t i, n;
652 static u_char hex[] = "0123456789ABCDEF";
653
654 static uint32_t escape[] = {
655 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
656
657 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
658 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
659
660 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
661 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
662
663 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
664 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
665
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 */
670 };
671
672
673 if (dst == NULL) {
674
675 /* find the number of the characters to be escaped */
676
677 n = 0;
678
679 for (i = 0; i < size; i++) {
680 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
681 n++;
682 }
683 src++;
684 }
685
686 return (uintptr_t) n;
687 }
688
689 for (i = 0; i < size; i++) {
690 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
691 *dst++ = '\\';
692 *dst++ = 'x';
693 *dst++ = hex[*src >> 4];
694 *dst++ = hex[*src & 0xf];
695 src++;
696
697 } else {
698 *dst++ = *src++;
699 }
700 }
701
702 return (uintptr_t) dst;
Igor Sysoevc1571722005-03-19 12:38:37 +0000703}
704
705
Igor Sysoev11d75322005-03-01 15:20:36 +0000706static void *
707ngx_http_log_create_main_conf(ngx_conf_t *cf)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000708{
709 ngx_http_log_main_conf_t *conf;
710
Igor Sysoev09c684b2005-11-09 17:25:55 +0000711 ngx_http_log_fmt_t *fmt;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000712
Igor Sysoevc1571722005-03-19 12:38:37 +0000713 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
714 if (conf == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000715 return NGX_CONF_ERROR;
716 }
717
Igor Sysoev11d75322005-03-01 15:20:36 +0000718 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
Igor Sysoev09c684b2005-11-09 17:25:55 +0000719 != NGX_OK)
Igor Sysoev11d75322005-03-01 15:20:36 +0000720 {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000721 return NGX_CONF_ERROR;
722 }
723
Igor Sysoev09c684b2005-11-09 17:25:55 +0000724 fmt = ngx_array_push(&conf->formats);
725 if (fmt == NULL) {
Igor Sysoev11d75322005-03-01 15:20:36 +0000726 return NGX_CONF_ERROR;
727 }
728
Igor Sysoev09c684b2005-11-09 17:25:55 +0000729 fmt->name.len = sizeof("combined") - 1;
730 fmt->name.data = (u_char *) "combined";
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000731
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000732 fmt->flushes = NULL;
733
Igor Sysoev09c684b2005-11-09 17:25:55 +0000734 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
735 if (fmt->ops == NULL) {
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000736 return NGX_CONF_ERROR;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000737 }
738
739 return conf;
740}
741
742
Igor Sysoev11d75322005-03-01 15:20:36 +0000743static void *
744ngx_http_log_create_loc_conf(ngx_conf_t *cf)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000745{
746 ngx_http_log_loc_conf_t *conf;
747
Igor Sysoevc1571722005-03-19 12:38:37 +0000748 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
749 if (conf == NULL) {
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000750 return NGX_CONF_ERROR;
751 }
Igor Sysoev74e95c22003-11-09 20:03:38 +0000752
Igor Sysoevb8821542008-06-30 12:27:24 +0000753 conf->open_file_cache = NGX_CONF_UNSET_PTR;
754
Igor Sysoev74e95c22003-11-09 20:03:38 +0000755 return conf;
756}
757
758
Igor Sysoev11d75322005-03-01 15:20:36 +0000759static char *
760ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000761{
762 ngx_http_log_loc_conf_t *prev = parent;
763 ngx_http_log_loc_conf_t *conf = child;
764
765 ngx_http_log_t *log;
766 ngx_http_log_fmt_t *fmt;
767 ngx_http_log_main_conf_t *lmcf;
768
Igor Sysoevb8821542008-06-30 12:27:24 +0000769 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
770
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;
774
775 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
776 conf->open_file_cache = NULL;
777 }
778 }
779
Igor Sysoev3a58cc92007-01-24 09:15:25 +0000780 if (conf->logs || conf->off) {
781 return NGX_CONF_OK;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000782 }
783
Igor Sysoev0da6d2f2008-06-30 15:51:28 +0000784 conf->logs = prev->logs;
785 conf->off = prev->off;
Igor Sysoev3a58cc92007-01-24 09:15:25 +0000786
787 if (conf->logs || conf->off) {
788 return NGX_CONF_OK;
789 }
790
791 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
792 if (conf->logs == NULL) {
793 return NGX_CONF_ERROR;
794 }
795
796 log = ngx_array_push(conf->logs);
797 if (log == NULL) {
798 return NGX_CONF_ERROR;
799 }
800
801 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
802 if (log->file == NULL) {
803 return NGX_CONF_ERROR;
804 }
805
Igor Sysoev06d21472008-07-01 06:26:48 +0000806 log->script = NULL;
Igor Sysoev3a58cc92007-01-24 09:15:25 +0000807 log->disk_full_time = 0;
808 log->error_log_time = 0;
809
810 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
811 fmt = lmcf->formats.elts;
812
813 /* the default "combined" format */
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000814 log->format = &fmt[0];
Igor Sysoev3a58cc92007-01-24 09:15:25 +0000815 lmcf->combined_used = 1;
816
Igor Sysoev74e95c22003-11-09 20:03:38 +0000817 return NGX_CONF_OK;
818}
819
820
Igor Sysoev11d75322005-03-01 15:20:36 +0000821static char *
822ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000823{
824 ngx_http_log_loc_conf_t *llcf = conf;
825
Igor Sysoevb8821542008-06-30 12:27:24 +0000826 ssize_t buf;
827 ngx_uint_t i, n;
828 ngx_str_t *value, name;
829 ngx_http_log_t *log;
830 ngx_http_log_fmt_t *fmt;
831 ngx_http_log_main_conf_t *lmcf;
832 ngx_http_script_compile_t sc;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000833
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000834 value = cf->args->elts;
835
836 if (ngx_strcmp(value[1].data, "off") == 0) {
837 llcf->off = 1;
838 return NGX_CONF_OK;
839 }
840
Igor Sysoev74e95c22003-11-09 20:03:38 +0000841 if (llcf->logs == NULL) {
Igor Sysoev85cccfb2004-09-15 16:00:43 +0000842 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
843 if (llcf->logs == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000844 return NGX_CONF_ERROR;
845 }
846 }
847
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000848 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
Igor Sysoev74e95c22003-11-09 20:03:38 +0000849
Igor Sysoevc1571722005-03-19 12:38:37 +0000850 log = ngx_array_push(llcf->logs);
851 if (log == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000852 return NGX_CONF_ERROR;
853 }
854
Igor Sysoevb8821542008-06-30 12:27:24 +0000855 ngx_memzero(log, sizeof(ngx_http_log_t));
Igor Sysoev74e95c22003-11-09 20:03:38 +0000856
Igor Sysoevb8821542008-06-30 12:27:24 +0000857 n = ngx_http_script_variables_count(&value[1]);
858
859 if (n == 0) {
860 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
861 if (log->file == NULL) {
862 return NGX_CONF_ERROR;
863 }
864
865 } else {
Igor Sysoev1f4220e2009-02-24 10:42:23 +0000866 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
Igor Sysoevb8821542008-06-30 12:27:24 +0000867 return NGX_CONF_ERROR;
868 }
869
870 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
871 if (log->script == NULL) {
872 return NGX_CONF_ERROR;
873 }
874
875 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
876
877 sc.cf = cf;
878 sc.source = &value[1];
879 sc.lengths = &log->script->lengths;
880 sc.values = &log->script->values;
881 sc.variables = n;
882 sc.complete_lengths = 1;
883 sc.complete_values = 1;
884
885 if (ngx_http_script_compile(&sc) != NGX_OK) {
886 return NGX_CONF_ERROR;
887 }
888 }
Igor Sysoev8a2b2fb2006-04-14 09:53:38 +0000889
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000890 if (cf->args->nelts >= 3) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000891 name = value[2];
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000892
893 if (ngx_strcmp(name.data, "combined") == 0) {
894 lmcf->combined_used = 1;
895 }
896
Igor Sysoev74e95c22003-11-09 20:03:38 +0000897 } else {
898 name.len = sizeof("combined") - 1;
Igor Sysoevda85f7f2004-03-16 21:26:01 +0000899 name.data = (u_char *) "combined";
Igor Sysoev09c684b2005-11-09 17:25:55 +0000900 lmcf->combined_used = 1;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000901 }
902
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)
907 {
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000908 log->format = &fmt[i];
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000909 goto buffer;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000910 }
911 }
912
Igor Sysoevaa828612005-02-09 14:31:07 +0000913 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
914 "unknown log format \"%V\"", &name);
Igor Sysoevaa828612005-02-09 14:31:07 +0000915 return NGX_CONF_ERROR;
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000916
917buffer:
918
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;
924 }
925
Igor Sysoevb8821542008-06-30 12:27:24 +0000926 if (log->script) {
927 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
928 "buffered logs can not have variables in name");
929 return NGX_CONF_ERROR;
930 }
931
Igor Sysoev697d1ae2005-10-27 15:46:13 +0000932 name.len = value[3].len - 7;
933 name.data = value[3].data + 7;
934
935 buf = ngx_parse_size(&name);
936
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;
941 }
942
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;
948 }
949
950 log->file->buffer = ngx_palloc(cf->pool, buf);
951 if (log->file->buffer == NULL) {
952 return NGX_CONF_ERROR;
953 }
954
955 log->file->pos = log->file->buffer;
956 log->file->last = log->file->buffer + buf;
957 }
958
959 return NGX_CONF_OK;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000960}
961
962
Igor Sysoev11d75322005-03-01 15:20:36 +0000963static char *
964ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000965{
966 ngx_http_log_main_conf_t *lmcf = conf;
967
Igor Sysoev09c684b2005-11-09 17:25:55 +0000968 ngx_str_t *value;
969 ngx_uint_t i;
970 ngx_http_log_fmt_t *fmt;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000971
972 value = cf->args->elts;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000973
974 fmt = lmcf->formats.elts;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000975 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)
Igor Sysoev74e95c22003-11-09 20:03:38 +0000978 {
Igor Sysoev2ae67272008-05-26 10:54:59 +0000979 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
980 "duplicate \"log_format\" name \"%V\"",
981 &value[1]);
982 return NGX_CONF_ERROR;
Igor Sysoev74e95c22003-11-09 20:03:38 +0000983 }
984 }
985
Igor Sysoevc1571722005-03-19 12:38:37 +0000986 fmt = ngx_array_push(&lmcf->formats);
987 if (fmt == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +0000988 return NGX_CONF_ERROR;
989 }
990
991 fmt->name = value[1];
992
Igor Sysoev2eec1e12008-09-27 15:08:02 +0000993 fmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));
994 if (fmt->flushes == NULL) {
995 return NGX_CONF_ERROR;
996 }
997
Igor Sysoev09c684b2005-11-09 17:25:55 +0000998 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
Igor Sysoevc1571722005-03-19 12:38:37 +0000999 if (fmt->ops == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00001000 return NGX_CONF_ERROR;
1001 }
1002
Igor Sysoev2eec1e12008-09-27 15:08:02 +00001003 return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
Igor Sysoev09c684b2005-11-09 17:25:55 +00001004}
Igor Sysoev74e95c22003-11-09 20:03:38 +00001005
Igor Sysoev09c684b2005-11-09 17:25:55 +00001006
1007static char *
Igor Sysoev2eec1e12008-09-27 15:08:02 +00001008ngx_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)
Igor Sysoev09c684b2005-11-09 17:25:55 +00001010{
Igor Sysoeva8c54c02006-11-27 14:46:15 +00001011 u_char *data, *p, ch;
1012 size_t i, len;
1013 ngx_str_t *value, var;
Igor Sysoev2eec1e12008-09-27 15:08:02 +00001014 ngx_int_t *flush;
Igor Sysoeva8c54c02006-11-27 14:46:15 +00001015 ngx_uint_t bracket;
1016 ngx_http_log_op_t *op;
1017 ngx_http_log_var_t *v;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001018
1019 value = args->elts;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001020
1021 for ( /* void */ ; s < args->nelts; s++) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00001022
Igor Sysoeva8c54c02006-11-27 14:46:15 +00001023 for (i = 0; i < value[s].len; i++) {
1024 if (value[s].data[i] != '%') {
1025 continue;
1026 }
1027
1028 ch = value[s].data[i + 1];
1029
1030 if ((ch >= 'A' && ch <= 'Z')
1031 || (ch >= 'a' && ch <= 'z')
1032 || ch == '{')
1033 {
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");
1037
1038 return NGX_CONF_ERROR;
1039 }
1040 }
1041
Igor Sysoev74e95c22003-11-09 20:03:38 +00001042 i = 0;
1043
1044 while (i < value[s].len) {
1045
Igor Sysoev09c684b2005-11-09 17:25:55 +00001046 op = ngx_array_push(ops);
Igor Sysoevc1571722005-03-19 12:38:37 +00001047 if (op == NULL) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00001048 return NGX_CONF_ERROR;
1049 }
1050
1051 data = &value[s].data[i];
1052
Igor Sysoeva8c54c02006-11-27 14:46:15 +00001053 if (value[s].data[i] == '$') {
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001054
1055 if (++i == value[s].len) {
1056 goto invalid;
1057 }
1058
1059 if (value[s].data[i] == '{') {
1060 bracket = 1;
1061
1062 if (++i == value[s].len) {
1063 goto invalid;
1064 }
1065
1066 var.data = &value[s].data[i];
1067
1068 } else {
1069 bracket = 0;
1070 var.data = &value[s].data[i];
1071 }
1072
1073 for (var.len = 0; i < value[s].len; i++, var.len++) {
1074 ch = value[s].data[i];
1075
1076 if (ch == '}' && bracket) {
1077 i++;
1078 bracket = 0;
1079 break;
1080 }
1081
1082 if ((ch >= 'A' && ch <= 'Z')
1083 || (ch >= 'a' && ch <= 'z')
1084 || (ch >= '0' && ch <= '9')
1085 || ch == '_')
1086 {
1087 continue;
1088 }
1089
Igor Sysoev74e95c22003-11-09 20:03:38 +00001090 break;
1091 }
1092
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001093 if (bracket) {
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;
1098 }
1099
1100 if (var.len == 0) {
1101 goto invalid;
1102 }
1103
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001104 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\"");
1108 }
1109
Igor Sysoev09c684b2005-11-09 17:25:55 +00001110 for (v = ngx_http_log_vars; v->name.len; v++) {
1111
1112 if (v->name.len == var.len
1113 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
1114 {
1115 op->len = v->len;
1116 op->getlen = NULL;
1117 op->run = v->run;
1118 op->data = 0;
1119
1120 goto found;
1121 }
1122 }
1123
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001124 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
1125 return NGX_CONF_ERROR;
1126 }
1127
Igor Sysoev2eec1e12008-09-27 15:08:02 +00001128 if (flushes) {
1129
1130 flush = ngx_array_push(flushes);
1131 if (flush == NULL) {
1132 return NGX_CONF_ERROR;
1133 }
1134
1135 *flush = op->data; /* variable index */
1136 }
1137
Igor Sysoev09c684b2005-11-09 17:25:55 +00001138 found:
1139
1140 continue;
1141 }
1142
1143 i++;
1144
Igor Sysoeva8c54c02006-11-27 14:46:15 +00001145 while (i < value[s].len && value[s].data[i] != '$') {
Igor Sysoev74e95c22003-11-09 20:03:38 +00001146 i++;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001147 }
Igor Sysoev74e95c22003-11-09 20:03:38 +00001148
Igor Sysoev09c684b2005-11-09 17:25:55 +00001149 len = &value[s].data[i] - data;
Igor Sysoev74e95c22003-11-09 20:03:38 +00001150
Igor Sysoev09c684b2005-11-09 17:25:55 +00001151 if (len) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00001152
Igor Sysoev09c684b2005-11-09 17:25:55 +00001153 op->len = len;
1154 op->getlen = NULL;
Igor Sysoev74e95c22003-11-09 20:03:38 +00001155
Igor Sysoev09c684b2005-11-09 17:25:55 +00001156 if (len <= sizeof(uintptr_t)) {
1157 op->run = ngx_http_log_copy_short;
1158 op->data = 0;
Igor Sysoev74e95c22003-11-09 20:03:38 +00001159
Igor Sysoev09c684b2005-11-09 17:25:55 +00001160 while (len--) {
1161 op->data <<= 8;
1162 op->data |= data[len];
Igor Sysoev74e95c22003-11-09 20:03:38 +00001163 }
Igor Sysoev09c684b2005-11-09 17:25:55 +00001164
1165 } else {
1166 op->run = ngx_http_log_copy_long;
1167
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +00001168 p = ngx_pnalloc(cf->pool, len);
Igor Sysoev09c684b2005-11-09 17:25:55 +00001169 if (p == NULL) {
1170 return NGX_CONF_ERROR;
1171 }
1172
1173 ngx_memcpy(p, data, len);
1174 op->data = (uintptr_t) p;
Igor Sysoev74e95c22003-11-09 20:03:38 +00001175 }
1176 }
1177 }
1178 }
1179
Igor Sysoev74e95c22003-11-09 20:03:38 +00001180 return NGX_CONF_OK;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001181
1182invalid:
1183
1184 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
1185
1186 return NGX_CONF_ERROR;
1187}
1188
1189
Igor Sysoevb8821542008-06-30 12:27:24 +00001190static char *
1191ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1192{
1193 ngx_http_log_loc_conf_t *llcf = conf;
1194
1195 time_t inactive, valid;
1196 ngx_str_t *value, s;
1197 ngx_int_t max, min_uses;
1198 ngx_uint_t i;
1199
1200 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
1201 return "is duplicate";
1202 }
1203
1204 value = cf->args->elts;
1205
1206 max = 0;
1207 inactive = 10;
1208 valid = 60;
1209 min_uses = 1;
1210
1211 for (i = 1; i < cf->args->nelts; i++) {
1212
1213 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
1214
1215 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
1216 if (max == NGX_ERROR) {
1217 goto failed;
1218 }
1219
1220 continue;
1221 }
1222
1223 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
1224
1225 s.len = value[i].len - 9;
1226 s.data = value[i].data + 9;
1227
1228 inactive = ngx_parse_time(&s, 1);
1229 if (inactive < 0) {
1230 goto failed;
1231 }
1232
1233 continue;
1234 }
1235
1236 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
1237
1238 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
1239 if (min_uses == NGX_ERROR) {
1240 goto failed;
1241 }
1242
1243 continue;
1244 }
1245
1246 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
1247
1248 s.len = value[i].len - 6;
1249 s.data = value[i].data + 6;
1250
1251 valid = ngx_parse_time(&s, 1);
1252 if (valid < 0) {
1253 goto failed;
1254 }
1255
1256 continue;
1257 }
1258
1259 if (ngx_strcmp(value[i].data, "off") == 0) {
1260
1261 llcf->open_file_cache = NULL;
1262
1263 continue;
1264 }
1265
1266 failed:
1267
1268 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1269 "invalid \"open_log_file_cache\" parameter \"%V\"",
1270 &value[i]);
1271 return NGX_CONF_ERROR;
1272 }
1273
1274 if (llcf->open_file_cache == NULL) {
1275 return NGX_CONF_OK;
1276 }
1277
1278 if (max == 0) {
1279 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1280 "\"open_log_file_cache\" must have \"max\" parameter");
1281 return NGX_CONF_ERROR;
1282 }
1283
1284 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
1285
1286 if (llcf->open_file_cache) {
1287
1288 llcf->open_file_cache_valid = valid;
1289 llcf->open_file_cache_min_uses = min_uses;
1290
1291 return NGX_CONF_OK;
1292 }
1293
1294 return NGX_CONF_ERROR;
1295}
1296
1297
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001298static ngx_int_t
Igor Sysoev09c684b2005-11-09 17:25:55 +00001299ngx_http_log_init(ngx_conf_t *cf)
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001300{
Igor Sysoev09c684b2005-11-09 17:25:55 +00001301 ngx_str_t *value;
1302 ngx_array_t a;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001303 ngx_http_handler_pt *h;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001304 ngx_http_log_fmt_t *fmt;
1305 ngx_http_log_main_conf_t *lmcf;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001306 ngx_http_core_main_conf_t *cmcf;
1307
Igor Sysoev09c684b2005-11-09 17:25:55 +00001308 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
1309
1310 if (lmcf->combined_used) {
1311 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
1312 return NGX_ERROR;
1313 }
1314
1315 value = ngx_array_push(&a);
1316 if (value == NULL) {
1317 return NGX_ERROR;
1318 }
1319
1320 *value = ngx_http_combined_fmt;
1321 fmt = lmcf->formats.elts;
1322
Igor Sysoev2eec1e12008-09-27 15:08:02 +00001323 if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
Igor Sysoev09c684b2005-11-09 17:25:55 +00001324 != NGX_CONF_OK)
1325 {
1326 return NGX_ERROR;
1327 }
1328 }
1329
1330 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001331
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001332 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
1333 if (h == NULL) {
1334 return NGX_ERROR;
1335 }
1336
1337 *h = ngx_http_log_handler;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001338
1339 return NGX_OK;
Igor Sysoev74e95c22003-11-09 20:03:38 +00001340}