Skip to content

Commit

Permalink
- Fix #383: log timestamps in ISO8601 format with timezone.
Browse files Browse the repository at this point in the history
  This adds the option `log-time-iso: yes` that logs in ISO8601
  format.
  • Loading branch information
wcawijngaards committed Sep 25, 2024
1 parent 5e4e809 commit b0b93ed
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 8 deletions.
1 change: 1 addition & 0 deletions configlexer.lex
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ dnstap-version{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_DNSTAP_VERSION; }
dnstap-log-auth-query-messages{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_DNSTAP_LOG_AUTH_QUERY_MESSAGES; }
dnstap-log-auth-response-messages{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_DNSTAP_LOG_AUTH_RESPONSE_MESSAGES; }
log-time-ascii{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_LOG_TIME_ASCII;}
log-time-iso{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_LOG_TIME_ISO;}
round-robin{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_ROUND_ROBIN;}
minimal-responses{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_MINIMAL_RESPONSES;}
confine-to-zone{COLON} { LEXOUT(("v(%s) ", yytext)); return VAR_CONFINE_TO_ZONE;}
Expand Down
6 changes: 6 additions & 0 deletions configparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct component {
%token VAR_STATISTICS
%token VAR_XFRD_RELOAD_TIMEOUT
%token VAR_LOG_TIME_ASCII
%token VAR_LOG_TIME_ISO
%token VAR_ROUND_ROBIN
%token VAR_MINIMAL_RESPONSES
%token VAR_CONFINE_TO_ZONE
Expand Down Expand Up @@ -457,6 +458,11 @@ server_option:
cfg_parser->opt->log_time_ascii = $2;
log_time_asc = cfg_parser->opt->log_time_ascii;
}
| VAR_LOG_TIME_ISO boolean
{
cfg_parser->opt->log_time_iso = $2;
log_time_iso = cfg_parser->opt->log_time_iso;
}
| VAR_ROUND_ROBIN boolean
{
cfg_parser->opt->round_robin = $2;
Expand Down
5 changes: 5 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
25 September 2024: Wouter
- Fix #383: log timestamps in ISO8601 format with timezone.
This adds the option `log-time-iso: yes` that logs in ISO8601
format.

23 August 2024: Wouter
- Merge #376: Point the user towards tcpdump for logging individual
queries.
Expand Down
3 changes: 3 additions & 0 deletions doc/RELNOTES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ NSD RELEASE NOTES
================
FEATURES:
- Support reloading configuration on SIGHUP.
- Fix #383: log timestamps in ISO8601 format with timezone.
This adds the option `log-time-iso: yes` that logs in ISO8601
format.
BUG FIXES:
- Fix title underline and declaration after statement warnings.
- Add cross platform freebsd, openbsd and netbsd to github ci.
Expand Down
2 changes: 2 additions & 0 deletions nsd-checkconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ config_print_zone(nsd_options_type* opt, const char* k, int s, const char *o,
SERV_GET_BIN(reload_config, o);
SERV_GET_BIN(zonefiles_check, o);
SERV_GET_BIN(log_time_ascii, o);
SERV_GET_BIN(log_time_iso, o);
SERV_GET_BIN(round_robin, o);
SERV_GET_BIN(minimal_responses, o);
SERV_GET_BIN(confine_to_zone, o);
Expand Down Expand Up @@ -672,6 +673,7 @@ config_test_print_server(nsd_options_type* opt)
print_string_var("xfrdir:", opt->xfrdir);
printf("\txfrd-reload-timeout: %d\n", opt->xfrd_reload_timeout);
printf("\tlog-time-ascii: %s\n", opt->log_time_ascii?"yes":"no");
printf("\tlog-time-iso: %s\n", opt->log_time_iso?"yes":"no");
printf("\tround-robin: %s\n", opt->round_robin?"yes":"no");
printf("\tminimal-responses: %s\n", opt->minimal_responses?"yes":"no");
printf("\tconfine-to-zone: %s\n",
Expand Down
4 changes: 4 additions & 0 deletions nsd.conf.5.in
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ Log time in ascii, if "no" then in seconds epoch. Default is yes.
This chooses the format when logging to file. The printout via syslog
has a timestamp formatted by syslog.
.TP
.B log\-time\-iso:\fR <yes or no>
Log time in ISO8601 format, if \fBlog\-time\-ascii:\fR yes is also set.
Default is no.
.TP
.B round\-robin:\fR <yes or no>
Enable round robin rotation of records in the answer. This changes the
order of records in the answer and this may balance load across them.
Expand Down
4 changes: 4 additions & 0 deletions nsd.conf.sample.in
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ server:
# log timestamp in ascii (y-m-d h:m:s.msec), yes is default.
# log-time-ascii: yes

# log timestamp in ISO8601 format if also log-time-ascii is enabled.
# (y-m-dTh:m:s.msec[+-]tzhours:tzminutes)
# log-time-iso: no

# round robin rotation of records in the answer.
# round-robin: no

Expand Down
1 change: 1 addition & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct nsd_options {
int zonefiles_check;
int zonefiles_write;
int log_time_ascii;
int log_time_iso;
int round_robin;
int minimal_responses;
int refuse_any;
Expand Down
6 changes: 6 additions & 0 deletions tpkg/checkconf.tdir/checkconf.check
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -176,6 +177,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -253,6 +255,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: no
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -339,6 +342,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -469,6 +473,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -597,6 +602,7 @@ server:
xfrdir: "/tmp"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down
6 changes: 6 additions & 0 deletions tpkg/checkconf.tdir/checkconf.check2.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -176,6 +177,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -253,6 +255,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: no
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -339,6 +342,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -469,6 +473,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down Expand Up @@ -597,6 +602,7 @@ server:
xfrdir: "@xfrdir@"
xfrd-reload-timeout: 1
log-time-ascii: yes
log-time-iso: no
round-robin: no
minimal-responses: no
confine-to-zone: no
Expand Down
41 changes: 33 additions & 8 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static const char *global_ident = NULL;
static log_function_type *current_log_function = log_file;
static FILE *current_log_file = NULL;
int log_time_asc = 1;
int log_time_iso = 0;

#ifdef USE_LOG_PROCESS_ROLE
void
Expand Down Expand Up @@ -160,15 +161,39 @@ log_file(int priority, const char *message)
char tmbuf[32];
tmbuf[0]=0;
tv.tv_usec = 0;
if(gettimeofday(&tv, NULL) == 0) {
struct tm tm;
time_t now = (time_t)tv.tv_sec;
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S",
localtime_r(&now, &tm));
if(log_time_iso) {
char tzbuf[16];
tzbuf[0]=0;
/* log time in iso format */
if(gettimeofday(&tv, NULL) == 0) {
struct tm tm, *tm_p;
time_t now = (time_t)tv.tv_sec;
tm_p = localtime_r(&now, &tm);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%dT%H:%M:%S",
tm_p);
if(strftime(tzbuf, sizeof(tzbuf), "%z", tm_p) == 5) {
/* put ':' in "+hh:mm" */
tzbuf[5] = tzbuf[4];
tzbuf[4] = tzbuf[3];
tzbuf[3] = ':';
tzbuf[6] = 0;
}
}
fprintf(current_log_file, "%s.%3.3d%s %s[%d]: %s: %s",
tmbuf, (int)tv.tv_usec/1000, tzbuf,
global_ident, (int) getpid(), priority_text, message);
} else {
/* log time in ascii format */
if(gettimeofday(&tv, NULL) == 0) {
struct tm tm;
time_t now = (time_t)tv.tv_sec;
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S",
localtime_r(&now, &tm));
}
fprintf(current_log_file, "[%s.%3.3d] %s[%d]: %s: %s",
tmbuf, (int)tv.tv_usec/1000,
global_ident, (int) getpid(), priority_text, message);
}
fprintf(current_log_file, "[%s.%3.3d] %s[%d]: %s: %s",
tmbuf, (int)tv.tv_usec/1000,
global_ident, (int) getpid(), priority_text, message);
} else
#endif /* have time functions */
fprintf(current_log_file, "[%d] %s[%d]: %s: %s",
Expand Down
3 changes: 3 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ extern int nsd_debug_level;
/* set to true to log time prettyprinted, or false to print epoch */
extern int log_time_asc;

/* set to true to log time in iso format */
extern int log_time_iso;

/*
* Timespec functions.
*/
Expand Down

0 comments on commit b0b93ed

Please sign in to comment.