92 u_char *buf,
size_t len);
97 static ssize_t ngx_http_log_gzip(
ngx_fd_t fd, u_char *buf,
size_t len,
100 static void *ngx_http_log_gzip_alloc(
void *opaque, u_int items, u_int size);
101 static void ngx_http_log_gzip_free(
void *opaque,
void *address);
105 static void ngx_http_log_flush_handler(
ngx_event_t *ev);
132 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src,
size_t size);
135 static void *ngx_http_log_create_main_conf(
ngx_conf_t *cf);
136 static void *ngx_http_log_create_loc_conf(
ngx_conf_t *cf);
137 static char *ngx_http_log_merge_loc_conf(
ngx_conf_t *cf,
void *parent,
143 static char *ngx_http_log_compile_format(
ngx_conf_t *cf,
154 ngx_http_log_set_format,
162 ngx_http_log_set_log,
169 ngx_http_log_open_file_cache,
182 ngx_http_log_create_main_conf,
188 ngx_http_log_create_loc_conf,
189 ngx_http_log_merge_loc_conf
195 &ngx_http_log_module_ctx,
196 ngx_http_log_commands,
213 ngx_string(
"$remote_addr - $remote_user [$time_local] "
214 "\"$request\" $status $body_bytes_sent "
215 "\"$http_referer\" \"$http_user_agent\"");
220 {
ngx_string(
"time_local"),
sizeof(
"28/Sep/1970:12:00:00 +0600") - 1,
222 {
ngx_string(
"time_iso8601"),
sizeof(
"1970-09-28T12:00:00+06:00") - 1,
223 ngx_http_log_iso8601 },
226 ngx_http_log_request_time },
230 ngx_http_log_body_bytes_sent },
232 ngx_http_log_request_length },
261 if (
ngx_time() == log[l].disk_full_time) {
277 if (op[i].len == 0) {
278 len += op[
i].
getlen(r, op[i].data);
291 if (len > (
size_t) (buffer->
last - buffer->
pos)) {
293 ngx_http_log_write(r, &log[l], buffer->
start,
299 if (len <= (
size_t) (buffer->
last - buffer->
pos)) {
308 p = op[
i].
run(r, p, &op[i]);
331 p = op[
i].
run(r, p, &op[i]);
336 ngx_http_log_write(r, &log[l], line, p - line);
355 if (log->
script == NULL) {
361 if (buffer && buffer->
gzip) {
362 n = ngx_http_log_gzip(log->
file->
fd, buf, len, buffer->
gzip,
365 n = ngx_write_fd(log->
file->
fd, buf, len);
368 n = ngx_write_fd(log->
file->
fd, buf, len);
373 n = ngx_http_log_script_write(r, log->
script, &name, buf, len);
376 if (n == (ssize_t) len) {
411 u_char **name, u_char *buf,
size_t len)
431 path.
data[root] =
'\0';
456 "testing \"%s\" existence failed", path.
data);
464 "testing \"%s\" existence failed", path.
data);
483 "http log \"%s\"", log.
data);
509 "http log #%d", of.
fd);
511 n = ngx_write_fd(of.
fd, buf, len);
523 int rc, wbits, memlevel;
532 memlevel = MAX_MEM_LEVEL - 1;
534 while ((ssize_t) len < ((1 << (wbits - 1)) - 262)) {
544 size = len + ((len + 7) >> 3) + ((len + 63) >> 6) + 5 + 18;
556 zstream.zalloc = ngx_http_log_gzip_alloc;
557 zstream.zfree = ngx_http_log_gzip_free;
558 zstream.opaque = pool;
565 zstream.next_in = buf;
566 zstream.avail_in = len;
567 zstream.next_out = out;
568 zstream.avail_out = size;
570 rc = deflateInit2(&zstream, (
int) level, Z_DEFLATED, wbits + 16, memlevel,
579 "deflate in: ni:%p no:%p ai:%ud ao:%ud",
580 zstream.next_in, zstream.next_out,
581 zstream.avail_in, zstream.avail_out);
583 rc = deflate(&zstream, Z_FINISH);
585 if (rc != Z_STREAM_END) {
587 "deflate(Z_FINISH) failed: %d", rc);
592 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
593 zstream.next_in, zstream.next_out,
594 zstream.avail_in, zstream.avail_out,
597 size -= zstream.avail_out;
599 rc = deflateEnd(&zstream);
606 n = ngx_write_fd(fd, out, size);
608 if (n != (ssize_t) size) {
627 ngx_http_log_gzip_alloc(
void *opaque, u_int items, u_int size)
632 "gzip alloc: n:%ud s:%ud", items, size);
639 ngx_http_log_gzip_free(
void *opaque,
void *address)
668 n = ngx_http_log_gzip(file->
fd, buffer->
start, len, buffer->
gzip, log);
670 n = ngx_write_fd(file->
fd, buffer->
start, len);
673 n = ngx_write_fd(file->
fd, buffer->
start, len);
681 }
else if ((
size_t) n != len) {
699 "http log buffer flush handler");
701 ngx_http_log_flush(ev->
data, ev->
log);
716 *buf++ = (u_char) (data & 0xff);
783 return ngx_sprintf(buf,
"%T.%03M", ms / 1000, ms % 1000);
860 op->
getlen = ngx_http_log_variable_getlen;
861 op->
run = ngx_http_log_variable;
880 len = ngx_http_log_escape(NULL, value->
data, value->
len);
882 value->
escape = len ? 1 : 0;
884 return value->
len + len * 3;
904 return (u_char *) ngx_http_log_escape(buf, value->
data, value->
len);
910 ngx_http_log_escape(u_char *dst, u_char *src,
size_t size)
913 static u_char hex[] =
"0123456789ABCDEF";
915 static uint32_t escape[] = {
941 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
948 return (uintptr_t) n;
952 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
955 *dst++ = hex[*src >> 4];
956 *dst++ = hex[*src & 0xf];
965 return (uintptr_t) dst;
997 if (fmt->
ops == NULL) {
1022 ngx_http_log_merge_loc_conf(
ngx_conf_t *cf,
void *parent,
void *child)
1042 if (conf->
logs || conf->
off) {
1049 if (conf->
logs || conf->
off) {
1054 if (conf->
logs == NULL) {
1064 if (log->
file == NULL) {
1108 "invalid parameter \"%V\"", &value[2]);
1112 if (llcf->
logs == NULL) {
1114 if (llcf->
logs == NULL) {
1132 if (log->
file == NULL) {
1142 if (log->
script == NULL) {
1175 if (fmt[i].name.
len == name.
len
1183 if (log->
format == NULL) {
1185 "unknown log format \"%V\"", &name);
1195 if (
ngx_strncmp(value[i].data,
"buffer=", 7) == 0) {
1203 "invalid buffer size \"%V\"", &s);
1210 if (
ngx_strncmp(value[i].data,
"flush=", 6) == 0) {
1218 "invalid flush time \"%V\"", &s);
1226 && (value[i].len == 4 || value[i].data[4] ==
'='))
1233 if (value[i].len == 4) {
1234 gzip = Z_BEST_SPEED;
1243 if (gzip < 1 || gzip > 9) {
1245 "invalid compression level \"%V\"", &s);
1253 "nginx was built without zlib support");
1259 "invalid parameter \"%V\"", &value[i]);
1263 if (flush && size == 0) {
1265 "no buffer is defined for access_log \"%V\"",
1274 "buffered logs cannot have variables in name");
1281 if (buffer->
last - buffer->
start != size
1282 || buffer->
flush != flush
1283 || buffer->
gzip != gzip)
1286 "access_log \"%V\" already defined "
1287 "with conflicting parameters",
1296 if (buffer == NULL) {
1301 if (buffer->
start == NULL) {
1310 if (buffer->
event == NULL) {
1318 buffer->
flush = flush;
1321 buffer->
gzip = gzip;
1342 "the \"log_format\" directive may be used "
1343 "only on \"http\" level");
1350 if (fmt[i].name.
len == value[1].
len
1354 "duplicate \"log_format\" name \"%V\"",
1365 fmt->
name = value[1];
1373 if (fmt->
ops == NULL) {
1377 return ngx_http_log_compile_format(cf, fmt->
flushes, fmt->
ops, cf->
args, 2);
1385 u_char *data, *p, ch;
1395 for ( ; s < args->
nelts; s++) {
1399 while (i < value[s].len) {
1406 data = &value[s].
data[
i];
1408 if (value[s].data[i] ==
'$') {
1410 if (++i == value[s].len) {
1414 if (value[s].data[i] ==
'{') {
1417 if (++i == value[s].len) {
1428 for (var.
len = 0; i < value[s].
len; i++, var.
len++) {
1429 ch = value[s].
data[
i];
1431 if (ch ==
'}' && bracket) {
1437 if ((ch >=
'A' && ch <=
'Z')
1438 || (ch >=
'a' && ch <=
'z')
1439 || (ch >=
'0' && ch <=
'9')
1450 "the closing bracket in \"%V\" "
1451 "variable is missing", &var);
1459 for (v = ngx_http_log_vars; v->
name.
len; v++) {
1473 if (ngx_http_log_variable_compile(cf, op, &var) !=
NGX_OK) {
1480 if (flush == NULL) {
1494 while (i < value[s].len && value[s].data[i] !=
'$') {
1498 len = &value[s].
data[
i] - data;
1505 if (len <=
sizeof(uintptr_t)) {
1506 op->
run = ngx_http_log_copy_short;
1511 op->
data |= data[len];
1515 op->
run = ngx_http_log_copy_long;
1523 op->
data = (uintptr_t) p;
1544 time_t inactive, valid;
1550 return "is duplicate";
1564 max =
ngx_atoi(value[i].data + 4, value[i].len - 4);
1572 if (
ngx_strncmp(value[i].data,
"inactive=", 9) == 0) {
1585 if (
ngx_strncmp(value[i].data,
"min_uses=", 9) == 0) {
1587 min_uses =
ngx_atoi(value[i].data + 9, value[i].len - 9);
1595 if (
ngx_strncmp(value[i].data,
"valid=", 6) == 0) {
1618 "invalid \"open_log_file_cache\" parameter \"%V\"",
1629 "\"open_log_file_cache\" must have \"max\" parameter");
1665 if (value == NULL) {
1669 *value = ngx_http_combined_fmt;
1672 if (ngx_http_log_compile_format(cf, NULL, fmt->
ops, &a, 0)
1686 *h = ngx_http_log_handler;