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