Skip to content

Format SQL query logs with DECLARE statements and readable multi-line output #168

@yash-sharma-spraxa

Description

@yash-sharma-spraxa

When SQL queries are logged (slow queries, errors, etc.), the output is currently hard-to-read pino JSON with escaped newlines/tabs. It should instead print as proper multi-line SQL with the parameters declared at the top.

  • Current Output (from logs)
"query":"SELECT *\nFROM Table\tWHERE Id = @Id",
"parameters":{"Id":{"name":"Id","io":1,"value":5}}
  • Desired Output
DECLARE @Id INT = 5

SELECT *
FROM Table WHERE Id = @Id
  • What Changes Are Needed

Create a formatting helper that takes { query, parameters } and returns a string like:

DECLARE @Param1 TYPE1 = Value1
DECLARE @Param2 TYPE2 = Value2

SELECT ...
  1. Map mssql type constructors (Int, VarChar, DateTime2, Bit, etc.) to their SQL type names.
  2. Handle string quoting ('value'), NULL (= NULL), and date formatting in DECLARE lines.
  3. Preserve actual newlines/tabs in the query (don't JSON-escape them).
  4. Apply the helper at all 3 logging sites in the MSSQL module — slow query threshold logger, max query time warning logger, and query error logger.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions