Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32beb2b

Browse files
committed
ngx-json-log: log to syslog
1 parent 6658d58 commit 32beb2b

File tree

6 files changed

+136
-10
lines changed

6 files changed

+136
-10
lines changed

‎README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ The output format is configurable.
1414

1515
It also allows to log complex and multi-level JSON documents.
1616

17-
It supports logging to text file or to a kafka topic.
17+
It supports logging to text file, to syslog or to a kafka topic.
1818

1919
It supports multiple output destinations with multiple formats for a location.
2020

2121
### Current version and limitations
2222

23-
Current version released is 0.0.6.
23+
Current version released is 0.0.7.
2424

2525
Stream logging is only available when using nginx (>= 1.11.2).
2626

@@ -118,6 +118,7 @@ Additionally, the variable $http_json_err_log_req will be set with a base64 enco
118118
';
119119
120120
json_log file:/tmp/log my_log;'
121+
json_log syslog:server=unix:/dev/log my_log;'
121122
```
122123

123124
This will produce the following JSON line to '/tmp/log' file .

‎src/ngx_http_json_log_filter_module.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,18 @@ ngx_http_json_log_header_bad_request(ngx_http_request_t *r)
437437
continue;
438438
}
439439

440+
/* Write to syslog */
441+
if (location->type == NGX_JSON_LOG_SINK_SYSLOG) {
442+
if (!location->syslog) {
443+
continue;
444+
}
445+
if (ngx_json_log_write_sink_syslog(r->pool->log,
446+
r->pool, location->syslog, txt) == NGX_ERROR) {
447+
ngx_log_error(NGX_LOG_EMERG, r->pool->log, 0, "Syslog write error!");
448+
}
449+
continue;
450+
}
451+
440452
#if (NGX_HAVE_LIBRDKAFKA)
441453
/* Write to kafka */
442454
if (location->type == NGX_JSON_LOG_SINK_KAFKA) {

‎src/ngx_http_json_log_module.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,19 @@ static ngx_int_t ngx_http_json_log_log_handler(ngx_http_request_t *r)
287287

288288
if (ngx_json_log_write_sink_file(r->pool->log,
289289
location->file->fd, txt) == NGX_ERROR) {
290-
ngx_log_error(NGX_LOG_EMERG, r->pool->log, 0, "Write Error!");
290+
ngx_log_error(NGX_LOG_EMERG, r->pool->log, 0, "File write error!");
291+
}
292+
continue;
293+
}
294+
295+
/* Write to syslog */
296+
if (location->type == NGX_JSON_LOG_SINK_SYSLOG) {
297+
if (!location->syslog) {
298+
continue;
299+
}
300+
if (ngx_json_log_write_sink_syslog(r->pool->log,
301+
r->pool, location->syslog, txt) == NGX_ERROR) {
302+
ngx_log_error(NGX_LOG_EMERG, r->pool->log, 0, "Syslog write error!");
291303
}
292304
continue;
293305
}
@@ -472,6 +484,7 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
472484
#if (NGX_HAVE_LIBRDKAFKA)
473485
&& ! NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)
474486
#endif
487+
&& ! NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)
475488
) {
476489
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
477490
"Invalid prefix [%v] for HTTP log JSON output location", value);
@@ -491,11 +504,13 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
491504
new_location->type = NGX_JSON_LOG_SINK_FILE;
492505
prefix_len = NGX_JSON_LOG_FILE_OUT_LEN;
493506
#if (NGX_HAVE_LIBRDKAFKA)
494-
}
495-
else if (NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
507+
} else if (NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
496508
new_location->type = NGX_JSON_LOG_SINK_KAFKA;
497509
prefix_len = NGX_JSON_LOG_KAFKA_OUT_LEN;
498510
#endif
511+
} else if (NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)) {
512+
new_location->type = NGX_JSON_LOG_SINK_SYSLOG;
513+
prefix_len = NGX_JSON_LOG_SYSLOG_OUT_LEN;
499514
}
500515

501516
/* Saves location without prefix. */
@@ -510,6 +525,18 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
510525
&new_location->location);
511526
}
512527

528+
/* If sink type is syslog */
529+
if (new_location->type == NGX_JSON_LOG_SINK_SYSLOG) {
530+
new_location->syslog = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
531+
if (new_location->syslog == NULL) {
532+
return NGX_CONF_ERROR;
533+
}
534+
535+
if (ngx_syslog_process_conf(cf, new_location->syslog) != NGX_CONF_OK) {
536+
return NGX_CONF_ERROR;
537+
}
538+
}
539+
513540
#if (NGX_HAVE_LIBRDKAFKA)
514541
/* If sink type is kafka, then set topic config for this location */
515542
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {

‎src/ngx_json_log_output.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,43 @@ ngx_json_log_write_sink_file(ngx_log_t *log,
1919
}
2020
return NGX_OK;
2121
}
22+
23+
ngx_int_t
24+
ngx_json_log_write_sink_syslog(ngx_log_t *log,
25+
ngx_pool_t* pool,
26+
ngx_syslog_peer_t *syslog,
27+
const char *txt) {
28+
29+
ssize_t n = 0;
30+
size_t size = 0;
31+
u_char *line, *p;
32+
size_t len = 0;
33+
34+
len += sizeof("<255>Jan 01 00:00:00 ") - 1
35+
+ ngx_cycle->hostname.len + 1
36+
+ syslog->tag.len + 2
37+
+ strlen(txt);
38+
39+
line = ngx_pnalloc(pool, len);
40+
if (line == NULL) {
41+
return NGX_ERROR;
42+
}
43+
44+
p = ngx_syslog_add_header(syslog, line);
45+
p = ngx_snprintf(p, len, "%s", txt);
46+
size = p - line;
47+
n = ngx_syslog_send(syslog, line, size);
48+
49+
if (n < 0) {
50+
ngx_log_error(NGX_LOG_WARN, log, 0,
51+
"send() to syslog failed");
52+
return NGX_ERROR;
53+
54+
} else if ((size_t) n != size) {
55+
ngx_log_error(NGX_LOG_WARN, log, 0,
56+
"send() to syslog has written only %z of %uz",
57+
n, size);
58+
return NGX_ERROR;
59+
}
60+
return NGX_OK;
61+
}

‎src/ngx_json_log_output.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,27 @@
4444
NGX_JSON_LOG_KAFKA_OUT_LEN) == 0 )
4545
#endif
4646

47+
#define NGX_JSON_LOG_SYSLOG_OUT_LEN (sizeof("syslog:") - 1)
48+
#define NGX_JSON_LOG_HAS_SYSLOG_PREFIX(str) \
49+
(ngx_strncmp(str->data, \
50+
"syslog:", \
51+
NGX_JSON_LOG_SYSLOG_OUT_LEN) == 0 )
52+
53+
4754
typedef enum {
4855
NGX_JSON_LOG_SINK_FILE = 0,
56+
NGX_JSON_LOG_SINK_SYSLOG = 1,
4957
#if (NGX_HAVE_LIBRDKAFKA)
50-
NGX_JSON_LOG_SINK_KAFKA = 1
58+
NGX_JSON_LOG_SINK_KAFKA = 2
5159
#endif
5260
} ngx_json_log_sink_e;
5361

5462
struct ngx_json_log_output_location_s {
5563
ngx_str_t location;
5664
ngx_json_log_sink_e type;
5765
ngx_json_log_format_t format;
58-
ngx_open_file_t *file;
66+
ngx_open_file_t *file;
67+
ngx_syslog_peer_t *syslog;
5968
#if (NGX_HAVE_LIBRDKAFKA)
6069
ngx_json_log_kafka_conf_t kafka;
6170
#endif
@@ -67,5 +76,11 @@ ngx_int_t
6776
ngx_json_log_write_sink_file(ngx_log_t *log,
6877
ngx_fd_t fd, const char *txt);
6978

79+
ngx_int_t
80+
ngx_json_log_write_sink_syslog(ngx_log_t *log,
81+
ngx_pool_t* pool,
82+
ngx_syslog_peer_t *syslog,
83+
const char *txt);
84+
7085
#endif // __NGX_JSON_LOG_OUTPUT_H__
7186

‎src/ngx_stream_json_log_module.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,19 @@ ngx_stream_json_log_log_handler(ngx_stream_session_t *s)
237237
if (ngx_json_log_write_sink_file(s->connection->pool->log,
238238
location->file->fd, txt) == NGX_ERROR) {
239239
ngx_log_error(NGX_LOG_EMERG,
240-
s->connection->pool->log, 0, "Write Error!");
240+
s->connection->pool->log, 0, "File write error!");
241+
}
242+
continue;
243+
}
244+
245+
/* Write to syslog */
246+
if (location->type == NGX_JSON_LOG_SINK_SYSLOG) {
247+
if (!location->syslog) {
248+
continue;
249+
}
250+
if (ngx_json_log_write_sink_syslog(s->connection->pool->log,
251+
s->connection->pool,location->syslog, txt) == NGX_ERROR) {
252+
ngx_log_error(NGX_LOG_EMERG, s->connection->pool->log, 0, "Syslog write error!");
241253
}
242254
continue;
243255
}
@@ -393,8 +405,12 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
393405

394406
value = &args[1];
395407

396-
if (! NGX_JSON_LOG_HAS_FILE_PREFIX(value) &&
397-
! NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
408+
if (! NGX_JSON_LOG_HAS_FILE_PREFIX(value)
409+
#if (NGX_HAVE_LIBRDKAFKA)
410+
&& ! NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)
411+
#endif
412+
&& ! NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)
413+
) {
398414
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
399415
"Invalid prefix [%v] for HTTP log JSON output location", value);
400416
return NGX_CONF_ERROR;
@@ -418,6 +434,9 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
418434
new_location->type = NGX_JSON_LOG_SINK_KAFKA;
419435
prefix_len = NGX_JSON_LOG_KAFKA_OUT_LEN;
420436
#endif
437+
} else if (NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)) {
438+
new_location->type = NGX_JSON_LOG_SINK_SYSLOG;
439+
prefix_len = NGX_JSON_LOG_SYSLOG_OUT_LEN;
421440
}
422441

423442
/* Saves location without prefix. */
@@ -432,6 +451,18 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
432451
&new_location->location);
433452
}
434453

454+
/* If sink type is syslog */
455+
if (new_location->type == NGX_JSON_LOG_SINK_SYSLOG) {
456+
new_location->syslog = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
457+
if (new_location->syslog == NULL) {
458+
return NGX_CONF_ERROR;
459+
}
460+
461+
if (ngx_syslog_process_conf(cf, new_location->syslog) != NGX_CONF_OK) {
462+
return NGX_CONF_ERROR;
463+
}
464+
}
465+
435466
#if (NGX_HAVE_LIBRDKAFKA)
436467
/* If sink type is kafka, then set topic config for this location */
437468
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /