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 },
|
676
|
208 { ngx_string("status"), NGX_INT_T_LEN, ngx_http_log_status },
|
122
|
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
|
682
|
221 static ngx_int_t
|
118
|
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) {
|
678
|
587 status = 9;
|
554
|
588
|
|
589 } else {
|
|
590 status = 0;
|
|
591 }
|
|
592
|
676
|
593 return ngx_sprintf(buf, "%03ui", status);
|
118
|
594 }
|
|
595
|
|
596
|
|
597 static u_char *
|
122
|
598 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
|
599 ngx_http_log_op_t *op)
|
118
|
600 {
|
|
601 return ngx_sprintf(buf, "%O", r->connection->sent);
|
|
602 }
|
|
603
|
|
604
|
316
|
605 /*
|
|
606 * although there is a real $body_bytes_sent variable,
|
|
607 * this log operation code function is more optimized for logging
|
|
608 */
|
|
609
|
118
|
610 static u_char *
|
126
|
611 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
612 ngx_http_log_op_t *op)
|
|
613 {
|
|
614 off_t length;
|
|
615
|
|
616 length = r->connection->sent - r->header_size;
|
|
617
|
|
618 if (length > 0) {
|
|
619 return ngx_sprintf(buf, "%O", length);
|
|
620 }
|
|
621
|
|
622 *buf = '0';
|
|
623
|
|
624 return buf + 1;
|
|
625 }
|
|
626
|
|
627
|
|
628 static u_char *
|
|
629 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
630 ngx_http_log_op_t *op)
|
|
631 {
|
212
|
632 return ngx_sprintf(buf, "%O", r->request_length);
|
118
|
633 }
|
|
634
|
|
635
|
|
636 static ngx_int_t
|
|
637 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
|
|
638 ngx_str_t *value)
|
|
639 {
|
|
640 ngx_int_t index;
|
|
641
|
|
642 index = ngx_http_get_variable_index(cf, value);
|
|
643 if (index == NGX_ERROR) {
|
|
644 return NGX_ERROR;
|
|
645 }
|
|
646
|
|
647 op->len = 0;
|
|
648 op->getlen = ngx_http_log_variable_getlen;
|
|
649 op->run = ngx_http_log_variable;
|
|
650 op->data = index;
|
|
651
|
|
652 return NGX_OK;
|
|
653 }
|
|
654
|
|
655
|
|
656 static size_t
|
|
657 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
|
|
658 {
|
376
|
659 uintptr_t len;
|
118
|
660 ngx_http_variable_value_t *value;
|
|
661
|
|
662 value = ngx_http_get_indexed_variable(r, data);
|
|
663
|
122
|
664 if (value == NULL || value->not_found) {
|
118
|
665 return 1;
|
|
666 }
|
|
667
|
376
|
668 len = ngx_http_log_escape(NULL, value->data, value->len);
|
|
669
|
|
670 value->escape = len ? 1 : 0;
|
|
671
|
|
672 return value->len + len * 3;
|
118
|
673 }
|
|
674
|
|
675
|
|
676 static u_char *
|
|
677 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
678 {
|
|
679 ngx_http_variable_value_t *value;
|
|
680
|
|
681 value = ngx_http_get_indexed_variable(r, op->data);
|
|
682
|
122
|
683 if (value == NULL || value->not_found) {
|
118
|
684 *buf = '-';
|
|
685 return buf + 1;
|
|
686 }
|
|
687
|
376
|
688 if (value->escape == 0) {
|
|
689 return ngx_cpymem(buf, value->data, value->len);
|
|
690
|
|
691 } else {
|
|
692 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
|
|
693 }
|
|
694 }
|
|
695
|
|
696
|
|
697 static uintptr_t
|
|
698 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
|
|
699 {
|
540
|
700 ngx_uint_t n;
|
376
|
701 static u_char hex[] = "0123456789ABCDEF";
|
|
702
|
|
703 static uint32_t escape[] = {
|
|
704 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
705
|
|
706 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
707 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
|
|
708
|
|
709 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
710 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
|
|
711
|
|
712 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
644
|
713 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
376
|
714
|
644
|
715 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
716 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
717 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
718 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
376
|
719 };
|
|
720
|
|
721
|
|
722 if (dst == NULL) {
|
|
723
|
|
724 /* find the number of the characters to be escaped */
|
|
725
|
526
|
726 n = 0;
|
376
|
727
|
540
|
728 while (size) {
|
376
|
729 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
730 n++;
|
|
731 }
|
|
732 src++;
|
540
|
733 size--;
|
376
|
734 }
|
|
735
|
|
736 return (uintptr_t) n;
|
|
737 }
|
|
738
|
540
|
739 while (size) {
|
376
|
740 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
741 *dst++ = '\\';
|
|
742 *dst++ = 'x';
|
|
743 *dst++ = hex[*src >> 4];
|
|
744 *dst++ = hex[*src & 0xf];
|
|
745 src++;
|
|
746
|
|
747 } else {
|
|
748 *dst++ = *src++;
|
|
749 }
|
540
|
750 size--;
|
376
|
751 }
|
|
752
|
|
753 return (uintptr_t) dst;
|
118
|
754 }
|
|
755
|
|
756
|
|
757 static void *
|
|
758 ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
759 {
|
|
760 ngx_http_log_main_conf_t *conf;
|
|
761
|
122
|
762 ngx_http_log_fmt_t *fmt;
|
118
|
763
|
|
764 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
|
|
765 if (conf == NULL) {
|
496
|
766 return NULL;
|
118
|
767 }
|
|
768
|
|
769 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
|
122
|
770 != NGX_OK)
|
118
|
771 {
|
496
|
772 return NULL;
|
118
|
773 }
|
|
774
|
122
|
775 fmt = ngx_array_push(&conf->formats);
|
|
776 if (fmt == NULL) {
|
496
|
777 return NULL;
|
118
|
778 }
|
|
779
|
570
|
780 ngx_str_set(&fmt->name, "combined");
|
118
|
781
|
412
|
782 fmt->flushes = NULL;
|
|
783
|
122
|
784 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
|
785 if (fmt->ops == NULL) {
|
496
|
786 return NULL;
|
118
|
787 }
|
|
788
|
|
789 return conf;
|
|
790 }
|
|
791
|
|
792
|
|
793 static void *
|
|
794 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
795 {
|
|
796 ngx_http_log_loc_conf_t *conf;
|
|
797
|
|
798 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
|
|
799 if (conf == NULL) {
|
496
|
800 return NULL;
|
118
|
801 }
|
|
802
|
384
|
803 conf->open_file_cache = NGX_CONF_UNSET_PTR;
|
|
804
|
118
|
805 return conf;
|
|
806 }
|
|
807
|
|
808
|
|
809 static char *
|
|
810 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
811 {
|
|
812 ngx_http_log_loc_conf_t *prev = parent;
|
|
813 ngx_http_log_loc_conf_t *conf = child;
|
|
814
|
|
815 ngx_http_log_t *log;
|
|
816 ngx_http_log_fmt_t *fmt;
|
|
817 ngx_http_log_main_conf_t *lmcf;
|
|
818
|
384
|
819 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
820
|
|
821 conf->open_file_cache = prev->open_file_cache;
|
|
822 conf->open_file_cache_valid = prev->open_file_cache_valid;
|
|
823 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
|
|
824
|
|
825 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
826 conf->open_file_cache = NULL;
|
|
827 }
|
|
828 }
|
|
829
|
278
|
830 if (conf->logs || conf->off) {
|
|
831 return NGX_CONF_OK;
|
|
832 }
|
118
|
833
|
386
|
834 conf->logs = prev->logs;
|
|
835 conf->off = prev->off;
|
118
|
836
|
278
|
837 if (conf->logs || conf->off) {
|
|
838 return NGX_CONF_OK;
|
|
839 }
|
118
|
840
|
278
|
841 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
842 if (conf->logs == NULL) {
|
|
843 return NGX_CONF_ERROR;
|
|
844 }
|
118
|
845
|
278
|
846 log = ngx_array_push(conf->logs);
|
|
847 if (log == NULL) {
|
|
848 return NGX_CONF_ERROR;
|
|
849 }
|
118
|
850
|
278
|
851 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
|
|
852 if (log->file == NULL) {
|
|
853 return NGX_CONF_ERROR;
|
|
854 }
|
118
|
855
|
386
|
856 log->script = NULL;
|
278
|
857 log->disk_full_time = 0;
|
|
858 log->error_log_time = 0;
|
118
|
859
|
278
|
860 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
861 fmt = lmcf->formats.elts;
|
|
862
|
|
863 /* the default "combined" format */
|
412
|
864 log->format = &fmt[0];
|
278
|
865 lmcf->combined_used = 1;
|
118
|
866
|
|
867 return NGX_CONF_OK;
|
|
868 }
|
|
869
|
|
870
|
|
871 static char *
|
|
872 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
873 {
|
|
874 ngx_http_log_loc_conf_t *llcf = conf;
|
|
875
|
384
|
876 ssize_t buf;
|
|
877 ngx_uint_t i, n;
|
|
878 ngx_str_t *value, name;
|
|
879 ngx_http_log_t *log;
|
|
880 ngx_http_log_fmt_t *fmt;
|
|
881 ngx_http_log_main_conf_t *lmcf;
|
|
882 ngx_http_script_compile_t sc;
|
118
|
883
|
|
884 value = cf->args->elts;
|
|
885
|
|
886 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
887 llcf->off = 1;
|
516
|
888 if (cf->args->nelts == 2) {
|
|
889 return NGX_CONF_OK;
|
|
890 }
|
|
891
|
|
892 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
893 "invalid parameter \"%V\"", &value[2]);
|
|
894 return NGX_CONF_ERROR;
|
118
|
895 }
|
|
896
|
|
897 if (llcf->logs == NULL) {
|
|
898 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
899 if (llcf->logs == NULL) {
|
|
900 return NGX_CONF_ERROR;
|
|
901 }
|
|
902 }
|
|
903
|
|
904 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
905
|
|
906 log = ngx_array_push(llcf->logs);
|
|
907 if (log == NULL) {
|
|
908 return NGX_CONF_ERROR;
|
|
909 }
|
|
910
|
384
|
911 ngx_memzero(log, sizeof(ngx_http_log_t));
|
|
912
|
|
913 n = ngx_http_script_variables_count(&value[1]);
|
|
914
|
|
915 if (n == 0) {
|
|
916 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
|
|
917 if (log->file == NULL) {
|
|
918 return NGX_CONF_ERROR;
|
|
919 }
|
|
920
|
|
921 } else {
|
454
|
922 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
|
384
|
923 return NGX_CONF_ERROR;
|
|
924 }
|
|
925
|
|
926 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
|
|
927 if (log->script == NULL) {
|
|
928 return NGX_CONF_ERROR;
|
|
929 }
|
|
930
|
|
931 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
932
|
|
933 sc.cf = cf;
|
|
934 sc.source = &value[1];
|
|
935 sc.lengths = &log->script->lengths;
|
|
936 sc.values = &log->script->values;
|
|
937 sc.variables = n;
|
|
938 sc.complete_lengths = 1;
|
|
939 sc.complete_values = 1;
|
|
940
|
|
941 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
942 return NGX_CONF_ERROR;
|
|
943 }
|
118
|
944 }
|
|
945
|
120
|
946 if (cf->args->nelts >= 3) {
|
118
|
947 name = value[2];
|
130
|
948
|
|
949 if (ngx_strcmp(name.data, "combined") == 0) {
|
|
950 lmcf->combined_used = 1;
|
|
951 }
|
|
952
|
118
|
953 } else {
|
570
|
954 ngx_str_set(&name, "combined");
|
122
|
955 lmcf->combined_used = 1;
|
118
|
956 }
|
|
957
|
|
958 fmt = lmcf->formats.elts;
|
|
959 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
960 if (fmt[i].name.len == name.len
|
|
961 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
962 {
|
412
|
963 log->format = &fmt[i];
|
120
|
964 goto buffer;
|
118
|
965 }
|
|
966 }
|
|
967
|
|
968 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
969 "unknown log format \"%V\"", &name);
|
120
|
970 return NGX_CONF_ERROR;
|
118
|
971
|
120
|
972 buffer:
|
|
973
|
|
974 if (cf->args->nelts == 4) {
|
|
975 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
|
|
976 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
977 "invalid parameter \"%V\"", &value[3]);
|
|
978 return NGX_CONF_ERROR;
|
|
979 }
|
|
980
|
384
|
981 if (log->script) {
|
|
982 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
640
|
983 "buffered logs cannot have variables in name");
|
384
|
984 return NGX_CONF_ERROR;
|
|
985 }
|
|
986
|
120
|
987 name.len = value[3].len - 7;
|
|
988 name.data = value[3].data + 7;
|
|
989
|
|
990 buf = ngx_parse_size(&name);
|
|
991
|
|
992 if (buf == NGX_ERROR) {
|
|
993 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
654
|
994 "invalid buffer value \"%V\"", &name);
|
120
|
995 return NGX_CONF_ERROR;
|
|
996 }
|
|
997
|
|
998 if (log->file->buffer && log->file->last - log->file->pos != buf) {
|
|
999 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1000 "access_log \"%V\" already defined "
|
|
1001 "with different buffer size", &value[1]);
|
|
1002 return NGX_CONF_ERROR;
|
|
1003 }
|
|
1004
|
|
1005 log->file->buffer = ngx_palloc(cf->pool, buf);
|
|
1006 if (log->file->buffer == NULL) {
|
|
1007 return NGX_CONF_ERROR;
|
|
1008 }
|
|
1009
|
|
1010 log->file->pos = log->file->buffer;
|
|
1011 log->file->last = log->file->buffer + buf;
|
|
1012 }
|
|
1013
|
|
1014 return NGX_CONF_OK;
|
118
|
1015 }
|
|
1016
|
|
1017
|
|
1018 static char *
|
|
1019 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1020 {
|
|
1021 ngx_http_log_main_conf_t *lmcf = conf;
|
|
1022
|
122
|
1023 ngx_str_t *value;
|
|
1024 ngx_uint_t i;
|
|
1025 ngx_http_log_fmt_t *fmt;
|
118
|
1026
|
654
|
1027 if (cf->cmd_type != NGX_HTTP_MAIN_CONF) {
|
|
1028 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1029 "the \"log_format\" directive may be used "
|
|
1030 "only on \"http\" level");
|
|
1031 }
|
|
1032
|
118
|
1033 value = cf->args->elts;
|
|
1034
|
|
1035 fmt = lmcf->formats.elts;
|
122
|
1036 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
1037 if (fmt[i].name.len == value[1].len
|
|
1038 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
|
118
|
1039 {
|
380
|
1040 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1041 "duplicate \"log_format\" name \"%V\"",
|
|
1042 &value[1]);
|
|
1043 return NGX_CONF_ERROR;
|
118
|
1044 }
|
|
1045 }
|
|
1046
|
|
1047 fmt = ngx_array_push(&lmcf->formats);
|
|
1048 if (fmt == NULL) {
|
|
1049 return NGX_CONF_ERROR;
|
|
1050 }
|
|
1051
|
|
1052 fmt->name = value[1];
|
|
1053
|
412
|
1054 fmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));
|
|
1055 if (fmt->flushes == NULL) {
|
|
1056 return NGX_CONF_ERROR;
|
|
1057 }
|
|
1058
|
122
|
1059 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
118
|
1060 if (fmt->ops == NULL) {
|
|
1061 return NGX_CONF_ERROR;
|
|
1062 }
|
|
1063
|
412
|
1064 return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
|
122
|
1065 }
|
|
1066
|
118
|
1067
|
122
|
1068 static char *
|
412
|
1069 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
|
|
1070 ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s)
|
122
|
1071 {
|
260
|
1072 u_char *data, *p, ch;
|
|
1073 size_t i, len;
|
|
1074 ngx_str_t *value, var;
|
412
|
1075 ngx_int_t *flush;
|
260
|
1076 ngx_uint_t bracket;
|
|
1077 ngx_http_log_op_t *op;
|
|
1078 ngx_http_log_var_t *v;
|
122
|
1079
|
|
1080 value = args->elts;
|
|
1081
|
|
1082 for ( /* void */ ; s < args->nelts; s++) {
|
118
|
1083
|
|
1084 i = 0;
|
|
1085
|
|
1086 while (i < value[s].len) {
|
|
1087
|
122
|
1088 op = ngx_array_push(ops);
|
118
|
1089 if (op == NULL) {
|
|
1090 return NGX_CONF_ERROR;
|
|
1091 }
|
|
1092
|
|
1093 data = &value[s].data[i];
|
|
1094
|
260
|
1095 if (value[s].data[i] == '$') {
|
118
|
1096
|
|
1097 if (++i == value[s].len) {
|
|
1098 goto invalid;
|
|
1099 }
|
|
1100
|
|
1101 if (value[s].data[i] == '{') {
|
|
1102 bracket = 1;
|
|
1103
|
|
1104 if (++i == value[s].len) {
|
|
1105 goto invalid;
|
|
1106 }
|
|
1107
|
|
1108 var.data = &value[s].data[i];
|
|
1109
|
|
1110 } else {
|
|
1111 bracket = 0;
|
|
1112 var.data = &value[s].data[i];
|
|
1113 }
|
|
1114
|
|
1115 for (var.len = 0; i < value[s].len; i++, var.len++) {
|
|
1116 ch = value[s].data[i];
|
|
1117
|
|
1118 if (ch == '}' && bracket) {
|
|
1119 i++;
|
|
1120 bracket = 0;
|
|
1121 break;
|
|
1122 }
|
|
1123
|
|
1124 if ((ch >= 'A' && ch <= 'Z')
|
|
1125 || (ch >= 'a' && ch <= 'z')
|
|
1126 || (ch >= '0' && ch <= '9')
|
|
1127 || ch == '_')
|
|
1128 {
|
|
1129 continue;
|
|
1130 }
|
|
1131
|
|
1132 break;
|
|
1133 }
|
|
1134
|
|
1135 if (bracket) {
|
|
1136 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1137 "the closing bracket in \"%V\" "
|
|
1138 "variable is missing", &var);
|
|
1139 return NGX_CONF_ERROR;
|
|
1140 }
|
|
1141
|
|
1142 if (var.len == 0) {
|
|
1143 goto invalid;
|
|
1144 }
|
|
1145
|
126
|
1146 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
|
|
1147 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1148 "use \"$body_bytes_sent\" instead of "
|
|
1149 "\"$apache_bytes_sent\"");
|
|
1150 }
|
|
1151
|
122
|
1152 for (v = ngx_http_log_vars; v->name.len; v++) {
|
|
1153
|
|
1154 if (v->name.len == var.len
|
|
1155 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
|
|
1156 {
|
|
1157 op->len = v->len;
|
|
1158 op->getlen = NULL;
|
|
1159 op->run = v->run;
|
|
1160 op->data = 0;
|
|
1161
|
|
1162 goto found;
|
|
1163 }
|
|
1164 }
|
|
1165
|
118
|
1166 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
|
|
1167 return NGX_CONF_ERROR;
|
|
1168 }
|
|
1169
|
412
|
1170 if (flushes) {
|
|
1171
|
|
1172 flush = ngx_array_push(flushes);
|
|
1173 if (flush == NULL) {
|
|
1174 return NGX_CONF_ERROR;
|
|
1175 }
|
|
1176
|
|
1177 *flush = op->data; /* variable index */
|
|
1178 }
|
|
1179
|
122
|
1180 found:
|
|
1181
|
|
1182 continue;
|
|
1183 }
|
|
1184
|
|
1185 i++;
|
118
|
1186
|
260
|
1187 while (i < value[s].len && value[s].data[i] != '$') {
|
122
|
1188 i++;
|
|
1189 }
|
118
|
1190
|
122
|
1191 len = &value[s].data[i] - data;
|
118
|
1192
|
122
|
1193 if (len) {
|
118
|
1194
|
122
|
1195 op->len = len;
|
|
1196 op->getlen = NULL;
|
118
|
1197
|
122
|
1198 if (len <= sizeof(uintptr_t)) {
|
|
1199 op->run = ngx_http_log_copy_short;
|
|
1200 op->data = 0;
|
118
|
1201
|
122
|
1202 while (len--) {
|
|
1203 op->data <<= 8;
|
|
1204 op->data |= data[len];
|
|
1205 }
|
|
1206
|
|
1207 } else {
|
|
1208 op->run = ngx_http_log_copy_long;
|
118
|
1209
|
382
|
1210 p = ngx_pnalloc(cf->pool, len);
|
122
|
1211 if (p == NULL) {
|
|
1212 return NGX_CONF_ERROR;
|
|
1213 }
|
118
|
1214
|
122
|
1215 ngx_memcpy(p, data, len);
|
|
1216 op->data = (uintptr_t) p;
|
118
|
1217 }
|
|
1218 }
|
|
1219 }
|
|
1220 }
|
|
1221
|
|
1222 return NGX_CONF_OK;
|
|
1223
|
|
1224 invalid:
|
|
1225
|
|
1226 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
|
|
1227
|
|
1228 return NGX_CONF_ERROR;
|
|
1229 }
|
|
1230
|
|
1231
|
384
|
1232 static char *
|
|
1233 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1234 {
|
|
1235 ngx_http_log_loc_conf_t *llcf = conf;
|
|
1236
|
|
1237 time_t inactive, valid;
|
|
1238 ngx_str_t *value, s;
|
|
1239 ngx_int_t max, min_uses;
|
|
1240 ngx_uint_t i;
|
|
1241
|
|
1242 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
|
|
1243 return "is duplicate";
|
|
1244 }
|
|
1245
|
|
1246 value = cf->args->elts;
|
|
1247
|
|
1248 max = 0;
|
|
1249 inactive = 10;
|
|
1250 valid = 60;
|
|
1251 min_uses = 1;
|
|
1252
|
|
1253 for (i = 1; i < cf->args->nelts; i++) {
|
|
1254
|
|
1255 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
|
|
1256
|
|
1257 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
|
|
1258 if (max == NGX_ERROR) {
|
|
1259 goto failed;
|
|
1260 }
|
|
1261
|
|
1262 continue;
|
|
1263 }
|
|
1264
|
|
1265 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1266
|
|
1267 s.len = value[i].len - 9;
|
|
1268 s.data = value[i].data + 9;
|
|
1269
|
|
1270 inactive = ngx_parse_time(&s, 1);
|
662
|
1271 if (inactive == (time_t) NGX_ERROR) {
|
384
|
1272 goto failed;
|
|
1273 }
|
|
1274
|
|
1275 continue;
|
|
1276 }
|
|
1277
|
|
1278 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
|
|
1279
|
|
1280 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
|
|
1281 if (min_uses == NGX_ERROR) {
|
|
1282 goto failed;
|
|
1283 }
|
|
1284
|
|
1285 continue;
|
|
1286 }
|
|
1287
|
|
1288 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
|
|
1289
|
|
1290 s.len = value[i].len - 6;
|
|
1291 s.data = value[i].data + 6;
|
|
1292
|
|
1293 valid = ngx_parse_time(&s, 1);
|
662
|
1294 if (valid == (time_t) NGX_ERROR) {
|
384
|
1295 goto failed;
|
|
1296 }
|
|
1297
|
|
1298 continue;
|
|
1299 }
|
|
1300
|
|
1301 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1302
|
|
1303 llcf->open_file_cache = NULL;
|
|
1304
|
|
1305 continue;
|
|
1306 }
|
|
1307
|
|
1308 failed:
|
|
1309
|
|
1310 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1311 "invalid \"open_log_file_cache\" parameter \"%V\"",
|
|
1312 &value[i]);
|
|
1313 return NGX_CONF_ERROR;
|
|
1314 }
|
|
1315
|
|
1316 if (llcf->open_file_cache == NULL) {
|
|
1317 return NGX_CONF_OK;
|
|
1318 }
|
|
1319
|
|
1320 if (max == 0) {
|
|
1321 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1322 "\"open_log_file_cache\" must have \"max\" parameter");
|
|
1323 return NGX_CONF_ERROR;
|
|
1324 }
|
|
1325
|
|
1326 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
|
|
1327
|
|
1328 if (llcf->open_file_cache) {
|
|
1329
|
|
1330 llcf->open_file_cache_valid = valid;
|
|
1331 llcf->open_file_cache_min_uses = min_uses;
|
|
1332
|
|
1333 return NGX_CONF_OK;
|
|
1334 }
|
|
1335
|
|
1336 return NGX_CONF_ERROR;
|
|
1337 }
|
|
1338
|
|
1339
|
118
|
1340 static ngx_int_t
|
122
|
1341 ngx_http_log_init(ngx_conf_t *cf)
|
118
|
1342 {
|
122
|
1343 ngx_str_t *value;
|
|
1344 ngx_array_t a;
|
130
|
1345 ngx_http_handler_pt *h;
|
122
|
1346 ngx_http_log_fmt_t *fmt;
|
|
1347 ngx_http_log_main_conf_t *lmcf;
|
118
|
1348 ngx_http_core_main_conf_t *cmcf;
|
|
1349
|
122
|
1350 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
1351
|
|
1352 if (lmcf->combined_used) {
|
|
1353 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
|
|
1354 return NGX_ERROR;
|
|
1355 }
|
|
1356
|
|
1357 value = ngx_array_push(&a);
|
|
1358 if (value == NULL) {
|
|
1359 return NGX_ERROR;
|
|
1360 }
|
|
1361
|
|
1362 *value = ngx_http_combined_fmt;
|
|
1363 fmt = lmcf->formats.elts;
|
|
1364
|
412
|
1365 if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
|
122
|
1366 != NGX_CONF_OK)
|
|
1367 {
|
|
1368 return NGX_ERROR;
|
|
1369 }
|
|
1370 }
|
|
1371
|
|
1372 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
118
|
1373
|
130
|
1374 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
|
|
1375 if (h == NULL) {
|
|
1376 return NGX_ERROR;
|
|
1377 }
|
|
1378
|
|
1379 *h = ngx_http_log_handler;
|
118
|
1380
|
|
1381 return NGX_OK;
|
|
1382 }
|