118
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10 #include <nginx.h>
|
|
11
|
|
12
|
260
|
13 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
|
|
14
|
|
15 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
|
|
16 ngx_http_log_op_t *op);
|
|
17
|
|
18 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
|
|
19 uintptr_t data);
|
|
20
|
|
21
|
|
22 struct ngx_http_log_op_s {
|
|
23 size_t len;
|
|
24 ngx_http_log_op_getlen_pt getlen;
|
|
25 ngx_http_log_op_run_pt run;
|
|
26 uintptr_t data;
|
|
27 };
|
|
28
|
122
|
29
|
|
30 typedef struct {
|
|
31 ngx_str_t name;
|
|
32 ngx_array_t *ops; /* array of ngx_http_log_op_t */
|
|
33 } ngx_http_log_fmt_t;
|
|
34
|
206
|
35
|
122
|
36 typedef 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
|
206
|
41
|
122
|
42 typedef struct {
|
384
|
43 ngx_array_t *lengths;
|
|
44 ngx_array_t *values;
|
|
45 } ngx_http_log_script_t;
|
|
46
|
|
47
|
|
48 typedef struct {
|
122
|
49 ngx_open_file_t *file;
|
384
|
50 ngx_http_log_script_t *script;
|
182
|
51 time_t disk_full_time;
|
278
|
52 time_t error_log_time;
|
122
|
53 ngx_array_t *ops; /* array of ngx_http_log_op_t */
|
|
54 } ngx_http_log_t;
|
|
55
|
206
|
56
|
122
|
57 typedef struct {
|
|
58 ngx_array_t *logs; /* array of ngx_http_log_t */
|
384
|
59
|
|
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
|
122
|
64 ngx_uint_t off; /* unsigned off:1 */
|
|
65 } ngx_http_log_loc_conf_t;
|
|
66
|
|
67
|
|
68 typedef 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
|
278
|
75 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
|
|
76 u_char *buf, size_t len);
|
384
|
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);
|
278
|
79
|
118
|
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);
|
122
|
92 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
93 ngx_http_log_op_t *op);
|
126
|
94 static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
|
122
|
95 u_char *buf, ngx_http_log_op_t *op);
|
118
|
96 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
97 ngx_http_log_op_t *op);
|
|
98
|
|
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,
|
|
102 uintptr_t data);
|
|
103 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
|
|
104 ngx_http_log_op_t *op);
|
376
|
105 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
|
118
|
106
|
|
107
|
|
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,
|
|
111 void *child);
|
|
112 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
113 void *conf);
|
|
114 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
115 void *conf);
|
122
|
116 static char *ngx_http_log_compile_format(ngx_conf_t *cf,
|
|
117 ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
|
384
|
118 static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
119 void *conf);
|
122
|
120 static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
|
118
|
121
|
|
122
|
|
123 static ngx_command_t ngx_http_log_commands[] = {
|
|
124
|
|
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,
|
|
129 0,
|
|
130 NULL },
|
|
131
|
|
132 { ngx_string("access_log"),
|
194
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
340
|
134 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
|
118
|
135 ngx_http_log_set_log,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
|
137 0,
|
|
138 NULL },
|
|
139
|
384
|
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,
|
|
144 0,
|
|
145 NULL },
|
|
146
|
118
|
147 ngx_null_command
|
|
148 };
|
|
149
|
|
150
|
216
|
151 static ngx_http_module_t ngx_http_log_module_ctx = {
|
260
|
152 NULL, /* preconfiguration */
|
122
|
153 ngx_http_log_init, /* postconfiguration */
|
118
|
154
|
|
155 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
|
|
166 ngx_module_t ngx_http_log_module = {
|
|
167 NGX_MODULE_V1,
|
|
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 */
|
122
|
172 NULL, /* init module */
|
118
|
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
|
|
179 };
|
|
180
|
|
181
|
278
|
182 static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
|
122
|
183
|
|
184
|
|
185 static ngx_str_t ngx_http_combined_fmt =
|
130
|
186 ngx_string("$remote_addr - $remote_user [$time_local] "
|
126
|
187 "\"$request\" $status $body_bytes_sent "
|
122
|
188 "\"$http_referer\" \"$http_user_agent\"");
|
118
|
189
|
|
190
|
122
|
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 },
|
130
|
194 { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
|
122
|
195 ngx_http_log_time },
|
|
196 { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
|
298
|
197 { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
|
|
198 ngx_http_log_request_time },
|
122
|
199 { ngx_string("status"), 3, ngx_http_log_status },
|
|
200 { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
|
126
|
201 { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
|
|
202 ngx_http_log_body_bytes_sent },
|
122
|
203 { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
|
126
|
204 ngx_http_log_body_bytes_sent },
|
122
|
205 { ngx_string("request_length"), NGX_SIZE_T_LEN,
|
|
206 ngx_http_log_request_length },
|
|
207
|
|
208 { ngx_null_string, 0, NULL }
|
|
209 };
|
118
|
210
|
|
211
|
|
212 ngx_int_t
|
|
213 ngx_http_log_handler(ngx_http_request_t *r)
|
|
214 {
|
|
215 u_char *line, *p;
|
|
216 size_t len;
|
278
|
217 ngx_uint_t i, l;
|
118
|
218 ngx_http_log_t *log;
|
120
|
219 ngx_open_file_t *file;
|
118
|
220 ngx_http_log_op_t *op;
|
|
221 ngx_http_log_loc_conf_t *lcf;
|
|
222
|
|
223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
224 "http log handler");
|
|
225
|
|
226 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
227
|
|
228 if (lcf->off) {
|
|
229 return NGX_OK;
|
|
230 }
|
|
231
|
|
232 log = lcf->logs->elts;
|
|
233 for (l = 0; l < lcf->logs->nelts; l++) {
|
|
234
|
182
|
235 if (ngx_time() == log[l].disk_full_time) {
|
|
236
|
|
237 /*
|
278
|
238 * on FreeBSD writing to a full filesystem with enabled softupdates
|
182
|
239 * may block process for much longer time than writing to non-full
|
278
|
240 * filesystem, so we skip writing to a log for one second
|
182
|
241 */
|
|
242
|
|
243 continue;
|
|
244 }
|
|
245
|
118
|
246 len = 0;
|
|
247 op = log[l].ops->elts;
|
|
248 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
249 if (op[i].len == 0) {
|
|
250 len += op[i].getlen(r, op[i].data);
|
|
251
|
|
252 } else {
|
|
253 len += op[i].len;
|
|
254 }
|
|
255 }
|
|
256
|
120
|
257 len += NGX_LINEFEED_SIZE;
|
|
258
|
|
259 file = log[l].file;
|
|
260
|
384
|
261 if (file && file->buffer) {
|
120
|
262
|
|
263 if (len > (size_t) (file->last - file->pos)) {
|
|
264
|
278
|
265 ngx_http_log_write(r, &log[l], file->buffer,
|
|
266 file->pos - file->buffer);
|
120
|
267
|
|
268 file->pos = file->buffer;
|
|
269 }
|
|
270
|
|
271 if (len <= (size_t) (file->last - file->pos)) {
|
|
272
|
|
273 p = file->pos;
|
|
274
|
|
275 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
276 p = op[i].run(r, p, &op[i]);
|
|
277 }
|
|
278
|
|
279 ngx_linefeed(p);
|
|
280
|
|
281 file->pos = p;
|
|
282
|
|
283 continue;
|
|
284 }
|
|
285 }
|
118
|
286
|
382
|
287 line = ngx_pnalloc(r->pool, len);
|
118
|
288 if (line == NULL) {
|
|
289 return NGX_ERROR;
|
|
290 }
|
|
291
|
|
292 p = line;
|
|
293
|
|
294 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
295 p = op[i].run(r, p, &op[i]);
|
|
296 }
|
|
297
|
120
|
298 ngx_linefeed(p);
|
|
299
|
278
|
300 ngx_http_log_write(r, &log[l], line, p - line);
|
118
|
301 }
|
|
302
|
|
303 return NGX_OK;
|
|
304 }
|
|
305
|
|
306
|
278
|
307 static void
|
|
308 ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
|
|
309 size_t len)
|
|
310 {
|
384
|
311 u_char *name;
|
|
312 time_t now;
|
|
313 ssize_t n;
|
|
314 ngx_err_t err;
|
278
|
315
|
384
|
316 if (log->script == NULL) {
|
|
317 name = log->file->name.data;
|
|
318 n = ngx_write_fd(log->file->fd, buf, len);
|
|
319
|
|
320 } else {
|
|
321 name = NULL;
|
|
322 n = ngx_http_log_script_write(r, log->script, &name, buf, len);
|
|
323 }
|
278
|
324
|
|
325 if (n == (ssize_t) len) {
|
|
326 return;
|
|
327 }
|
|
328
|
|
329 now = ngx_time();
|
|
330
|
|
331 if (n == -1) {
|
|
332 err = ngx_errno;
|
|
333
|
|
334 if (err == NGX_ENOSPC) {
|
|
335 log->disk_full_time = now;
|
|
336 }
|
|
337
|
296
|
338 if (now - log->error_log_time > 59) {
|
278
|
339 ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
|
384
|
340 ngx_write_fd_n " to \"%s\" failed", name);
|
278
|
341
|
|
342 log->error_log_time = now;
|
|
343 }
|
|
344
|
|
345 return;
|
|
346 }
|
|
347
|
296
|
348 if (now - log->error_log_time > 59) {
|
278
|
349 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
384
|
350 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
|
|
351 name, n, len);
|
278
|
352
|
|
353 log->error_log_time = now;
|
|
354 }
|
|
355 }
|
|
356
|
|
357
|
384
|
358 static ssize_t
|
|
359 ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
|
|
360 u_char **name, u_char *buf, size_t len)
|
|
361 {
|
|
362 size_t root;
|
|
363 ssize_t n;
|
|
364 ngx_str_t log, path;
|
|
365 ngx_open_file_info_t of;
|
|
366 ngx_http_log_loc_conf_t *llcf;
|
|
367 ngx_http_core_loc_conf_t *clcf;
|
|
368
|
|
369 if (r->err_status == NGX_HTTP_NOT_FOUND) {
|
|
370
|
|
371 /* test root directory existance */
|
|
372
|
|
373 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
|
|
374 /* simulate successfull logging */
|
|
375 return len;
|
|
376 }
|
|
377
|
|
378 path.data[root] = '\0';
|
|
379
|
|
380 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
381
|
|
382 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
383
|
|
384 of.valid = clcf->open_file_cache_valid;
|
|
385 of.min_uses = clcf->open_file_cache_min_uses;
|
|
386 of.errors = clcf->open_file_cache_errors;
|
|
387 of.events = clcf->open_file_cache_events;
|
|
388
|
|
389 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
390 != NGX_OK
|
|
391 || !of.is_dir)
|
|
392 {
|
|
393 /* no root directory: simulate successfull logging */
|
|
394 return len;
|
|
395 }
|
|
396 }
|
|
397
|
|
398 if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
|
|
399 script->values->elts)
|
|
400 == NULL)
|
|
401 {
|
|
402 /* simulate successfull logging */
|
|
403 return len;
|
|
404 }
|
|
405
|
|
406 log.data[log.len - 1] = '\0';
|
|
407 *name = log.data;
|
|
408
|
|
409 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
410 "http log \"%s\"", log.data);
|
|
411
|
|
412 llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
413
|
|
414 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
415
|
|
416 of.log = 1;
|
|
417 of.valid = llcf->open_file_cache_valid;
|
|
418 of.min_uses = llcf->open_file_cache_min_uses;
|
|
419
|
|
420 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
|
|
421 != NGX_OK)
|
|
422 {
|
|
423 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
424 ngx_open_file_n " \"%s\" failed", log.data);
|
|
425 return -1;
|
|
426 }
|
|
427
|
|
428 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
429 "http log #%d", of.fd);
|
|
430
|
|
431 n = ngx_write_fd(of.fd, buf, len);
|
|
432
|
|
433 return n;
|
|
434 }
|
|
435
|
|
436
|
118
|
437 static u_char *
|
|
438 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
|
|
439 ngx_http_log_op_t *op)
|
|
440 {
|
|
441 size_t len;
|
|
442 uintptr_t data;
|
|
443
|
|
444 len = op->len;
|
|
445 data = op->data;
|
|
446
|
|
447 while (len--) {
|
|
448 *buf++ = (u_char) (data & 0xff);
|
|
449 data >>= 8;
|
|
450 }
|
|
451
|
|
452 return buf;
|
|
453 }
|
|
454
|
|
455
|
|
456 static u_char *
|
|
457 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
|
|
458 ngx_http_log_op_t *op)
|
|
459 {
|
|
460 return ngx_cpymem(buf, (u_char *) op->data, op->len);
|
|
461 }
|
|
462
|
|
463
|
|
464 static u_char *
|
|
465 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
466 ngx_http_log_op_t *op)
|
|
467 {
|
|
468 return ngx_sprintf(buf, "%ui", r->connection->number);
|
|
469 }
|
|
470
|
|
471
|
|
472 static u_char *
|
|
473 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
474 {
|
|
475 if (r->pipeline) {
|
|
476 *buf = 'p';
|
|
477 } else {
|
|
478 *buf = '.';
|
|
479 }
|
|
480
|
|
481 return buf + 1;
|
|
482 }
|
|
483
|
|
484
|
|
485 static u_char *
|
|
486 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
487 {
|
|
488 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
489 ngx_cached_http_log_time.len);
|
|
490 }
|
|
491
|
|
492
|
|
493 static u_char *
|
|
494 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
495 {
|
|
496 ngx_time_t *tp;
|
|
497
|
|
498 tp = ngx_timeofday();
|
|
499
|
|
500 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
|
|
501 }
|
|
502
|
|
503
|
|
504 static u_char *
|
|
505 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
506 ngx_http_log_op_t *op)
|
|
507 {
|
298
|
508 ngx_time_t *tp;
|
|
509 ngx_msec_int_t ms;
|
|
510
|
|
511 tp = ngx_timeofday();
|
118
|
512
|
346
|
513 ms = (ngx_msec_int_t)
|
|
514 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
|
298
|
515 ms = (ms >= 0) ? ms : 0;
|
118
|
516
|
298
|
517 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
|
118
|
518 }
|
|
519
|
|
520
|
|
521 static u_char *
|
|
522 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
523 {
|
|
524 return ngx_sprintf(buf, "%ui",
|
|
525 r->err_status ? r->err_status : r->headers_out.status);
|
|
526 }
|
|
527
|
|
528
|
|
529 static u_char *
|
122
|
530 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
|
531 ngx_http_log_op_t *op)
|
118
|
532 {
|
|
533 return ngx_sprintf(buf, "%O", r->connection->sent);
|
|
534 }
|
|
535
|
|
536
|
316
|
537 /*
|
|
538 * although there is a real $body_bytes_sent variable,
|
|
539 * this log operation code function is more optimized for logging
|
|
540 */
|
|
541
|
118
|
542 static u_char *
|
126
|
543 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
544 ngx_http_log_op_t *op)
|
|
545 {
|
|
546 off_t length;
|
|
547
|
|
548 length = r->connection->sent - r->header_size;
|
|
549
|
|
550 if (length > 0) {
|
|
551 return ngx_sprintf(buf, "%O", length);
|
|
552 }
|
|
553
|
|
554 *buf = '0';
|
|
555
|
|
556 return buf + 1;
|
|
557 }
|
|
558
|
|
559
|
|
560 static u_char *
|
|
561 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
562 ngx_http_log_op_t *op)
|
|
563 {
|
212
|
564 return ngx_sprintf(buf, "%O", r->request_length);
|
118
|
565 }
|
|
566
|
|
567
|
|
568 static ngx_int_t
|
|
569 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
|
|
570 ngx_str_t *value)
|
|
571 {
|
|
572 ngx_int_t index;
|
|
573
|
|
574 index = ngx_http_get_variable_index(cf, value);
|
|
575 if (index == NGX_ERROR) {
|
|
576 return NGX_ERROR;
|
|
577 }
|
|
578
|
|
579 op->len = 0;
|
|
580 op->getlen = ngx_http_log_variable_getlen;
|
|
581 op->run = ngx_http_log_variable;
|
|
582 op->data = index;
|
|
583
|
|
584 return NGX_OK;
|
|
585 }
|
|
586
|
|
587
|
|
588 static size_t
|
|
589 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
|
|
590 {
|
376
|
591 uintptr_t len;
|
118
|
592 ngx_http_variable_value_t *value;
|
|
593
|
|
594 value = ngx_http_get_indexed_variable(r, data);
|
|
595
|
122
|
596 if (value == NULL || value->not_found) {
|
118
|
597 return 1;
|
|
598 }
|
|
599
|
376
|
600 len = ngx_http_log_escape(NULL, value->data, value->len);
|
|
601
|
|
602 value->escape = len ? 1 : 0;
|
|
603
|
|
604 return value->len + len * 3;
|
118
|
605 }
|
|
606
|
|
607
|
|
608 static u_char *
|
|
609 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
610 {
|
|
611 ngx_http_variable_value_t *value;
|
|
612
|
|
613 value = ngx_http_get_indexed_variable(r, op->data);
|
|
614
|
122
|
615 if (value == NULL || value->not_found) {
|
118
|
616 *buf = '-';
|
|
617 return buf + 1;
|
|
618 }
|
|
619
|
376
|
620 if (value->escape == 0) {
|
|
621 return ngx_cpymem(buf, value->data, value->len);
|
|
622
|
|
623 } else {
|
|
624 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
|
|
625 }
|
|
626 }
|
|
627
|
|
628
|
|
629 static uintptr_t
|
|
630 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
|
|
631 {
|
|
632 ngx_uint_t i, n;
|
|
633 static u_char hex[] = "0123456789ABCDEF";
|
|
634
|
|
635 static uint32_t escape[] = {
|
|
636 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
637
|
|
638 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
639 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
|
|
640
|
|
641 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
642 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
|
|
643
|
|
644 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
645 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
646
|
|
647 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
648 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
649 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
650 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
651 };
|
|
652
|
|
653
|
|
654 if (dst == NULL) {
|
|
655
|
|
656 /* find the number of the characters to be escaped */
|
|
657
|
|
658 n = 0;
|
|
659
|
|
660 for (i = 0; i < size; i++) {
|
|
661 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
662 n++;
|
|
663 }
|
|
664 src++;
|
|
665 }
|
|
666
|
|
667 return (uintptr_t) n;
|
|
668 }
|
|
669
|
|
670 for (i = 0; i < size; i++) {
|
|
671 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
672 *dst++ = '\\';
|
|
673 *dst++ = 'x';
|
|
674 *dst++ = hex[*src >> 4];
|
|
675 *dst++ = hex[*src & 0xf];
|
|
676 src++;
|
|
677
|
|
678 } else {
|
|
679 *dst++ = *src++;
|
|
680 }
|
|
681 }
|
|
682
|
|
683 return (uintptr_t) dst;
|
118
|
684 }
|
|
685
|
|
686
|
|
687 static void *
|
|
688 ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
689 {
|
|
690 ngx_http_log_main_conf_t *conf;
|
|
691
|
122
|
692 ngx_http_log_fmt_t *fmt;
|
118
|
693
|
|
694 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
|
|
695 if (conf == NULL) {
|
|
696 return NGX_CONF_ERROR;
|
|
697 }
|
|
698
|
|
699 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
|
122
|
700 != NGX_OK)
|
118
|
701 {
|
|
702 return NGX_CONF_ERROR;
|
|
703 }
|
|
704
|
122
|
705 fmt = ngx_array_push(&conf->formats);
|
|
706 if (fmt == NULL) {
|
118
|
707 return NGX_CONF_ERROR;
|
|
708 }
|
|
709
|
122
|
710 fmt->name.len = sizeof("combined") - 1;
|
|
711 fmt->name.data = (u_char *) "combined";
|
118
|
712
|
122
|
713 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
|
714 if (fmt->ops == NULL) {
|
118
|
715 return NGX_CONF_ERROR;
|
|
716 }
|
|
717
|
|
718 return conf;
|
|
719 }
|
|
720
|
|
721
|
|
722 static void *
|
|
723 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
724 {
|
|
725 ngx_http_log_loc_conf_t *conf;
|
|
726
|
|
727 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
|
|
728 if (conf == NULL) {
|
|
729 return NGX_CONF_ERROR;
|
|
730 }
|
|
731
|
384
|
732 conf->open_file_cache = NGX_CONF_UNSET_PTR;
|
|
733
|
118
|
734 return conf;
|
|
735 }
|
|
736
|
|
737
|
|
738 static char *
|
|
739 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
740 {
|
|
741 ngx_http_log_loc_conf_t *prev = parent;
|
|
742 ngx_http_log_loc_conf_t *conf = child;
|
|
743
|
|
744 ngx_http_log_t *log;
|
|
745 ngx_http_log_fmt_t *fmt;
|
|
746 ngx_http_log_main_conf_t *lmcf;
|
|
747
|
384
|
748 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
749
|
|
750 conf->open_file_cache = prev->open_file_cache;
|
|
751 conf->open_file_cache_valid = prev->open_file_cache_valid;
|
|
752 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
|
|
753
|
|
754 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
755 conf->open_file_cache = NULL;
|
|
756 }
|
|
757 }
|
|
758
|
278
|
759 if (conf->logs || conf->off) {
|
|
760 return NGX_CONF_OK;
|
|
761 }
|
118
|
762
|
278
|
763 *conf = *prev;
|
118
|
764
|
278
|
765 if (conf->logs || conf->off) {
|
|
766 return NGX_CONF_OK;
|
|
767 }
|
118
|
768
|
278
|
769 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
770 if (conf->logs == NULL) {
|
|
771 return NGX_CONF_ERROR;
|
|
772 }
|
118
|
773
|
278
|
774 log = ngx_array_push(conf->logs);
|
|
775 if (log == NULL) {
|
|
776 return NGX_CONF_ERROR;
|
|
777 }
|
118
|
778
|
278
|
779 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
|
|
780 if (log->file == NULL) {
|
|
781 return NGX_CONF_ERROR;
|
|
782 }
|
118
|
783
|
278
|
784 log->disk_full_time = 0;
|
|
785 log->error_log_time = 0;
|
118
|
786
|
278
|
787 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
788 fmt = lmcf->formats.elts;
|
|
789
|
|
790 /* the default "combined" format */
|
|
791 log->ops = fmt[0].ops;
|
|
792 lmcf->combined_used = 1;
|
118
|
793
|
|
794 return NGX_CONF_OK;
|
|
795 }
|
|
796
|
|
797
|
|
798 static char *
|
|
799 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
800 {
|
|
801 ngx_http_log_loc_conf_t *llcf = conf;
|
|
802
|
384
|
803 ssize_t buf;
|
|
804 ngx_uint_t i, n;
|
|
805 ngx_str_t *value, name;
|
|
806 ngx_http_log_t *log;
|
|
807 ngx_http_log_fmt_t *fmt;
|
|
808 ngx_http_log_main_conf_t *lmcf;
|
|
809 ngx_http_script_compile_t sc;
|
118
|
810
|
|
811 value = cf->args->elts;
|
|
812
|
|
813 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
814 llcf->off = 1;
|
|
815 return NGX_CONF_OK;
|
|
816 }
|
|
817
|
|
818 if (llcf->logs == NULL) {
|
|
819 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
820 if (llcf->logs == NULL) {
|
|
821 return NGX_CONF_ERROR;
|
|
822 }
|
|
823 }
|
|
824
|
|
825 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
826
|
|
827 log = ngx_array_push(llcf->logs);
|
|
828 if (log == NULL) {
|
|
829 return NGX_CONF_ERROR;
|
|
830 }
|
|
831
|
384
|
832 ngx_memzero(log, sizeof(ngx_http_log_t));
|
|
833
|
|
834 n = ngx_http_script_variables_count(&value[1]);
|
|
835
|
|
836 if (n == 0) {
|
|
837 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
|
|
838 if (log->file == NULL) {
|
|
839 return NGX_CONF_ERROR;
|
|
840 }
|
|
841
|
|
842 } else {
|
|
843 if (ngx_conf_full_name(cf->cycle, &value[1], 0) == NGX_ERROR) {
|
|
844 return NGX_CONF_ERROR;
|
|
845 }
|
|
846
|
|
847 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
|
|
848 if (log->script == NULL) {
|
|
849 return NGX_CONF_ERROR;
|
|
850 }
|
|
851
|
|
852 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
853
|
|
854 sc.cf = cf;
|
|
855 sc.source = &value[1];
|
|
856 sc.lengths = &log->script->lengths;
|
|
857 sc.values = &log->script->values;
|
|
858 sc.variables = n;
|
|
859 sc.complete_lengths = 1;
|
|
860 sc.complete_values = 1;
|
|
861
|
|
862 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
863 return NGX_CONF_ERROR;
|
|
864 }
|
118
|
865 }
|
|
866
|
120
|
867 if (cf->args->nelts >= 3) {
|
118
|
868 name = value[2];
|
130
|
869
|
|
870 if (ngx_strcmp(name.data, "combined") == 0) {
|
|
871 lmcf->combined_used = 1;
|
|
872 }
|
|
873
|
118
|
874 } else {
|
|
875 name.len = sizeof("combined") - 1;
|
|
876 name.data = (u_char *) "combined";
|
122
|
877 lmcf->combined_used = 1;
|
118
|
878 }
|
|
879
|
|
880 fmt = lmcf->formats.elts;
|
|
881 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
882 if (fmt[i].name.len == name.len
|
|
883 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
884 {
|
|
885 log->ops = fmt[i].ops;
|
120
|
886 goto buffer;
|
118
|
887 }
|
|
888 }
|
|
889
|
|
890 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
891 "unknown log format \"%V\"", &name);
|
120
|
892 return NGX_CONF_ERROR;
|
118
|
893
|
120
|
894 buffer:
|
|
895
|
|
896 if (cf->args->nelts == 4) {
|
|
897 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
|
|
898 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
899 "invalid parameter \"%V\"", &value[3]);
|
|
900 return NGX_CONF_ERROR;
|
|
901 }
|
|
902
|
384
|
903 if (log->script) {
|
|
904 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
905 "buffered logs can not have variables in name");
|
|
906 return NGX_CONF_ERROR;
|
|
907 }
|
|
908
|
120
|
909 name.len = value[3].len - 7;
|
|
910 name.data = value[3].data + 7;
|
|
911
|
|
912 buf = ngx_parse_size(&name);
|
|
913
|
|
914 if (buf == NGX_ERROR) {
|
|
915 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
916 "invalid parameter \"%V\"", &value[3]);
|
|
917 return NGX_CONF_ERROR;
|
|
918 }
|
|
919
|
|
920 if (log->file->buffer && log->file->last - log->file->pos != buf) {
|
|
921 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
922 "access_log \"%V\" already defined "
|
|
923 "with different buffer size", &value[1]);
|
|
924 return NGX_CONF_ERROR;
|
|
925 }
|
|
926
|
|
927 log->file->buffer = ngx_palloc(cf->pool, buf);
|
|
928 if (log->file->buffer == NULL) {
|
|
929 return NGX_CONF_ERROR;
|
|
930 }
|
|
931
|
|
932 log->file->pos = log->file->buffer;
|
|
933 log->file->last = log->file->buffer + buf;
|
|
934 }
|
|
935
|
|
936 return NGX_CONF_OK;
|
118
|
937 }
|
|
938
|
|
939
|
|
940 static char *
|
|
941 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
942 {
|
|
943 ngx_http_log_main_conf_t *lmcf = conf;
|
|
944
|
122
|
945 ngx_str_t *value;
|
|
946 ngx_uint_t i;
|
|
947 ngx_http_log_fmt_t *fmt;
|
118
|
948
|
|
949 value = cf->args->elts;
|
|
950
|
|
951 fmt = lmcf->formats.elts;
|
122
|
952 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
953 if (fmt[i].name.len == value[1].len
|
|
954 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
|
118
|
955 {
|
380
|
956 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
957 "duplicate \"log_format\" name \"%V\"",
|
|
958 &value[1]);
|
|
959 return NGX_CONF_ERROR;
|
118
|
960 }
|
|
961 }
|
|
962
|
|
963 fmt = ngx_array_push(&lmcf->formats);
|
|
964 if (fmt == NULL) {
|
|
965 return NGX_CONF_ERROR;
|
|
966 }
|
|
967
|
|
968 fmt->name = value[1];
|
|
969
|
122
|
970 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
118
|
971 if (fmt->ops == NULL) {
|
|
972 return NGX_CONF_ERROR;
|
|
973 }
|
|
974
|
122
|
975 return ngx_http_log_compile_format(cf, fmt->ops, cf->args, 2);
|
|
976 }
|
|
977
|
118
|
978
|
122
|
979 static char *
|
|
980 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,
|
|
981 ngx_array_t *args, ngx_uint_t s)
|
|
982 {
|
260
|
983 u_char *data, *p, ch;
|
|
984 size_t i, len;
|
|
985 ngx_str_t *value, var;
|
|
986 ngx_uint_t bracket;
|
|
987 ngx_http_log_op_t *op;
|
|
988 ngx_http_log_var_t *v;
|
122
|
989
|
|
990 value = args->elts;
|
|
991
|
|
992 for ( /* void */ ; s < args->nelts; s++) {
|
118
|
993
|
260
|
994 for (i = 0; i < value[s].len; i++) {
|
|
995 if (value[s].data[i] != '%') {
|
|
996 continue;
|
|
997 }
|
|
998
|
|
999 ch = value[s].data[i + 1];
|
|
1000
|
|
1001 if ((ch >= 'A' && ch <= 'Z')
|
|
1002 || (ch >= 'a' && ch <= 'z')
|
|
1003 || ch == '{')
|
|
1004 {
|
|
1005 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1006 "the parameters in the \"%%name\" form are not supported, "
|
|
1007 "use the \"$variable\" instead");
|
|
1008
|
|
1009 return NGX_CONF_ERROR;
|
|
1010 }
|
|
1011 }
|
|
1012
|
118
|
1013 i = 0;
|
|
1014
|
|
1015 while (i < value[s].len) {
|
|
1016
|
122
|
1017 op = ngx_array_push(ops);
|
118
|
1018 if (op == NULL) {
|
|
1019 return NGX_CONF_ERROR;
|
|
1020 }
|
|
1021
|
|
1022 data = &value[s].data[i];
|
|
1023
|
260
|
1024 if (value[s].data[i] == '$') {
|
118
|
1025
|
|
1026 if (++i == value[s].len) {
|
|
1027 goto invalid;
|
|
1028 }
|
|
1029
|
|
1030 if (value[s].data[i] == '{') {
|
|
1031 bracket = 1;
|
|
1032
|
|
1033 if (++i == value[s].len) {
|
|
1034 goto invalid;
|
|
1035 }
|
|
1036
|
|
1037 var.data = &value[s].data[i];
|
|
1038
|
|
1039 } else {
|
|
1040 bracket = 0;
|
|
1041 var.data = &value[s].data[i];
|
|
1042 }
|
|
1043
|
|
1044 for (var.len = 0; i < value[s].len; i++, var.len++) {
|
|
1045 ch = value[s].data[i];
|
|
1046
|
|
1047 if (ch == '}' && bracket) {
|
|
1048 i++;
|
|
1049 bracket = 0;
|
|
1050 break;
|
|
1051 }
|
|
1052
|
|
1053 if ((ch >= 'A' && ch <= 'Z')
|
|
1054 || (ch >= 'a' && ch <= 'z')
|
|
1055 || (ch >= '0' && ch <= '9')
|
|
1056 || ch == '_')
|
|
1057 {
|
|
1058 continue;
|
|
1059 }
|
|
1060
|
|
1061 break;
|
|
1062 }
|
|
1063
|
|
1064 if (bracket) {
|
|
1065 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1066 "the closing bracket in \"%V\" "
|
|
1067 "variable is missing", &var);
|
|
1068 return NGX_CONF_ERROR;
|
|
1069 }
|
|
1070
|
|
1071 if (var.len == 0) {
|
|
1072 goto invalid;
|
|
1073 }
|
|
1074
|
126
|
1075 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
|
|
1076 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1077 "use \"$body_bytes_sent\" instead of "
|
|
1078 "\"$apache_bytes_sent\"");
|
|
1079 }
|
|
1080
|
122
|
1081 for (v = ngx_http_log_vars; v->name.len; v++) {
|
|
1082
|
|
1083 if (v->name.len == var.len
|
|
1084 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
|
|
1085 {
|
|
1086 op->len = v->len;
|
|
1087 op->getlen = NULL;
|
|
1088 op->run = v->run;
|
|
1089 op->data = 0;
|
|
1090
|
|
1091 goto found;
|
|
1092 }
|
|
1093 }
|
|
1094
|
118
|
1095 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
|
|
1096 return NGX_CONF_ERROR;
|
|
1097 }
|
|
1098
|
122
|
1099 found:
|
|
1100
|
|
1101 continue;
|
|
1102 }
|
|
1103
|
|
1104 i++;
|
118
|
1105
|
260
|
1106 while (i < value[s].len && value[s].data[i] != '$') {
|
122
|
1107 i++;
|
|
1108 }
|
118
|
1109
|
122
|
1110 len = &value[s].data[i] - data;
|
118
|
1111
|
122
|
1112 if (len) {
|
118
|
1113
|
122
|
1114 op->len = len;
|
|
1115 op->getlen = NULL;
|
118
|
1116
|
122
|
1117 if (len <= sizeof(uintptr_t)) {
|
|
1118 op->run = ngx_http_log_copy_short;
|
|
1119 op->data = 0;
|
118
|
1120
|
122
|
1121 while (len--) {
|
|
1122 op->data <<= 8;
|
|
1123 op->data |= data[len];
|
|
1124 }
|
|
1125
|
|
1126 } else {
|
|
1127 op->run = ngx_http_log_copy_long;
|
118
|
1128
|
382
|
1129 p = ngx_pnalloc(cf->pool, len);
|
122
|
1130 if (p == NULL) {
|
|
1131 return NGX_CONF_ERROR;
|
|
1132 }
|
118
|
1133
|
122
|
1134 ngx_memcpy(p, data, len);
|
|
1135 op->data = (uintptr_t) p;
|
118
|
1136 }
|
|
1137 }
|
|
1138 }
|
|
1139 }
|
|
1140
|
|
1141 return NGX_CONF_OK;
|
|
1142
|
|
1143 invalid:
|
|
1144
|
|
1145 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
|
|
1146
|
|
1147 return NGX_CONF_ERROR;
|
|
1148 }
|
|
1149
|
|
1150
|
384
|
1151 static char *
|
|
1152 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1153 {
|
|
1154 ngx_http_log_loc_conf_t *llcf = conf;
|
|
1155
|
|
1156 time_t inactive, valid;
|
|
1157 ngx_str_t *value, s;
|
|
1158 ngx_int_t max, min_uses;
|
|
1159 ngx_uint_t i;
|
|
1160
|
|
1161 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
|
|
1162 return "is duplicate";
|
|
1163 }
|
|
1164
|
|
1165 value = cf->args->elts;
|
|
1166
|
|
1167 max = 0;
|
|
1168 inactive = 10;
|
|
1169 valid = 60;
|
|
1170 min_uses = 1;
|
|
1171
|
|
1172 for (i = 1; i < cf->args->nelts; i++) {
|
|
1173
|
|
1174 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
|
|
1175
|
|
1176 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
|
|
1177 if (max == NGX_ERROR) {
|
|
1178 goto failed;
|
|
1179 }
|
|
1180
|
|
1181 continue;
|
|
1182 }
|
|
1183
|
|
1184 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1185
|
|
1186 s.len = value[i].len - 9;
|
|
1187 s.data = value[i].data + 9;
|
|
1188
|
|
1189 inactive = ngx_parse_time(&s, 1);
|
|
1190 if (inactive < 0) {
|
|
1191 goto failed;
|
|
1192 }
|
|
1193
|
|
1194 continue;
|
|
1195 }
|
|
1196
|
|
1197 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
|
|
1198
|
|
1199 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
|
|
1200 if (min_uses == NGX_ERROR) {
|
|
1201 goto failed;
|
|
1202 }
|
|
1203
|
|
1204 continue;
|
|
1205 }
|
|
1206
|
|
1207 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
|
|
1208
|
|
1209 s.len = value[i].len - 6;
|
|
1210 s.data = value[i].data + 6;
|
|
1211
|
|
1212 valid = ngx_parse_time(&s, 1);
|
|
1213 if (valid < 0) {
|
|
1214 goto failed;
|
|
1215 }
|
|
1216
|
|
1217 continue;
|
|
1218 }
|
|
1219
|
|
1220 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1221
|
|
1222 llcf->open_file_cache = NULL;
|
|
1223
|
|
1224 continue;
|
|
1225 }
|
|
1226
|
|
1227 failed:
|
|
1228
|
|
1229 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1230 "invalid \"open_log_file_cache\" parameter \"%V\"",
|
|
1231 &value[i]);
|
|
1232 return NGX_CONF_ERROR;
|
|
1233 }
|
|
1234
|
|
1235 if (llcf->open_file_cache == NULL) {
|
|
1236 return NGX_CONF_OK;
|
|
1237 }
|
|
1238
|
|
1239 if (max == 0) {
|
|
1240 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1241 "\"open_log_file_cache\" must have \"max\" parameter");
|
|
1242 return NGX_CONF_ERROR;
|
|
1243 }
|
|
1244
|
|
1245 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
|
|
1246
|
|
1247 if (llcf->open_file_cache) {
|
|
1248
|
|
1249 llcf->open_file_cache_valid = valid;
|
|
1250 llcf->open_file_cache_min_uses = min_uses;
|
|
1251
|
|
1252 return NGX_CONF_OK;
|
|
1253 }
|
|
1254
|
|
1255 return NGX_CONF_ERROR;
|
|
1256 }
|
|
1257
|
|
1258
|
118
|
1259 static ngx_int_t
|
122
|
1260 ngx_http_log_init(ngx_conf_t *cf)
|
118
|
1261 {
|
122
|
1262 ngx_str_t *value;
|
|
1263 ngx_array_t a;
|
130
|
1264 ngx_http_handler_pt *h;
|
122
|
1265 ngx_http_log_fmt_t *fmt;
|
|
1266 ngx_http_log_main_conf_t *lmcf;
|
118
|
1267 ngx_http_core_main_conf_t *cmcf;
|
|
1268
|
122
|
1269 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
1270
|
|
1271 if (lmcf->combined_used) {
|
|
1272 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
|
|
1273 return NGX_ERROR;
|
|
1274 }
|
|
1275
|
|
1276 value = ngx_array_push(&a);
|
|
1277 if (value == NULL) {
|
|
1278 return NGX_ERROR;
|
|
1279 }
|
|
1280
|
|
1281 *value = ngx_http_combined_fmt;
|
|
1282 fmt = lmcf->formats.elts;
|
|
1283
|
|
1284 if (ngx_http_log_compile_format(cf, fmt->ops, &a, 0)
|
|
1285 != NGX_CONF_OK)
|
|
1286 {
|
|
1287 return NGX_ERROR;
|
|
1288 }
|
|
1289 }
|
|
1290
|
|
1291 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
118
|
1292
|
130
|
1293 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
|
|
1294 if (h == NULL) {
|
|
1295 return NGX_ERROR;
|
|
1296 }
|
|
1297
|
|
1298 *h = ngx_http_log_handler;
|
118
|
1299
|
|
1300 return NGX_OK;
|
|
1301 }
|