How can I make DBI log all queries including params?

自古美人都是妖i 提交于 2019-12-10 20:56:51

问题


I am trying to figure out what a legacy app does to our mysql databases (plural) as I need to refactor/replace a part of it completely without caring how it does it. I'm just interested in the result.

Currently I am working with DBI::Profile and DBI::ProfileDumper by adding $ENV{DBI_PROFILE} = "!Statement/DBI::ProfileDumper";. That shows me a bunch of queries when running dbiprof dbi.prof on the commandline, but it seems a little odd.

Might be I am reading the report wrong.

#####[ 3 ]###########################################################
  Count         : 4
  Total Time    : 0.018930 seconds
  Longest Time  : 0.018881 seconds
  Shortest Time : 0.000005 seconds
  Average Time  : 0.004732 seconds
  Key 1         :

INSERT INTO orders (foo, bar) VALUES (1, 2)

Why is it showing 4 as Count? It is creating two rows in the orders table, not 4. Also, it is showing the values of the first insert it is doing. For some others, it keeps the ? in the query string.

Maybe profiling is the wrong approach. I've been thinking about monkey-patching DBI to log the query, but I am not sure where to go with that. Might be I could add a logging function to prepare, and to execute to get the query strings as well as the parameters.

Is there something similar already that I can use? Surely someone wanted to do this? Or maybe another approach?

I'm open for any kind of suggestion.


回答1:


You want to use tracing at level 2: https://metacpan.org/pod/DBI#TRACING




回答2:


DBI::Log is a great simple solution to this. My favourite thing about it is you can invoke it on the command line like:

perl -MDBI::Log script.pl

..and it'll output directly to STDERR, without requiring any modifications to your code.




回答3:


You'll get more meaningful tracing which is also more flexible with DBIx::Log4perl or the slightly more modern DBIx::LogAny.



来源:https://stackoverflow.com/questions/19703521/how-can-i-make-dbi-log-all-queries-including-params

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!