Skip to content

feat: json log format #1791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ configEnum propagation_error_behavior_enum[] = {
{"panic-on-replicas", PROPAGATION_ERR_BEHAVIOR_PANIC_ON_REPLICAS},
{NULL, 0}};

configEnum log_format_enum[] = {{"legacy", LOG_FORMAT_LEGACY}, {"logfmt", LOG_FORMAT_LOGFMT}, {NULL, 0}};
configEnum log_format_enum[] = {{"legacy", LOG_FORMAT_LEGACY}, {"logfmt", LOG_FORMAT_LOGFMT}, {"json", LOG_FORMAT_JSON}, {NULL, 0}};

configEnum log_timestamp_format_enum[] = {{"legacy", LOG_TIMESTAMP_LEGACY},
{"iso8601", LOG_TIMESTAMP_ISO8601},
Expand Down
7 changes: 5 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ void serverLogRaw(int level, const char *msg) {
const char *verbose_level[] = {"debug", "info", "notice", "warning"};
const char *roles[] = {"sentinel", "RDB/AOF", "replica", "primary"};
const char *role_chars = "XCSM";
const char *json_format = "{\"pid\":%d,\"role\":\"%s\",\"timestamp\":\"%s\",\"level\":\"%s\",\"message\":\"%s\"}\n";
const char *logfmt_format = "pid=%d role=%s timestamp=\"%s\" level=%s message=\"%s\"\n";
FILE *fp;
char buf[64];
int rawmode = (level & LL_RAW);
Expand Down Expand Up @@ -229,13 +231,14 @@ void serverLogRaw(int level, const char *msg) {
}
switch (server.log_format) {
case LOG_FORMAT_LOGFMT:
case LOG_FORMAT_JSON:
if (hasInvalidLogfmtChar(msg)) {
char safemsg[LOG_MAX_LEN];
filterInvalidLogfmtChar(safemsg, LOG_MAX_LEN, msg);
fprintf(fp, "pid=%d role=%s timestamp=\"%s\" level=%s message=\"%s\"\n", (int)getpid(), roles[role_index],
fprintf(fp, server.log_format == LOG_FORMAT_LOGFMT ? logfmt_format : json_format, (int)getpid(), roles[role_index],
buf, verbose_level[level], safemsg);
} else {
fprintf(fp, "pid=%d role=%s timestamp=\"%s\" level=%s message=\"%s\"\n", (int)getpid(), roles[role_index],
fprintf(fp, server.log_format == LOG_FORMAT_LOGFMT ? logfmt_format : json_format, (int)getpid(), roles[role_index],
buf, verbose_level[level], msg);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ typedef enum {

/* Sets log format */
typedef enum { LOG_FORMAT_LEGACY = 0,
LOG_FORMAT_LOGFMT } log_format_type;
LOG_FORMAT_LOGFMT,
LOG_FORMAT_JSON } log_format_type;

/* Sets log timestamp format */
typedef enum { LOG_TIMESTAMP_LEGACY = 0,
Expand Down
1 change: 1 addition & 0 deletions valkey.conf
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ loglevel notice
#
# - legacy: the default, traditional log format
# - logfmt: a structured log format; see https://www.brandur.org/logfmt
# - json: a structured log format; see https://json.org/
#
# log-format legacy

Expand Down
Loading