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