Mercurial > hg > nginx
comparison src/http/modules/ngx_http_log_module.c @ 5702:777202558122
Added syslog support for error_log and access_log directives.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Mon, 12 May 2014 16:34:15 +0400 |
parents | cb308813b453 |
children | 7f425d67f91a |
comparison
equal
deleted
inserted
replaced
5701:1209b8a7b077 | 5702:777202558122 |
---|---|
64 typedef struct { | 64 typedef struct { |
65 ngx_open_file_t *file; | 65 ngx_open_file_t *file; |
66 ngx_http_log_script_t *script; | 66 ngx_http_log_script_t *script; |
67 time_t disk_full_time; | 67 time_t disk_full_time; |
68 time_t error_log_time; | 68 time_t error_log_time; |
69 ngx_syslog_peer_t *syslog_peer; | |
69 ngx_http_log_fmt_t *format; | 70 ngx_http_log_fmt_t *format; |
70 ngx_http_complex_value_t *filter; | 71 ngx_http_complex_value_t *filter; |
71 } ngx_http_log_t; | 72 } ngx_http_log_t; |
72 | 73 |
73 | 74 |
238 | 239 |
239 static ngx_int_t | 240 static ngx_int_t |
240 ngx_http_log_handler(ngx_http_request_t *r) | 241 ngx_http_log_handler(ngx_http_request_t *r) |
241 { | 242 { |
242 u_char *line, *p; | 243 u_char *line, *p; |
243 size_t len; | 244 size_t len, size; |
245 ssize_t n; | |
244 ngx_str_t val; | 246 ngx_str_t val; |
245 ngx_uint_t i, l; | 247 ngx_uint_t i, l; |
246 ngx_http_log_t *log; | 248 ngx_http_log_t *log; |
247 ngx_http_log_op_t *op; | 249 ngx_http_log_op_t *op; |
248 ngx_http_log_buf_t *buffer; | 250 ngx_http_log_buf_t *buffer; |
292 } else { | 294 } else { |
293 len += op[i].len; | 295 len += op[i].len; |
294 } | 296 } |
295 } | 297 } |
296 | 298 |
299 if (log[l].syslog_peer) { | |
300 | |
301 /* length of syslog's PRI and HEADER message parts */ | |
302 len += sizeof("<255>Jan 01 00:00:00 ") - 1 | |
303 + ngx_cycle->hostname.len + 1 | |
304 + log[l].syslog_peer->tag.len + 2; | |
305 | |
306 goto alloc_line; | |
307 } | |
308 | |
297 len += NGX_LINEFEED_SIZE; | 309 len += NGX_LINEFEED_SIZE; |
298 | 310 |
299 buffer = log[l].file ? log[l].file->data : NULL; | 311 buffer = log[l].file ? log[l].file->data : NULL; |
300 | 312 |
301 if (buffer) { | 313 if (buffer) { |
330 if (buffer->event && buffer->event->timer_set) { | 342 if (buffer->event && buffer->event->timer_set) { |
331 ngx_del_timer(buffer->event); | 343 ngx_del_timer(buffer->event); |
332 } | 344 } |
333 } | 345 } |
334 | 346 |
347 alloc_line: | |
348 | |
335 line = ngx_pnalloc(r->pool, len); | 349 line = ngx_pnalloc(r->pool, len); |
336 if (line == NULL) { | 350 if (line == NULL) { |
337 return NGX_ERROR; | 351 return NGX_ERROR; |
338 } | 352 } |
339 | 353 |
340 p = line; | 354 p = line; |
341 | 355 |
356 if (log[l].syslog_peer) { | |
357 p = ngx_syslog_add_header(log[l].syslog_peer, line); | |
358 } | |
359 | |
342 for (i = 0; i < log[l].format->ops->nelts; i++) { | 360 for (i = 0; i < log[l].format->ops->nelts; i++) { |
343 p = op[i].run(r, p, &op[i]); | 361 p = op[i].run(r, p, &op[i]); |
362 } | |
363 | |
364 if (log[l].syslog_peer) { | |
365 | |
366 size = p - line; | |
367 | |
368 n = ngx_syslog_send(log[l].syslog_peer, line, size); | |
369 | |
370 if (n < 0) { | |
371 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, | |
372 "send() to syslog failed"); | |
373 | |
374 } else if ((size_t) n != size) { | |
375 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, | |
376 "send() to syslog has written only %z of %uz", | |
377 n, size); | |
378 } | |
379 | |
380 continue; | |
344 } | 381 } |
345 | 382 |
346 ngx_linefeed(p); | 383 ngx_linefeed(p); |
347 | 384 |
348 ngx_http_log_write(r, &log[l], line, p - line); | 385 ngx_http_log_write(r, &log[l], line, p - line); |
1078 } | 1115 } |
1079 | 1116 |
1080 log->script = NULL; | 1117 log->script = NULL; |
1081 log->disk_full_time = 0; | 1118 log->disk_full_time = 0; |
1082 log->error_log_time = 0; | 1119 log->error_log_time = 0; |
1120 log->syslog_peer = NULL; | |
1083 | 1121 |
1084 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module); | 1122 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module); |
1085 fmt = lmcf->formats.elts; | 1123 fmt = lmcf->formats.elts; |
1086 | 1124 |
1087 /* the default "combined" format */ | 1125 /* the default "combined" format */ |
1101 ngx_int_t gzip; | 1139 ngx_int_t gzip; |
1102 ngx_uint_t i, n; | 1140 ngx_uint_t i, n; |
1103 ngx_msec_t flush; | 1141 ngx_msec_t flush; |
1104 ngx_str_t *value, name, s, filter; | 1142 ngx_str_t *value, name, s, filter; |
1105 ngx_http_log_t *log; | 1143 ngx_http_log_t *log; |
1144 ngx_syslog_peer_t *peer; | |
1106 ngx_http_log_buf_t *buffer; | 1145 ngx_http_log_buf_t *buffer; |
1107 ngx_http_log_fmt_t *fmt; | 1146 ngx_http_log_fmt_t *fmt; |
1108 ngx_http_log_main_conf_t *lmcf; | 1147 ngx_http_log_main_conf_t *lmcf; |
1109 ngx_http_script_compile_t sc; | 1148 ngx_http_script_compile_t sc; |
1110 ngx_http_compile_complex_value_t ccv; | 1149 ngx_http_compile_complex_value_t ccv; |
1135 if (log == NULL) { | 1174 if (log == NULL) { |
1136 return NGX_CONF_ERROR; | 1175 return NGX_CONF_ERROR; |
1137 } | 1176 } |
1138 | 1177 |
1139 ngx_memzero(log, sizeof(ngx_http_log_t)); | 1178 ngx_memzero(log, sizeof(ngx_http_log_t)); |
1179 | |
1180 | |
1181 if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) { | |
1182 | |
1183 peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t)); | |
1184 if (peer == NULL) { | |
1185 return NGX_CONF_ERROR; | |
1186 } | |
1187 | |
1188 if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) { | |
1189 return NGX_CONF_ERROR; | |
1190 } | |
1191 | |
1192 log->syslog_peer = peer; | |
1193 | |
1194 goto process_formats; | |
1195 } | |
1140 | 1196 |
1141 n = ngx_http_script_variables_count(&value[1]); | 1197 n = ngx_http_script_variables_count(&value[1]); |
1142 | 1198 |
1143 if (n == 0) { | 1199 if (n == 0) { |
1144 log->file = ngx_conf_open_file(cf->cycle, &value[1]); | 1200 log->file = ngx_conf_open_file(cf->cycle, &value[1]); |
1169 if (ngx_http_script_compile(&sc) != NGX_OK) { | 1225 if (ngx_http_script_compile(&sc) != NGX_OK) { |
1170 return NGX_CONF_ERROR; | 1226 return NGX_CONF_ERROR; |
1171 } | 1227 } |
1172 } | 1228 } |
1173 | 1229 |
1230 process_formats: | |
1231 | |
1174 if (cf->args->nelts >= 3) { | 1232 if (cf->args->nelts >= 3) { |
1175 name = value[2]; | 1233 name = value[2]; |
1176 | 1234 |
1177 if (ngx_strcmp(name.data, "combined") == 0) { | 1235 if (ngx_strcmp(name.data, "combined") == 0) { |
1178 lmcf->combined_used = 1; | 1236 lmcf->combined_used = 1; |
1195 | 1253 |
1196 if (log->format == NULL) { | 1254 if (log->format == NULL) { |
1197 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | 1255 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1198 "unknown log format \"%V\"", &name); | 1256 "unknown log format \"%V\"", &name); |
1199 return NGX_CONF_ERROR; | 1257 return NGX_CONF_ERROR; |
1258 } | |
1259 | |
1260 if (log->syslog_peer != NULL) { | |
1261 if (cf->args->nelts > 3) { | |
1262 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1263 "parameter \"%V\" is not supported by syslog", | |
1264 &value[3]); | |
1265 return NGX_CONF_ERROR; | |
1266 } | |
1267 | |
1268 return NGX_CONF_OK; | |
1200 } | 1269 } |
1201 | 1270 |
1202 size = 0; | 1271 size = 0; |
1203 flush = 0; | 1272 flush = 0; |
1204 gzip = 0; | 1273 gzip = 0; |