Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't change Rack formatter in development environment #1280

Open
Drowze opened this issue Jan 20, 2023 · 2 comments
Open

Can't change Rack formatter in development environment #1280

Drowze opened this issue Jan 20, 2023 · 2 comments
Assignees

Comments

@Drowze
Copy link
Contributor

Drowze commented Jan 20, 2023

Developing an application, I was given a task to change the logger formatter, so the logs are correctly consumed by our observability stack. It's also a requirement that production logs and development logs go through the same formatter, with minimal change.
To do that, I created a formatter class, registered it and changed my application formatter. Something along the lines of:

class DatadogLogFormatter < Dry::Logger::Formatters::Structured
  def format(entry)
    # ...
  end
end

Dry::Logger.register_formatter(:datadog, DatadogLogFormatter)

module MyApp
  class App < Hanami::App
    config.logger.formatter = :datadog
  end
end

...but that didn't change the log formatting for rack requests on development/testing, which was very confusing at first... Eventually I found out that it was due to how development logging is setup here, where the constructor is hardcodding the formatter on all logs tagged as rack entries. See:

      def development_logger(_env, app_name, **options)
        Dry.Logger(app_name, **options) do |setup|
          setup
            .add_backend(log_if: -> entry { !entry.tag?(:rack) })
            .add_backend(formatter: :rack, log_if: -> entry { entry.tag?(:rack) })
        end
      end

It's a bit annoying, but for now I've worked around this issue by setting a custom logger_constructor as follows:

config.logger.logger_constructor = -> (_env, app_name, **options) { Dry.Logger(app_name, **options) }
@Drowze Drowze changed the title Changing Rack formatter in development environment CamChanging Rack formatter in development environment Jan 20, 2023
@Drowze Drowze changed the title CamChanging Rack formatter in development environment Can't change Rack formatter in development environment Jan 20, 2023
@jodosha
Copy link
Member

jodosha commented Jan 30, 2023

@Drowze Thanks for reporting. I can reproduce the problem.

@solnic
Copy link
Member

solnic commented Feb 2, 2023

Should we then discard the default dev/test logger whenever a custom formatter is configured? The default dev/test logger is there for convenience, the common use case is that you want a human-friendly logger so that you can look at your dev/test logs. Personally, if I need a custom logger I just add it as an additional logger, rather than changing the defaults. We can of course make it respect a custom formatter and use it as a trigger to skip the default logger. I'm just trying to make sure that this would be the expected behavior in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants