Skip to content

Commit 62a28fd

Browse files
committed
fix(logging): use local log schema
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent f05f0df commit 62a28fd

4 files changed

Lines changed: 31 additions & 49 deletions

File tree

‎src/foundation/log.c‎

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,6 @@ static const char *level_str(CBMLogLevel level) {
111111
}
112112
}
113113

114-
static const char *severity_str(CBMLogLevel level) {
115-
switch (level) {
116-
case CBM_LOG_DEBUG:
117-
return "DEBUG";
118-
case CBM_LOG_INFO:
119-
return "INFO";
120-
case CBM_LOG_WARN:
121-
return "WARNING";
122-
case CBM_LOG_ERROR:
123-
return "ERROR";
124-
default:
125-
return "DEFAULT";
126-
}
127-
}
128-
129114
static void append_char(char *buf, size_t bufsz, size_t *pos, char ch) {
130115
if (*pos < bufsz - 1) {
131116
buf[*pos] = ch;
@@ -231,10 +216,8 @@ void cbm_log(CBMLogLevel level, const char *msg, ...) {
231216
va_start(args, msg);
232217

233218
if (g_log_format == CBM_LOG_FORMAT_JSON) {
234-
append_raw(line_buf, sizeof(line_buf), &pos, "{\"severity\":");
235-
append_json_string(line_buf, sizeof(line_buf), &pos, severity_str(level));
236-
append_raw(line_buf, sizeof(line_buf), &pos, ",\"message\":");
237-
append_json_string(line_buf, sizeof(line_buf), &pos, msg ? msg : "");
219+
append_raw(line_buf, sizeof(line_buf), &pos, "{\"level\":");
220+
append_json_string(line_buf, sizeof(line_buf), &pos, level_str(level));
238221
append_raw(line_buf, sizeof(line_buf), &pos, ",\"event\":");
239222
append_json_string(line_buf, sizeof(line_buf), &pos, msg ? msg : "");
240223
for (;;) {
@@ -299,13 +282,13 @@ void cbm_log_mcp_request(const char *method, const char *tool_name, bool is_erro
299282
char duration_ms[CBM_SZ_32];
300283
snprintf(duration_ms, sizeof(duration_ms), "%" PRId64, duration_us / 1000);
301284
if (tool_name && tool_name[0] != '\0') {
302-
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "rpc.system", "jsonrpc",
303-
"rpc.method", method ? method : "", "mcp.tool.name", tool_name, "status",
285+
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "protocol", "jsonrpc",
286+
"method", method ? method : "", "tool", tool_name, "status",
304287
is_error ? "error" : "ok", "duration_ms", duration_ms, NULL);
305288
} else {
306-
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "rpc.system", "jsonrpc",
307-
"rpc.method", method ? method : "", "status", is_error ? "error" : "ok",
308-
"duration_ms", duration_ms, NULL);
289+
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "protocol", "jsonrpc",
290+
"method", method ? method : "", "status", is_error ? "error" : "ok", "duration_ms",
291+
duration_ms, NULL);
309292
}
310293
}
311294

@@ -329,8 +312,7 @@ void cbm_log_http_request(const char *component, const char *method, const char
329312
level = CBM_LOG_WARN;
330313
}
331314

332-
cbm_log(level, "http.request", "component", component ? component : "", "http.request.method",
333-
method ? method : "", "url.path", safe_path, "http.response.status_code", status_buf,
334-
"duration_ms", duration_buf, "http.request.body.size", request_buf,
335-
"http.response.body.size", response_buf, NULL);
315+
cbm_log(level, "http.request", "component", component ? component : "", "method",
316+
method ? method : "", "path", safe_path, "status", status_buf, "duration_ms",
317+
duration_buf, "request_bytes", request_buf, "response_bytes", response_buf, NULL);
336318
}

‎tests/test_httpd.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,9 @@ TEST(ui_server_access_log_redacts_query) {
569569

570570
ASSERT_NOT_NULL(strstr(httpd_log_buf, "msg=http.request"));
571571
ASSERT_NOT_NULL(strstr(httpd_log_buf, "component=graph_ui"));
572-
ASSERT_NOT_NULL(strstr(httpd_log_buf, "http.request.method=GET"));
573-
ASSERT_NOT_NULL(strstr(httpd_log_buf, "url.path=/definitely/not/here"));
574-
ASSERT_NOT_NULL(strstr(httpd_log_buf, "http.response.status_code=404"));
572+
ASSERT_NOT_NULL(strstr(httpd_log_buf, "method=GET"));
573+
ASSERT_NOT_NULL(strstr(httpd_log_buf, "path=/definitely/not/here"));
574+
ASSERT_NOT_NULL(strstr(httpd_log_buf, "status=404"));
575575
ASSERT_NULL(strstr(httpd_log_buf, "token"));
576576
ASSERT_NULL(strstr(httpd_log_buf, "secret"));
577577
PASS();

‎tests/test_log.c‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ TEST(log_json_output) {
126126
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
127127
cbm_log_set_level(CBM_LOG_INFO);
128128

129-
ASSERT(cbm_str_contains_raw(output, "\"severity\":\"INFO\""));
130-
ASSERT(cbm_str_contains_raw(output, "\"message\":\"test.msg\""));
129+
ASSERT(cbm_str_contains_raw(output, "\"level\":\"info\""));
130+
ASSERT(cbm_str_contains_raw(output, "\"event\":\"test.msg\""));
131131
ASSERT(cbm_str_contains_raw(output, "\"key1\":\"val1\""));
132132
ASSERT(cbm_str_contains_raw(output, "\"key2\":\"line\\nbreak\""));
133133
PASS();
@@ -174,12 +174,13 @@ TEST(log_operational_helpers) {
174174
cbm_log_set_level(CBM_LOG_INFO);
175175

176176
ASSERT(cbm_str_contains_raw(output, "msg=mcp.request"));
177-
ASSERT(cbm_str_contains_raw(output, "rpc.method=tools/call"));
178-
ASSERT(cbm_str_contains_raw(output, "mcp.tool.name=search_graph"));
177+
ASSERT(cbm_str_contains_raw(output, "protocol=jsonrpc"));
178+
ASSERT(cbm_str_contains_raw(output, "method=tools/call"));
179+
ASSERT(cbm_str_contains_raw(output, "tool=search_graph"));
179180
ASSERT(cbm_str_contains_raw(output, "msg=http.request"));
180-
ASSERT(cbm_str_contains_raw(output, "http.request.method=GET"));
181-
ASSERT(cbm_str_contains_raw(output, "url.path=/api/layout"));
182-
ASSERT(cbm_str_contains_raw(output, "http.response.status_code=200"));
181+
ASSERT(cbm_str_contains_raw(output, "method=GET"));
182+
ASSERT(cbm_str_contains_raw(output, "path=/api/layout"));
183+
ASSERT(cbm_str_contains_raw(output, "status=200"));
183184
PASS();
184185
}
185186

@@ -197,16 +198,16 @@ TEST(log_format_from_env) {
197198
PASS();
198199
}
199200

200-
TEST(log_format_is_explicit_only) {
201+
TEST(log_format_unset_keeps_current) {
201202
cbm_unsetenv("CBM_LOG_FORMAT");
202-
cbm_setenv("ENVIRONMENT", "production", 1);
203-
cbm_setenv("K_SERVICE", "cbm", 1);
203+
cbm_log_set_format(CBM_LOG_FORMAT_JSON);
204+
cbm_log_init_from_env();
205+
ASSERT_EQ(cbm_log_get_format(), CBM_LOG_FORMAT_JSON);
206+
204207
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
205208
cbm_log_init_from_env();
206209
ASSERT_EQ(cbm_log_get_format(), CBM_LOG_FORMAT_TEXT);
207210

208-
cbm_unsetenv("ENVIRONMENT");
209-
cbm_unsetenv("K_SERVICE");
210211
PASS();
211212
}
212213

@@ -294,7 +295,7 @@ SUITE(log) {
294295
RUN_TEST(log_sink_tee_keeps_stderr);
295296
RUN_TEST(log_operational_helpers);
296297
RUN_TEST(log_format_from_env);
297-
RUN_TEST(log_format_is_explicit_only);
298+
RUN_TEST(log_format_unset_keeps_current);
298299
RUN_TEST(log_level_from_env_textual);
299300
RUN_TEST(log_level_from_env_numeric);
300301
RUN_TEST(log_level_from_env_invalid_ignored);

‎tests/test_mcp.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ TEST(server_handle_logs_request_without_params) {
422422

423423
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
424424
char *resp =
425-
cbm_mcp_server_handle(srv,
426-
"{\"jsonrpc\":\"2.0\",\"id\":210,\"method\":\"tools/list\","
427-
"\"params\":{\"token\":\"secret\"}}");
425+
cbm_mcp_server_handle(srv, "{\"jsonrpc\":\"2.0\",\"id\":210,\"method\":\"tools/list\","
426+
"\"params\":{\"token\":\"secret\"}}");
428427
ASSERT_NOT_NULL(resp);
429428
free(resp);
430429
cbm_mcp_server_free(srv);
@@ -433,8 +432,8 @@ TEST(server_handle_logs_request_without_params) {
433432
cbm_log_set_level(prev_level);
434433

435434
ASSERT_NOT_NULL(strstr(mcp_log_buf, "msg=mcp.request"));
436-
ASSERT_NOT_NULL(strstr(mcp_log_buf, "rpc.system=jsonrpc"));
437-
ASSERT_NOT_NULL(strstr(mcp_log_buf, "rpc.method=tools/list"));
435+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "protocol=jsonrpc"));
436+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "method=tools/list"));
438437
ASSERT_NOT_NULL(strstr(mcp_log_buf, "status=ok"));
439438
ASSERT_NULL(strstr(mcp_log_buf, "token"));
440439
ASSERT_NULL(strstr(mcp_log_buf, "secret"));

0 commit comments

Comments
 (0)