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