AugustNagro / magnum

A 'new look' for database access in Scala
Apache License 2.0
153 stars 10 forks source link

How to log SQL queries, or log slow queries? #32

Open adamw opened 1 month ago

adamw commented 1 month ago

Is there a way to (configurably) log the generated SQL queries, or log queries which are slow to execute?

There are some logSql functions in util, but they seem unused.

AugustNagro commented 1 month ago

Yes; you should be able to set your log level to DEBUG, which will log all sql queries. Please let me know if that's not working.

https://github.com/AugustNagro/magnum?tab=readme-ov-file#logging-sql-queries

logSql is used here for example:

https://github.com/AugustNagro/magnum/blob/master/magnum/src/main/scala/com/augustnagro/magnum/Query.scala

On Fri, Jul 26, 2024, 9:53 AM Adam Warski @.***> wrote:

Is there a way to (configurably) log the generated SQL queries, or log queries which are slow to execute?

There are some logSql functions in util, but they seem unused.

— Reply to this email directly, view it on GitHub https://github.com/AugustNagro/magnum/issues/32, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQOKNTUF65YJDJRJOXQRTDZOJPHPAVCNFSM6AAAAABLQVQJIWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQZTENBRGM3TOMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

adamw commented 1 month ago

Ah! Finally got it to work. Turns out that private val Log = System.getLogger(getClass.getName) will create a logger for scala.Predef, instead of com.augustnagro.magnum - what I was expecting in my logback config file. I suppose this should rather be the magnum's package?

Also, it would be great to have some kind of "interceptor" - logSql is fine for basic logging, but for more advanced use cases (like logging slow queries), or plugging in some metrics - an interceptor would be needed I think.

AugustNagro commented 1 month ago

create a logger for scala.Predef, instead of com.augustnagro.magnum - what I was expecting in my logback config file. I suppose this should rather be the magnum's package?

Yes.. that's a bug; thanks for raising!

"interceptor"

Open to adding as well. Do you have an example in mind?

On Wed, Jul 31, 2024, 9:13 AM Adam Warski @.***> wrote:

Ah! Finally got it to work. Turns out that private val Log = System.getLogger(getClass.getName) will create a logger for scala.Predef, instead of com.augustnagro.magnum - what I was expecting in my logback config file. I suppose this should rather be the magnum's package?

Also, it would be great to have some kind of "interceptor" - logSql is fine for basic logging, but for more advanced use cases (like logging slow queries), or plugging in some metrics - an interceptor would be needed I think.

— Reply to this email directly, view it on GitHub https://github.com/AugustNagro/magnum/issues/32#issuecomment-2260626990, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQOKNRRZNS3YRARIHGX6OLZPDWJHAVCNFSM6AAAAABLQVQJIWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRQGYZDMOJZGA . You are receiving this because you commented.Message ID: @.***>

adamw commented 1 month ago

I was using Doobie's LogHandler before and it worked well: https://github.com/tpolecat/doobie/blob/d1f84e64b026c0989818546c935c879e5dcc3598/modules/free/src/main/scala/doobie/util/log.scala#L122

So maybe something similar, passed as parameter to transact - just as connectionConfig is passed now?