When something breaks in production, the first thing I do is pull a SQL statement out of the logs and run it against the database myself.
What I actually get is this:
==> Preparing: SELECT id, username, status FROM user WHERE username = ? AND status = ? AND created_at > ? ==> Parameters: tom(String), 1(Integer), 2026-11-20 10:00:00.0(Timestamp) Three placeholders, three values, and I have to line them up by hand. Add quotes to the string, reformat the timestamp, strip the type annotations. Doing this once is fine. Doing it fifteen times a day is not.
So I wrote a tool to do it for me. I assumed it would be a weekend of regex. It turned into a description of what MyBatis log parsing actually involves, and I want to write down the six places it goes wrong — partly so I remember, partly because anyone else automating this will hit the same walls.
Trap 1: The parameters line often isn't there at all
Preparing: and Parameters: are two separate log statements at two different levels.
Preparing: is DEBUG. Parameters: requires TRACE.
Plenty of projects run at DEBUG for the mapper package, so you see the statement but never the values. No parsing tool can fix this — there is nothing to parse.
The check is one search: look for Parameters: in the log. If zero hits, it's a level problem, not a parser problem. Turn the mapper package up to TRACE in logback.xml or log4j2.xml.
There's a variant of this: the level is fine but another library owns the output. Add p6spy, or a log-shipping agent, and the line may be reformatted or dropped before you ever see it.
Trap 2: The prefix is not consistent
Depending on the environment, the version, and whatever wrapper is in play:
==> Preparing: SELECT ... # standard StdOutImpl Preparing: SELECT ... # bare form ==> Preparing: SELECT ... # single arrow [MyBatis] Preparing: SELECT ... # framework prefix Some teams prepend trace ids or tenant ids, which makes the prefix longer still.
One regex does not cover all of this. The reliable approach is to treat the prefix as optional noise: anchor on Preparing: and Parameters: and skip whatever comes before them. Do not try to match ==> specifically.
Trap 3: Batches don't line up
Single statements are easy. Batch inserts are not:
==> Preparing: INSERT INTO log (a, b, c) VALUES (?, ?, ?) ==> Parameters: 1(Integer), 2(Integer), 3(Integer) ==> Parameters: 4(Integer), 5(Integer), 6(Integer) ==> Parameters: 7(Integer), 8(Integer), 9(Integer) One statement, several parameter lines, each line a separate execution. If your parser takes "the last parameters line", you silently lose the first two rows.
The correct model: consecutive Parameters: lines belong to the same statement as repeated executions. Render them as one batch operation rather than three unrelated INSERTs, otherwise the reader can't tell what happened.
Trap 4: Values contain commas and parentheses
This is where a regex-based parser falls apart.
The parameters line is comma-separated, but values can contain commas:
==> Parameters: 张三,李四(String), 2026-11-20 10:00:00.0(Timestamp) That is one string parameter, not two. Split on comma and it tears in half.
Parentheses are just as bad: a value containing () breaks the (Type) extraction. Quoted strings with escapes break naive regex immediately.
What worked for me was a small state machine: walk the line character by character, track whether you're inside a string and how deep the parentheses go, and only treat a comma as a separator when depth is 0 and you're outside quotes. It's not clever, but it's an order of magnitude more reliable than regex.
Trap 5: null and the string "null" look the same
==> Parameters: null, null(String), null(Integer) Three different things:
-
null— actually null -
null(String)— the four-character string "null" -
null(Integer)— shouldn't exist, but if it does, the type info is not trustworthy
Rendering the first as NULL and the second as 'null' gives completely different results, and neither throws. Silent wrong answers are worse than loud failures.
The only signal you have is whether a type annotation is present.
Trap 6: Strip the type, but keep the format
tom(String) becomes 'tom' — drop the type, add quotes.
But 2026-11-20 10:00:00.0(Timestamp) cannot just be type-stripped: it's a date and must become '2026-11-20 10:00:00'. The trailing .0 is Java's fractional-second placeholder; paste it into a client and some dialects reject it outright.
The same category of problem:
-
true(Boolean)→1orTRUEdepending on dialect -
byte[]parameters → logged as[B@1a2b3c, which is unrecoverable - Large text → truncated to
...(truncated)in the log
Each of these needs explicit handling, or an honest "this one can't be reconstructed" — not a best-guess SQL that happens to be wrong.
Two ways around all of this
Every trap above lives on the "reconstruct SQL from log text" path. You can avoid that path entirely:
Change the data source. Instead of parsing logs, intercept at the JDBC layer. A lightweight Java agent hooks PreparedStatement.execute* and gets the statement with parameters already bound, plus real timing, affected row count and the exception. No log format issues, no level requirements. The cost is that the application has to actually be running, and you authorize the agent once.
Change where you look. Instead of grepping in a terminal, read the SQL inside the IDE. I built an IntelliJ IDEA plugin that parses the console output into runnable SQL, clusters statements with the same shape, highlights slow queries, and jumps back to the source line. Once the app is running, the same plugin shows the JDBC-level capture: real parameters, milliseconds, rows affected, the original exception.
The part that matters day to day: the reconstructed SQL runs from inside the IDE. No copy-paste round trip through a database client.
Boundaries, stated plainly
This solves "turn what's in the log into something a human can use". It does not solve:
- Missing parameters when the log level is too low (no data is no data)
-
byte[]and truncated values - Timing or row counts from the console path (the log doesn't contain them)
- Static preview versus actual execution (static constants get inlined; pagination literals get evaluated)
The plugin is free and open source (Apache-2.0), requires IDEA 2024.3+, and is on the JetBrains Marketplace as MyBatis SQL Visualizer. Docs at sql-visualizer.jvtools.cn.
I'm the author of that plugin. All six traps are ones I actually hit while building the parser, not a generic list of "things that might go wrong". If you've written something similar, you've probably met a few of them — I'd be interested in the shapes I haven't seen yet.


