# Suppressing SQL logging in production

**URL:** <https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080>\
**Category:** questions\
**Created:** [December 10, 2024, 5:31am UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080 "2024-12-10T05:31:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 10, 2024, 5:31am UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/1 "2024-12-10T05:31:24Z")

</div>

Hi everyone.

I would like to choose logger for production.  
I see sql queries in logs and it’s not good.  
I use lograge for rails application but, as I understand, it’s not usable for hanami.  
Сan you give me advice for to config logger for prod correctly?

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 11, 2024, 6:07am UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/2 "2024-12-11T06:07:45Z")

</div>

I checked logger

```ruby
 app["db.config"].gateways[:default].logger
=> nil

```

it is nil

But I see messages like

```auto
Loaded :postgres in 4ms INSERT INTO

```

As I understand

```ruby
Dry::Monitor::SQL::Logger.config

```

is responsible for this message

```auto
=> #<Dry::Configurable::Config values={:message_template=>" Loaded %s in %sms %s", :theme=>nil}>

```

is it possible to disable db\_logging?

```auto
lib/hanami/providers/db_logging.rb

```

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [December 11, 2024, 12:27pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/3 "2024-12-11T12:27:51Z")

</div>

Hi @vladimirtcats, this is an interesting one. You’ve certainly unearthed some things:

- DB logs in production mode are not JSON-formatted, like we default to for other kinds of logs
- There’s no obvious way to disable DB logs in production

We’ll have to look at ways to resolve this.

In the meantime, I reckon we can come up with some monkey patches that will at least allow you to disable production DB logging in the meanwhile.

I think it’ll come down to overriding `Dry::Monitor::SQL::Logger#log_query` in the production environment, so that the method does nothing. If that’s enough of a lead for you, please feel free to have a go!

If not, then give me a day or so to try a few things and get back to you.

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 11, 2024, 12:32pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/4 "2024-12-11T12:32:08Z")

</div>

> [@timriley](#):
>
> Dry::Monitor::SQL::Logger#log\_query

Thank for your reply!

I also thought about monkey patching

```ruby
Dry::Monitor::SQL::Logger#log_query

```

but I thought that I was in the wrong way

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 11, 2024, 12:47pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/5 "2024-12-11T12:47:35Z")

</div>

Can you give me advice how to correctly apply monkey patch for hanami?

As I understand, I can put patch in lib folder and require this patch in app.rb file

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [December 11, 2024, 1:06pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/6 "2024-12-11T13:06:22Z")

</div>

> [@vladimirtcats](#):
>
> I also thought about monkey patching `Dry::Monitor::SQL::Logger#log_query` but I thought that I was in the wrong way

Well, you’re not wrong 🙂 It’s not a clean long-term solution. But I think it should work! And at this stage I’m just looking for ways to unblock you, which will also allow us the time we need to figure out what that good long-term solution should look like.

> [@vladimirtcats](#):
>
> Can you give me advice how to correctly apply monkey patch for hanami?
> 
> As I understand, I can put patch in lib folder and require this patch in app.rb file

Yep, pretty much this.

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 11, 2024, 1:10pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/7 "2024-12-11T13:10:40Z")

</div>

I tried to apply patch but without success

```ruby
# lib/sql_monkey_patching.rb
# frozen_string_literal: true

module Dry
  module Monitor
    module SQL
      class Logger
        def log_query(time:, name:, query:); end
      end
    end
  end
end

```

config/app.rb

```ruby
require_relative "../lib/sql_monkey_patching"

```

it seems to me that my patch is overriden in some stage of loading app

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [December 11, 2024, 1:36pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/8 "2024-12-11T13:36:22Z")

</div>

Your patch file will want to make sure that the relevant dry-monitor class is loaded first. This is how you ensure that your version of the method ends up “winning”.

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 11, 2024, 1:45pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/9 "2024-12-11T13:45:34Z")

</div>

> [@vladimirtcats](#):
>
> `require_relative "../lib/sql_monkey_patching"`

I tried to add `require 'dry-monitor' ` in patch but result was the same

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/timriley/32/621_2.png) [@timriley](https://discourse.hanakai.org/u/timriley)\
**Post date:** [December 11, 2024, 9:44pm UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/10 "2024-12-11T21:44:48Z")

</div>

You’re getting close! But because the dry-monitor classes are autoloaded via Zeitwerk, a single `require "dry-monitor"` isn’t quite enough. You’ll want one of these, I think:

```ruby
require "dry/monitor"
require "dry/monitor/sql/logger"

module Dry
  module Monitor
    module SQL
      class Logger
        def log_query(time:, name:, query:); end
      end
    end
  end
end

```

or:

```ruby
require "dry/monitor"

Dry::Monitor::SQL::Logger.class_eval do
  def log_query(time:, name:, query:); end
end

```

The second is probably the nicer approach for a patch. It will give you early feedback e.g. if for any reason the full `Dry::Monitor::SQL::Logger` class is no longer available, whereas the code in first approach would end up continuing to execute (but do nothing) in the case of the underlying dry-monitor classes changing.

---

<div class="post-metadata">

**Author:** ![vladimirtcats](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/vladimirtcats/32/784_2.png) [@vladimirtcats](https://discourse.hanakai.org/u/vladimirtcats)\
**Post date:** [December 12, 2024, 5:16am UTC](https://discourse.hanakai.org/t/suppressing-sql-logging-in-production/1080/11 "2024-12-12T05:16:38Z")

</div>

Thank for your help!  
the final code for this patch is:

```ruby
require "dry-monitor"
require "dry/monitor/sql/logger"

Dry::Monitor::SQL::Logger.class_eval do
  def log_query(time:, name:, query:); end
end

```
