# Disable RackLogger in favor of manually hooking into actions' "handle" for logging requests

**URL:** https://discourse.hanakai.org/t/disable-racklogger-in-favor-of-manually-hooking-into-actions-handle-for-logging-requests/1132
**Category:** questions
**Created:** [February 24, 2025, 7:22pm UTC](https://discourse.hanakai.org/t/disable-racklogger-in-favor-of-manually-hooking-into-actions-handle-for-logging-requests/1132 "2025-02-24T19:22:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Drowze](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/drowze/32/814_2.png) [@Drowze](https://discourse.hanakai.org/u/Drowze)
#### Post date: [February 24, 2025, 7:22pm UTC](https://discourse.hanakai.org/t/disable-racklogger-in-favor-of-manually-hooking-into-actions-handle-for-logging-requests/1132/1 "2025-02-24T19:22:16Z")

</div>

Hello 👋

I’ve been spending a few hours investigating but couldn’t yet find a clean way - is it possible to disable `rack_monitor` completely? My intention is to hook around the `Hanami::Action#call` method, so it’s easier for actions to add some payload to the logging.

A practical example would be adding e.g.: “user\_id: …” to the log payload - and considering that user authentication might happen during the `Hanami::Action#call` lifecycle.

Also for context, here’s how we can do that using Rails:

```ruby
class AuthenticatedController < ApplicationController
  before_action :authenticate!

  def append_info_to_payload(payload)
    super
    payload[:usr] = {
      id: current_user&.id,
      email: current_user&.email
    }
  end

  # implementation details...
  # def authenticate!
  # def current_user
end

class LogSubscriber < ActiveSupport::LogSubscriber
  def process_action(event)
    info { { message: "Processed request", user: event.payload[:usr] }.to_json }
  end
  subscribe_log_level :process_action, :info
end

# detach old log subscriber
ActiveSupport::Notifications.unsubscribe("process_action.action_controller")

# attach my new log subscriber
::LogSubscriber.attach_to :action_controller

```

NOTE: Not directly related to the topic but I think it’s also related - I’m also looking to swap `Dry::Logger` for [`SemanticLogger`](https://github.com/reidmorrison/semantic_logger) (which all of our other services are now using). As of now I’m doing that by:

```ruby
# lib/rack_semantic_logger.rb
# patch Hanami RackLogger so it understands that SemanticLogger is compatible
Hanami::Web::RackLogger::UniversalLogger.singleton_class.prepend(
  Module.new do
    private def compatible_logger?(*)
      true
    end
  end
)
# Rack middleware to add some http-specific tags to all logs
class RackSemanticLogger
  def initialize(app, logger: SemanticLogger[Rack])
    @logger = logger
    @app = app
  end
  def call(env)
    request = Rack::Request.new(env)
    named_tags = {
      url: request.url,
      host: request.host,
      user_agent: request.user_agent,
      # etc...
    }

    @logger.tagged(http: named_tags) { @app.(env) }
  end
end

# config.ru
# NOTE: this is done in config.ru as it was the only way I found to
# wrap rack_monitor logs in my `@logger.tagged { }` block
use RackSemanticLogger

# app configuration in config/app.rb
$stdout.sync = true
config.logger = SemanticLogger[Hanami]
SemanticLogger.default_level = settings.log_level
SemanticLogger.add_appender(io: $stdout, formatter: MyCustomFormatter.new)

```

---

<div class="post-metadata">

### Author: ![madleech](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/madleech/32/840_2.png) [@madleech](https://discourse.hanakai.org/u/madleech)
#### Post date: [March 19, 2025, 6:35am UTC](https://discourse.hanakai.org/t/disable-racklogger-in-favor-of-manually-hooking-into-actions-handle-for-logging-requests/1132/2 "2025-03-19T06:35:59Z")

</div>

I struggled with this too, for exactly the same reasons. I ended up writing a custom formatter for `Dry::Logger` that appends the current request’s context from `RequestStore`.

```ruby
class JSONWithContext < Dry::Logger::Formatters::JSON
  def format(entry)
    RequestStore.store.each do |key, value|
      entry.payload[key] = value
    end
    super
  end
end

```

And then register it with `config.logger.formatter = JSONWithContext` in your `app.rb`.

I should point out that this has the side-effect of appending this context to _all_ your logs emitted during each request, however if you are logging to e.g. Datadog this is actually desirable, as all your logs now have `user_id` etc as attributes.
