Bug #120341 Add an option to output slow logs in JSON format
Submitted: 24 Apr 14:47 Modified: 25 Apr 5:43
Reporter: Kento Takeuchi (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Logging Severity:S4 (Feature request)
Version:8.4.4 OS:Linux
Assigned to: CPU Architecture:x86
Tags: Contribution

[24 Apr 14:47] Kento Takeuchi
Description:
This is a feature request.

MySQL currently supports text-based slow logs.
However, in modern monitoring environments—particularly in environments like Kubernetes—treating logs that span multiple lines as a single entry requires specific configuration (e.g., FluentBit’s multiline-parser).
In such environments, proper log collection requires configuration that integrates with sidecars or log collection agents.

However, MySQL itself can significantly reduce this effort by outputting meaningful, structured logs in a single line.

Therefore, I propose adding a feature to output the MySQL slow-log in JSON format.

How to repeat:
This is a feature request.

Suggested fix:
I propose adding the following setting to allow users to change the log format as needed.

Setting: --slow_query_log_format=[TRADITIONAL|JSON]
Specifications:
- TRADITIONAL: Outputs logs in the traditional format.
- JSON: Outputs logs as a single line of JSON.
- This option can only be changed at the GLOBAL level.
- The output file remains the same regardless of the format.
[24 Apr 14:49] Kento Takeuchi
This is a patch

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: slow-log-json.patch (application/octet-stream, text), 16.62 KiB.

[24 Apr 14:50] Kento Takeuchi
If you apply my patch file, you will see the following error log.

```
{"program":"/home/lrf141/mysqlProject/mysql-server/build/runtime_output_directory/mysqld","version":"8.4.4-debug-valgrind","compilation_comment":"Source distribution","tcp_port":"3306", "unix_socket":"/tmp/mysql.sock"}{"ts":"2026-04-24T13:49:46.058096Z","user_host":"root[root] @ localhost [127.0.0.1]","id":"    8","query_time":"10.001393","lock_time":"0.000008","rows_sent":5,"rows_examined":5,"db":"t1","timestamp":"1777038576","query":"select a, sleep(2) from t1"}
{"program":"/home/lrf141/mysqlProject/mysql-server/build/runtime_output_directory/mysqld","version":"8.4.4-debug-valgrind","compilation_comment":"Source distribution","tcp_port":"3306", "unix_socket":"/tmp/mysql.sock"}{"ts":"2026-04-24T13:57:14.188523Z","user_host":"root[root] @ localhost [127.0.0.1]","id":8,"query_time":"5.001653","lock_time":"0.000010","rows_sent":5,"rows_examined":5,"timestamp":1777039029,"query":"select a, sleep(1) from t1.t1"}
{"program":"/home/lrf141/mysqlProject/mysql-server/build/runtime_output_directory/mysqld","version":"8.4.4-debug-valgrind","compilation_comment":"Source distribution","tcp_port":"3306", "unix_socket":"/tmp/mysql.sock"}{"ts":"2026-04-24T14:16:03.752299Z","user_host":"root[root] @ localhost [127.0.0.1]","id":8,"query_time":5013395,"lock_time":40,"rows_sent":5,"rows_examined":5,"timestamp":1777040158,"query":"select a, sleep(1) from t1.t1"}
{"program":"/home/lrf141/mysqlProject/mysql-server/build/runtime_output_directory/mysqld","version":"8.4.4-debug-valgrind","compilation_comment":"Source distribution","tcp_port":"3306", "unix_socket":"/tmp/mysql.sock"}{"ts":"2026-04-24T14:22:55.637374Z","user_host":"root[root] @ localhost [127.0.0.1]","id":8,"query_time":5,"lock_time":0,"rows_sent":5,"rows_examined":5,"timestamp":1777040570,"query":"select a, sleep(1) from t1.t1"}
{"program":"/home/lrf141/mysqlProject/mysql-server/build/runtime_output_directory/mysqld","version":"8.4.4-debug-valgrind","compilation_comment":"Source distribution","tcp_port":"3306", "unix_socket":"/tmp/mysql.sock"}{"ts":"2026-04-24T14:25:02.748822Z","user_host":"root[root] @ localhost [127.0.0.1]","id":8,"query_time":5.020070,"lock_time":0.000055,"rows_sent":5,"rows_examined":5,"timestamp":1777040697,"query":"select a, sleep(1) from t1.t1"}
```

This output can be parsed using jq.
[24 Apr 15:09] Kento Takeuchi
Some debug logs (intermediate patch states) have been included, but the last two entries are logs generated by this patch.
[28 Apr 3:51] Kento Takeuchi
fix bug

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: slow-log-json.patch (application/octet-stream, text), 17.25 KiB.