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