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;
|
410
|
433 of.directio = NGX_MAX_OFF_T_VALUE;
|
384
|
434
|
|
435 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
|
|
436 != NGX_OK)
|
|
437 {
|
|
438 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
439 ngx_open_file_n " \"%s\" failed", log.data);
|
386
|
440 /* simulate successfull logging */
|
|
441 return len;
|
384
|
442 }
|
|
443
|
|
444 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
445 "http log #%d", of.fd);
|
|
446
|
|
447 n = ngx_write_fd(of.fd, buf, len);
|
|
448
|
|
449 return n;
|
|
450 }
|
|
451
|
|
452
|
118
|
453 static u_char *
|
|
454 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
|
|
455 ngx_http_log_op_t *op)
|
|
456 {
|
|
457 size_t len;
|
|
458 uintptr_t data;
|
|
459
|
|
460 len = op->len;
|
|
461 data = op->data;
|
|
462
|
|
463 while (len--) {
|
|
464 *buf++ = (u_char) (data & 0xff);
|
|
465 data >>= 8;
|
|
466 }
|
|
467
|
|
468 return buf;
|
|
469 }
|
|
470
|
|
471
|
|
472 static u_char *
|
|
473 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
|
|
474 ngx_http_log_op_t *op)
|
|
475 {
|
|
476 return ngx_cpymem(buf, (u_char *) op->data, op->len);
|
|
477 }
|
|
478
|
|
479
|
|
480 static u_char *
|
|
481 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
482 ngx_http_log_op_t *op)
|
|
483 {
|
|
484 return ngx_sprintf(buf, "%ui", r->connection->number);
|
|
485 }
|
|
486
|
|
487
|
|
488 static u_char *
|
|
489 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
490 {
|
|
491 if (r->pipeline) {
|
|
492 *buf = 'p';
|
|
493 } else {
|
|
494 *buf = '.';
|
|
495 }
|
|
496
|
|
497 return buf + 1;
|
|
498 }
|
|
499
|
|
500
|
|
501 static u_char *
|
|
502 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
503 {
|
|
504 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
505 ngx_cached_http_log_time.len);
|
|
506 }
|
|
507
|
|
508
|
|
509 static u_char *
|
|
510 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
511 {
|
|
512 ngx_time_t *tp;
|
|
513
|
|
514 tp = ngx_timeofday();
|
|
515
|
|
516 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
|
|
517 }
|
|
518
|
|
519
|
|
520 static u_char *
|
|
521 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
522 ngx_http_log_op_t *op)
|
|
523 {
|
298
|
524 ngx_time_t *tp;
|
|
525 ngx_msec_int_t ms;
|
|
526
|
|
527 tp = ngx_timeofday();
|
118
|
528
|
346
|
529 ms = (ngx_msec_int_t)
|
|
530 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
|
298
|
531 ms = (ms >= 0) ? ms : 0;
|
118
|
532
|
298
|
533 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
|
118
|
534 }
|
|
535
|
|
536
|
|
537 static u_char *
|
|
538 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
539 {
|
|
540 return ngx_sprintf(buf, "%ui",
|
|
541 r->err_status ? r->err_status : r->headers_out.status);
|
|
542 }
|
|
543
|
|
544
|
|
545 static u_char *
|
122
|
546 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
|
547 ngx_http_log_op_t *op)
|
118
|
548 {
|
|
549 return ngx_sprintf(buf, "%O", r->connection->sent);
|
|
550 }
|
|
551
|
|
552
|
316
|
553 /*
|
|
554 * although there is a real $body_bytes_sent variable,
|
|
555 * this log operation code function is more optimized for logging
|
|
556 */
|
|
557
|
118
|
558 static u_char *
|
126
|
559 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
560 ngx_http_log_op_t *op)
|
|
561 {
|
|
562 off_t length;
|
|
563
|
|
564 length = r->connection->sent - r->header_size;
|
|
565
|
|
566 if (length > 0) {
|
|
567 return ngx_sprintf(buf, "%O", length);
|
|
568 }
|
|
569
|
|
570 *buf = '0';
|
|
571
|
|
572 return buf + 1;
|
|
573 }
|
|
574
|
|
575
|
|
576 static u_char *
|
|
577 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
578 ngx_http_log_op_t *op)
|
|
579 {
|
212
|
580 return ngx_sprintf(buf, "%O", r->request_length);
|
118
|
581 }
|
|
582
|
|
583
|
|
584 static ngx_int_t
|
|
585 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
|
|
586 ngx_str_t *value)
|
|
587 {
|
|
588 ngx_int_t index;
|
|
589
|
|
590 index = ngx_http_get_variable_index(cf, value);
|
|
591 if (index == NGX_ERROR) {
|
|
592 return NGX_ERROR;
|
|
593 }
|
|
594
|
|
595 op->len = 0;
|
|
596 op->getlen = ngx_http_log_variable_getlen;
|
|
597 op->run = ngx_http_log_variable;
|
|
598 op->data = index;
|
|
599
|
|
600 return NGX_OK;
|
|
601 }
|
|
602
|
|
603
|
|
604 static size_t
|
|
605 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
|
|
606 {
|
376
|
607 uintptr_t len;
|
118
|
608 ngx_http_variable_value_t *value;
|
|
609
|
|
610 value = ngx_http_get_indexed_variable(r, data);
|
|
611
|
122
|
612 if (value == NULL || value->not_found) {
|
118
|
613 return 1;
|
|
614 }
|
|
615
|
376
|
616 len = ngx_http_log_escape(NULL, value->data, value->len);
|
|
617
|
|
618 value->escape = len ? 1 : 0;
|
|
619
|
|
620 return value->len + len * 3;
|
118
|
621 }
|
|
622
|
|
623
|
|
624 static u_char *
|
|
625 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
626 {
|
|
627 ngx_http_variable_value_t *value;
|
|
628
|
|
629 value = ngx_http_get_indexed_variable(r, op->data);
|
|
630
|
122
|
631 if (value == NULL || value->not_found) {
|
118
|
632 *buf = '-';
|
|
633 return buf + 1;
|
|
634 }
|
|
635
|
376
|
636 if (value->escape == 0) {
|
|
637 return ngx_cpymem(buf, value->data, value->len);
|
|
638
|
|
639 } else {
|
|
640 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
|
|
641 }
|
|
642 }
|
|
643
|
|
644
|
|
645 static uintptr_t
|
|
646 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
|
|
647 {
|
|
648 ngx_uint_t i, n;
|
|
649 static u_char hex[] = "0123456789ABCDEF";
|
|
650
|
|
651 static uint32_t escape[] = {
|
|
652 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
653
|
|
654 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
655 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
|
|
656
|
|
657 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
658 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
|
|
659
|
|
660 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
661 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
662
|
|
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 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
667 };
|
|
668
|
|
669
|
|
670 if (dst == NULL) {
|
|
671
|
|
672 /* find the number of the characters to be escaped */
|
|
673
|
|
674 n = 0;
|
|
675
|
|
676 for (i = 0; i < size; i++) {
|
|
677 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
678 n++;
|
|
679 }
|
|
680 src++;
|
|
681 }
|
|
682
|
|
683 return (uintptr_t) n;
|
|
684 }
|
|
685
|
|
686 for (i = 0; i < size; i++) {
|
|
687 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
688 *dst++ = '\\';
|
|
689 *dst++ = 'x';
|
|
690 *dst++ = hex[*src >> 4];
|
|
691 *dst++ = hex[*src & 0xf];
|
|
692 src++;
|
|
693
|
|
694 } else {
|
|
695 *dst++ = *src++;
|
|
696 }
|
|
697 }
|
|
698
|
|
699 return (uintptr_t) dst;
|
118
|
700 }
|
|
701
|
|
702
|
|
703 static void *
|
|
704 ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
705 {
|
|
706 ngx_http_log_main_conf_t *conf;
|
|
707
|
122
|
708 ngx_http_log_fmt_t *fmt;
|
118
|
709
|
|
710 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
|
|
711 if (conf == NULL) {
|
|
712 return NGX_CONF_ERROR;
|
|
713 }
|
|
714
|
|
715 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
|
122
|
716 != NGX_OK)
|
118
|
717 {
|
|
718 return NGX_CONF_ERROR;
|
|
719 }
|
|
720
|
122
|
721 fmt = ngx_array_push(&conf->formats);
|
|
722 if (fmt == NULL) {
|
118
|
723 return NGX_CONF_ERROR;
|
|
724 }
|
|
725
|
122
|
726 fmt->name.len = sizeof("combined") - 1;
|
|
727 fmt->name.data = (u_char *) "combined";
|
118
|
728
|
122
|
729 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
|
730 if (fmt->ops == NULL) {
|
118
|
731 return NGX_CONF_ERROR;
|
|
732 }
|
|
733
|
|
734 return conf;
|
|
735 }
|
|
736
|
|
737
|
|
738 static void *
|
|
739 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
740 {
|
|
741 ngx_http_log_loc_conf_t *conf;
|
|
742
|
|
743 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
|
|
744 if (conf == NULL) {
|
|
745 return NGX_CONF_ERROR;
|
|
746 }
|
|
747
|
384
|
748 conf->open_file_cache = NGX_CONF_UNSET_PTR;
|
|
749
|
118
|
750 return conf;
|
|
751 }
|
|
752
|
|
753
|
|
754 static char *
|
|
755 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
756 {
|
|
757 ngx_http_log_loc_conf_t *prev = parent;
|
|
758 ngx_http_log_loc_conf_t *conf = child;
|
|
759
|
|
760 ngx_http_log_t *log;
|
|
761 ngx_http_log_fmt_t *fmt;
|
|
762 ngx_http_log_main_conf_t *lmcf;
|
|
763
|
384
|
764 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
765
|
|
766 conf->open_file_cache = prev->open_file_cache;
|
|
767 conf->open_file_cache_valid = prev->open_file_cache_valid;
|
|
768 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
|
|
769
|
|
770 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
771 conf->open_file_cache = NULL;
|
|
772 }
|
|
773 }
|
|
774
|
278
|
775 if (conf->logs || conf->off) {
|
|
776 return NGX_CONF_OK;
|
|
777 }
|
118
|
778
|
386
|
779 conf->logs = prev->logs;
|
|
780 conf->off = prev->off;
|
118
|
781
|
278
|
782 if (conf->logs || conf->off) {
|
|
783 return NGX_CONF_OK;
|
|
784 }
|
118
|
785
|
278
|
786 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
787 if (conf->logs == NULL) {
|
|
788 return NGX_CONF_ERROR;
|
|
789 }
|
118
|
790
|
278
|
791 log = ngx_array_push(conf->logs);
|
|
792 if (log == NULL) {
|
|
793 return NGX_CONF_ERROR;
|
|
794 }
|
118
|
795
|
278
|
796 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
|
|
797 if (log->file == NULL) {
|
|
798 return NGX_CONF_ERROR;
|
|
799 }
|
118
|
800
|
386
|
801 log->script = NULL;
|
278
|
802 log->disk_full_time = 0;
|
|
803 log->error_log_time = 0;
|
118
|
804
|
278
|
805 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
806 fmt = lmcf->formats.elts;
|
|
807
|
|
808 /* the default "combined" format */
|
|
809 log->ops = fmt[0].ops;
|
|
810 lmcf->combined_used = 1;
|
118
|
811
|
|
812 return NGX_CONF_OK;
|
|
813 }
|
|
814
|
|
815
|
|
816 static char *
|
|
817 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
818 {
|
|
819 ngx_http_log_loc_conf_t *llcf = conf;
|
|
820
|
384
|
821 ssize_t buf;
|
|
822 ngx_uint_t i, n;
|
|
823 ngx_str_t *value, name;
|
|
824 ngx_http_log_t *log;
|
|
825 ngx_http_log_fmt_t *fmt;
|
|
826 ngx_http_log_main_conf_t *lmcf;
|
|
827 ngx_http_script_compile_t sc;
|
118
|
828
|
|
829 value = cf->args->elts;
|
|
830
|
|
831 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
832 llcf->off = 1;
|
|
833 return NGX_CONF_OK;
|
|
834 }
|
|
835
|
|
836 if (llcf->logs == NULL) {
|
|
837 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
838 if (llcf->logs == NULL) {
|
|
839 return NGX_CONF_ERROR;
|
|
840 }
|
|
841 }
|
|
842
|
|
843 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
844
|
|
845 log = ngx_array_push(llcf->logs);
|
|
846 if (log == NULL) {
|
|
847 return NGX_CONF_ERROR;
|
|
848 }
|
|
849
|
384
|
850 ngx_memzero(log, sizeof(ngx_http_log_t));
|
|
851
|
|
852 n = ngx_http_script_variables_count(&value[1]);
|
|
853
|
|
854 if (n == 0) {
|
|
855 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
|
|
856 if (log->file == NULL) {
|
|
857 return NGX_CONF_ERROR;
|
|
858 }
|
|
859
|
|
860 } else {
|
|
861 if (ngx_conf_full_name(cf->cycle, &value[1], 0) == NGX_ERROR) {
|
|
862 return NGX_CONF_ERROR;
|
|
863 }
|
|
864
|
|
865 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
|
|
866 if (log->script == NULL) {
|
|
867 return NGX_CONF_ERROR;
|
|
868 }
|
|
869
|
|
870 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
871
|
|
872 sc.cf = cf;
|
|
873 sc.source = &value[1];
|
|
874 sc.lengths = &log->script->lengths;
|
|
875 sc.values = &log->script->values;
|
|
876 sc.variables = n;
|
|
877 sc.complete_lengths = 1;
|
|
878 sc.complete_values = 1;
|
|
879
|
|
880 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
881 return NGX_CONF_ERROR;
|
|
882 }
|
118
|
883 }
|
|
884
|
120
|
885 if (cf->args->nelts >= 3) {
|
118
|
886 name = value[2];
|
130
|
887
|
|
888 if (ngx_strcmp(name.data, "combined") == 0) {
|
|
889 lmcf->combined_used = 1;
|
|
890 }
|
|
891
|
118
|
892 } else {
|
|
893 name.len = sizeof("combined") - 1;
|
|
894 name.data = (u_char *) "combined";
|
122
|
895 lmcf->combined_used = 1;
|
118
|
896 }
|
|
897
|
|
898 fmt = lmcf->formats.elts;
|
|
899 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
900 if (fmt[i].name.len == name.len
|
|
901 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
902 {
|
|
903 log->ops = fmt[i].ops;
|
120
|
904 goto buffer;
|
118
|
905 }
|
|
906 }
|
|
907
|
|
908 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
909 "unknown log format \"%V\"", &name);
|
120
|
910 return NGX_CONF_ERROR;
|
118
|
911
|
120
|
912 buffer:
|
|
913
|
|
914 if (cf->args->nelts == 4) {
|
|
915 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
|
|
916 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
917 "invalid parameter \"%V\"", &value[3]);
|
|
918 return NGX_CONF_ERROR;
|
|
919 }
|
|
920
|
384
|
921 if (log->script) {
|
|
922 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
923 "buffered logs can not have variables in name");
|
|
924 return NGX_CONF_ERROR;
|
|
925 }
|
|
926
|
120
|
927 name.len = value[3].len - 7;
|
|
928 name.data = value[3].data + 7;
|
|
929
|
|
930 buf = ngx_parse_size(&name);
|
|
931
|
|
932 if (buf == NGX_ERROR) {
|
|
933 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
934 "invalid parameter \"%V\"", &value[3]);
|
|
935 return NGX_CONF_ERROR;
|
|
936 }
|
|
937
|
|
938 if (log->file->buffer && log->file->last - log->file->pos != buf) {
|
|
939 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
940 "access_log \"%V\" already defined "
|
|
941 "with different buffer size", &value[1]);
|
|
942 return NGX_CONF_ERROR;
|
|
943 }
|
|
944
|
|
945 log->file->buffer = ngx_palloc(cf->pool, buf);
|
|
946 if (log->file->buffer == NULL) {
|
|
947 return NGX_CONF_ERROR;
|
|
948 }
|
|
949
|
|
950 log->file->pos = log->file->buffer;
|
|
951 log->file->last = log->file->buffer + buf;
|
|
952 }
|
|
953
|
|
954 return NGX_CONF_OK;
|
118
|
955 }
|
|
956
|
|
957
|
|
958 static char *
|
|
959 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
960 {
|
|
961 ngx_http_log_main_conf_t *lmcf = conf;
|
|
962
|
122
|
963 ngx_str_t *value;
|
|
964 ngx_uint_t i;
|
|
965 ngx_http_log_fmt_t *fmt;
|
118
|
966
|
|
967 value = cf->args->elts;
|
|
968
|
|
969 fmt = lmcf->formats.elts;
|
122
|
970 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
971 if (fmt[i].name.len == value[1].len
|
|
972 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
|
118
|
973 {
|
380
|
974 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
975 "duplicate \"log_format\" name \"%V\"",
|
|
976 &value[1]);
|
|
977 return NGX_CONF_ERROR;
|
118
|
978 }
|
|
979 }
|
|
980
|
|
981 fmt = ngx_array_push(&lmcf->formats);
|
|
982 if (fmt == NULL) {
|
|
983 return NGX_CONF_ERROR;
|
|
984 }
|
|
985
|
|
986 fmt->name = value[1];
|
|
987
|
122
|
988 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
118
|
989 if (fmt->ops == NULL) {
|
|
990 return NGX_CONF_ERROR;
|
|
991 }
|
|
992
|
122
|
993 return ngx_http_log_compile_format(cf, fmt->ops, cf->args, 2);
|
|
994 }
|
|
995
|
118
|
996
|
122
|
997 static char *
|
|
998 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,
|
|
999 ngx_array_t *args, ngx_uint_t s)
|
|
1000 {
|
260
|
1001 u_char *data, *p, ch;
|
|
1002 size_t i, len;
|
|
1003 ngx_str_t *value, var;
|
|
1004 ngx_uint_t bracket;
|
|
1005 ngx_http_log_op_t *op;
|
|
1006 ngx_http_log_var_t *v;
|
122
|
1007
|
|
1008 value = args->elts;
|
|
1009
|
|
1010 for ( /* void */ ; s < args->nelts; s++) {
|
118
|
1011
|
260
|
1012 for (i = 0; i < value[s].len; i++) {
|
|
1013 if (value[s].data[i] != '%') {
|
|
1014 continue;
|
|
1015 }
|
|
1016
|
|
1017 ch = value[s].data[i + 1];
|
|
1018
|
|
1019 if ((ch >= 'A' && ch <= 'Z')
|
|
1020 || (ch >= 'a' && ch <= 'z')
|
|
1021 || ch == '{')
|
|
1022 {
|
|
1023 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1024 "the parameters in the \"%%name\" form are not supported, "
|
|
1025 "use the \"$variable\" instead");
|
|
1026
|
|
1027 return NGX_CONF_ERROR;
|
|
1028 }
|
|
1029 }
|
|
1030
|
118
|
1031 i = 0;
|
|
1032
|
|
1033 while (i < value[s].len) {
|
|
1034
|
122
|
1035 op = ngx_array_push(ops);
|
118
|
1036 if (op == NULL) {
|
|
1037 return NGX_CONF_ERROR;
|
|
1038 }
|
|
1039
|
|
1040 data = &value[s].data[i];
|
|
1041
|
260
|
1042 if (value[s].data[i] == '$') {
|
118
|
1043
|
|
1044 if (++i == value[s].len) {
|
|
1045 goto invalid;
|
|
1046 }
|
|
1047
|
|
1048 if (value[s].data[i] == '{') {
|
|
1049 bracket = 1;
|
|
1050
|
|
1051 if (++i == value[s].len) {
|
|
1052 goto invalid;
|
|
1053 }
|
|
1054
|
|
1055 var.data = &value[s].data[i];
|
|
1056
|
|
1057 } else {
|
|
1058 bracket = 0;
|
|
1059 var.data = &value[s].data[i];
|
|
1060 }
|
|
1061
|
|
1062 for (var.len = 0; i < value[s].len; i++, var.len++) {
|
|
1063 ch = value[s].data[i];
|
|
1064
|
|
1065 if (ch == '}' && bracket) {
|
|
1066 i++;
|
|
1067 bracket = 0;
|
|
1068 break;
|
|
1069 }
|
|
1070
|
|
1071 if ((ch >= 'A' && ch <= 'Z')
|
|
1072 || (ch >= 'a' && ch <= 'z')
|
|
1073 || (ch >= '0' && ch <= '9')
|
|
1074 || ch == '_')
|
|
1075 {
|
|
1076 continue;
|
|
1077 }
|
|
1078
|
|
1079 break;
|
|
1080 }
|
|
1081
|
|
1082 if (bracket) {
|
|
1083 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1084 "the closing bracket in \"%V\" "
|
|
1085 "variable is missing", &var);
|
|
1086 return NGX_CONF_ERROR;
|
|
1087 }
|
|
1088
|
|
1089 if (var.len == 0) {
|
|
1090 goto invalid;
|
|
1091 }
|
|
1092
|
126
|
1093 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
|
|
1094 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1095 "use \"$body_bytes_sent\" instead of "
|
|
1096 "\"$apache_bytes_sent\"");
|
|
1097 }
|
|
1098
|
122
|
1099 for (v = ngx_http_log_vars; v->name.len; v++) {
|
|
1100
|
|
1101 if (v->name.len == var.len
|
|
1102 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
|
|
1103 {
|
|
1104 op->len = v->len;
|
|
1105 op->getlen = NULL;
|
|
1106 op->run = v->run;
|
|
1107 op->data = 0;
|
|
1108
|
|
1109 goto found;
|
|
1110 }
|
|
1111 }
|
|
1112
|
118
|
1113 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
|
|
1114 return NGX_CONF_ERROR;
|
|
1115 }
|
|
1116
|
122
|
1117 found:
|
|
1118
|
|
1119 continue;
|
|
1120 }
|
|
1121
|
|
1122 i++;
|
118
|
1123
|
260
|
1124 while (i < value[s].len && value[s].data[i] != '$') {
|
122
|
1125 i++;
|
|
1126 }
|
118
|
1127
|
122
|
1128 len = &value[s].data[i] - data;
|
118
|
1129
|
122
|
1130 if (len) {
|
118
|
1131
|
122
|
1132 op->len = len;
|
|
1133 op->getlen = NULL;
|
118
|
1134
|
122
|
1135 if (len <= sizeof(uintptr_t)) {
|
|
1136 op->run = ngx_http_log_copy_short;
|
|
1137 op->data = 0;
|
118
|
1138
|
122
|
1139 while (len--) {
|
|
1140 op->data <<= 8;
|
|
1141 op->data |= data[len];
|
|
1142 }
|
|
1143
|
|
1144 } else {
|
|
1145 op->run = ngx_http_log_copy_long;
|
118
|
1146
|
382
|
1147 p = ngx_pnalloc(cf->pool, len);
|
122
|
1148 if (p == NULL) {
|
|
1149 return NGX_CONF_ERROR;
|
|
1150 }
|
118
|
1151
|
122
|
1152 ngx_memcpy(p, data, len);
|
|
1153 op->data = (uintptr_t) p;
|
118
|
1154 }
|
|
1155 }
|
|
1156 }
|
|
1157 }
|
|
1158
|
|
1159 return NGX_CONF_OK;
|
|
1160
|
|
1161 invalid:
|
|
1162
|
|
1163 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
|
|
1164
|
|
1165 return NGX_CONF_ERROR;
|
|
1166 }
|
|
1167
|
|
1168
|
384
|
1169 static char *
|
|
1170 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1171 {
|
|
1172 ngx_http_log_loc_conf_t *llcf = conf;
|
|
1173
|
|
1174 time_t inactive, valid;
|
|
1175 ngx_str_t *value, s;
|
|
1176 ngx_int_t max, min_uses;
|
|
1177 ngx_uint_t i;
|
|
1178
|
|
1179 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
|
|
1180 return "is duplicate";
|
|
1181 }
|
|
1182
|
|
1183 value = cf->args->elts;
|
|
1184
|
|
1185 max = 0;
|
|
1186 inactive = 10;
|
|
1187 valid = 60;
|
|
1188 min_uses = 1;
|
|
1189
|
|
1190 for (i = 1; i < cf->args->nelts; i++) {
|
|
1191
|
|
1192 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
|
|
1193
|
|
1194 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
|
|
1195 if (max == NGX_ERROR) {
|
|
1196 goto failed;
|
|
1197 }
|
|
1198
|
|
1199 continue;
|
|
1200 }
|
|
1201
|
|
1202 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1203
|
|
1204 s.len = value[i].len - 9;
|
|
1205 s.data = value[i].data + 9;
|
|
1206
|
|
1207 inactive = ngx_parse_time(&s, 1);
|
|
1208 if (inactive < 0) {
|
|
1209 goto failed;
|
|
1210 }
|
|
1211
|
|
1212 continue;
|
|
1213 }
|
|
1214
|
|
1215 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
|
|
1216
|
|
1217 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
|
|
1218 if (min_uses == NGX_ERROR) {
|
|
1219 goto failed;
|
|
1220 }
|
|
1221
|
|
1222 continue;
|
|
1223 }
|
|
1224
|
|
1225 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
|
|
1226
|
|
1227 s.len = value[i].len - 6;
|
|
1228 s.data = value[i].data + 6;
|
|
1229
|
|
1230 valid = ngx_parse_time(&s, 1);
|
|
1231 if (valid < 0) {
|
|
1232 goto failed;
|
|
1233 }
|
|
1234
|
|
1235 continue;
|
|
1236 }
|
|
1237
|
|
1238 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1239
|
|
1240 llcf->open_file_cache = NULL;
|
|
1241
|
|
1242 continue;
|
|
1243 }
|
|
1244
|
|
1245 failed:
|
|
1246
|
|
1247 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1248 "invalid \"open_log_file_cache\" parameter \"%V\"",
|
|
1249 &value[i]);
|
|
1250 return NGX_CONF_ERROR;
|
|
1251 }
|
|
1252
|
|
1253 if (llcf->open_file_cache == NULL) {
|
|
1254 return NGX_CONF_OK;
|
|
1255 }
|
|
1256
|
|
1257 if (max == 0) {
|
|
1258 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1259 "\"open_log_file_cache\" must have \"max\" parameter");
|
|
1260 return NGX_CONF_ERROR;
|
|
1261 }
|
|
1262
|
|
1263 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
|
|
1264
|
|
1265 if (llcf->open_file_cache) {
|
|
1266
|
|
1267 llcf->open_file_cache_valid = valid;
|
|
1268 llcf->open_file_cache_min_uses = min_uses;
|
|
1269
|
|
1270 return NGX_CONF_OK;
|
|
1271 }
|
|
1272
|
|
1273 return NGX_CONF_ERROR;
|
|
1274 }
|
|
1275
|
|
1276
|
118
|
1277 static ngx_int_t
|
122
|
1278 ngx_http_log_init(ngx_conf_t *cf)
|
118
|
1279 {
|
122
|
1280 ngx_str_t *value;
|
|
1281 ngx_array_t a;
|
130
|
1282 ngx_http_handler_pt *h;
|
122
|
1283 ngx_http_log_fmt_t *fmt;
|
|
1284 ngx_http_log_main_conf_t *lmcf;
|
118
|
1285 ngx_http_core_main_conf_t *cmcf;
|
|
1286
|
122
|
1287 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
1288
|
|
1289 if (lmcf->combined_used) {
|
|
1290 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
|
|
1291 return NGX_ERROR;
|
|
1292 }
|
|
1293
|
|
1294 value = ngx_array_push(&a);
|
|
1295 if (value == NULL) {
|
|
1296 return NGX_ERROR;
|
|
1297 }
|
|
1298
|
|
1299 *value = ngx_http_combined_fmt;
|
|
1300 fmt = lmcf->formats.elts;
|
|
1301
|
|
1302 if (ngx_http_log_compile_format(cf, fmt->ops, &a, 0)
|
|
1303 != NGX_CONF_OK)
|
|
1304 {
|
|
1305 return NGX_ERROR;
|
|
1306 }
|
|
1307 }
|
|
1308
|
|
1309 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
118
|
1310
|
130
|
1311 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
|
|
1312 if (h == NULL) {
|
|
1313 return NGX_ERROR;
|
|
1314 }
|
|
1315
|
|
1316 *h = ngx_http_log_handler;
|
118
|
1317
|
|
1318 return NGX_OK;
|
|
1319 }
|