Skip to content

Commit

Permalink
Add a verbose_logging option to config
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorrison committed Jan 13, 2025
1 parent ff95522 commit 070151a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/twirp/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class Configuration
# Logger to use for Twirp requests. Defaults to Rails.logger
attr_accessor :logger

# Whether to log Twirp responses. Defaults to true if log level is :debug
# Example:
# Twirp Response: <Twirp::Example::Haberdasher::Hat: inches: 24, color: "Tan", name: "Pork Pie">
attr_accessor :verbose_logging

# An array of directories to search for *_twirp.rb files
# Defaults to ["lib"]
attr_accessor :load_paths
Expand All @@ -29,6 +34,7 @@ def initialize
@endpoint = "/twirp"
@load_paths = ["lib"]
@logger = Logger
@verbose_logging = nil
@middleware = []
@service_hooks = {}
end
Expand Down
7 changes: 5 additions & 2 deletions lib/twirp/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class Engine < ::Rails::Engine
app.config.twirp.send(key)
end

# Set up logging
app.config.middleware.use app.config.twirp.logger, ::Rails.logger
app.config.twirp.verbose_logging = ::Rails.logger.level == ::Logger::DEBUG if app.config.twirp.verbose_logging.nil?

app.config.twirp.middleware.each do |middleware|
app.config.middleware.use middleware
end
Expand All @@ -50,8 +53,8 @@ def services
service.send(hook_name, &hook)
end

# Add our own logging hooks in debug mode
if ::Rails.logger.level == ::Logger::DEBUG
# Add our own logging hooks if verbose_logging is enabled
if ::Rails.application.config.twirp.verbose_logging
service.on_success do |env|
::Rails.logger.debug("Twirp Response: #{env[:output].inspect}")
end
Expand Down

0 comments on commit 070151a

Please sign in to comment.