Skip to content

Commit fdd604a

Browse files
authored
Support pure json output for audit log (#10143)
1 parent 1776442 commit fdd604a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

ydb/core/audit/audit_log_impl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ TString GetJsonLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
9191
return ss.Str();
9292
}
9393

94+
TString GetJsonLogCompatibleLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
95+
const auto* msg = ev->Get();
96+
NJsonWriter::TBuf json;
97+
{
98+
auto obj = json.BeginObject();
99+
obj
100+
.WriteKey("@timestamp")
101+
.WriteString(msg->Time.ToString().data())
102+
.WriteKey("@log_type")
103+
.WriteString("audit");
104+
105+
for (auto& [k, v] : msg->Parts) {
106+
obj.WriteKey(k).WriteString(v);
107+
}
108+
json.EndObject();
109+
}
110+
return json.Str();
111+
}
112+
94113
TString GetTxtLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
95114
const auto* msg = ev->Get();
96115
TStringStream ss;
@@ -146,6 +165,9 @@ class TAuditLogActor final : public TActor<TAuditLogActor> {
146165
case NKikimrConfig::TAuditConfig::TXT:
147166
WriteLog(GetTxtLog(ev), logBackends.second);
148167
break;
168+
case NKikimrConfig::TAuditConfig::JSON_LOG_COMPATIBLE:
169+
WriteLog(GetJsonLogCompatibleLog(ev), logBackends.second);
170+
break;
149171
default:
150172
WriteLog(GetJsonLog(ev), logBackends.second);
151173
break;

ydb/core/protos/config.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,9 @@ message TMeteringConfig {
14381438

14391439
message TAuditConfig {
14401440
enum EFormat {
1441-
JSON = 1;
1442-
TXT = 2;
1441+
JSON = 1; // Outputs audit log in format: "<time>: {"k1": "v1", "k2": "v2", ...}" where <time> is ISO 8601 format time string, k1, k2, ..., kn - fields of audit log message and v1, v2, ..., vn are their values
1442+
TXT = 2; // Outputs audit log in format: "<time>: k1=v1, k2=v2, ..." where <time> is ISO 8601 format time string, k1, k2, ..., kn - fields of audit log message and v1, v2, ..., vn are their values
1443+
JSON_LOG_COMPATIBLE = 3; // Outputs audit log in format: "{"@timestamp": "<ISO 8601 time>", "@log_type": "audit", "k1": "v1", "k2": "v2", ...}" where @timestamp is ISO 8601 format time string, k1, k2, ..., kn - fields of audit log message and v1, v2, ..., vn are their values // Suitable for output both debug log and audit log to the same destination (stderr)
14431444
}
14441445

14451446
message TStderrBackend {

ydb/library/actors/core/log.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ namespace NActors {
486486
j.BeginObject()
487487
.WriteKey("@timestamp")
488488
.WriteString(Settings->UseLocalTimestamps ? FormatLocalTimestamp(time, buf) : time.ToString().data())
489+
.WriteKey("@log_type")
490+
.WriteString("debug")
489491
.WriteKey("microseconds")
490492
.WriteULongLong(time.MicroSeconds())
491493
.WriteKey("host")

0 commit comments

Comments
 (0)