Skip to content

Commit

Permalink
Merge pull request #240 from y-yagi/allow_to_customize_log_message
Browse files Browse the repository at this point in the history
Allow to customize a message of action log
  • Loading branch information
reidmorrison authored Sep 29, 2024
2 parents 0c35abc + 5cfe0ac commit f8f1084
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
16 changes: 14 additions & 2 deletions lib/rails_semantic_logger/action_controller/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module ActionController
class LogSubscriber < ActiveSupport::LogSubscriber
INTERNAL_PARAMS = %w[controller action format _method only_path].freeze

class << self
attr_accessor :action_message_format
end

# Log as debug to hide Processing messages in production
def start_processing(event)
controller_logger(event).debug { "Processing ##{event.payload[:action]}" }
controller_logger(event).debug { action_message("Processing", event.payload) }
end

def process_action(event)
Expand Down Expand Up @@ -59,7 +63,7 @@ def process_action(event)
payload.delete(:response)

{
message: "Completed ##{payload[:action]}",
message: action_message("Completed", event.payload),
duration: event.duration,
payload: payload
}
Expand Down Expand Up @@ -122,6 +126,14 @@ def extract_path(path)
index = path.index("?")
index ? path[0, index] : path
end

def action_message(message, payload)
if self.class.action_message_format
self.class.action_message_format.call(message, payload)
else
"#{message} ##{payload[:action]}"
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rails_semantic_logger/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class Engine < ::Rails::Engine
if defined?(::ActionController)
require "action_controller/log_subscriber"

RailsSemanticLogger::ActionController::LogSubscriber.action_message_format = config.rails_semantic_logger.action_message_format
RailsSemanticLogger.swap_subscriber(
::ActionController::LogSubscriber,
RailsSemanticLogger::ActionController::LogSubscriber,
Expand Down
33 changes: 21 additions & 12 deletions lib/rails_semantic_logger/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,32 @@ module RailsSemanticLogger
# * named_tags: *DEPRECATED*
# Instead, supply a Hash to config.log_tags
# config.rails_semantic_logger.named_tags = nil
#
# * Change the message format of Action Controller action.
# A block that will be called to format the message.
# It is supplied with the `message` and `payload` and should return the formatted data.
#
# config.rails_semantic_logger.action_message_format = -> (message, payload) do
# "#{message} - #{payload[:controller]}##{payload[:action]}"
# end
class Options
attr_accessor :semantic, :started, :processing, :rendered, :ap_options, :add_file_appender,
:quiet_assets, :format, :named_tags, :filter, :console_logger
:quiet_assets, :format, :named_tags, :filter, :console_logger, :action_message_format

# Setup default values
def initialize
@semantic = true
@started = false
@processing = false
@rendered = false
@ap_options = {multiline: false}
@add_file_appender = true
@quiet_assets = false
@format = :default
@named_tags = nil
@filter = nil
@console_logger = true
@semantic = true
@started = false
@processing = false
@rendered = false
@ap_options = {multiline: false}
@add_file_appender = true
@quiet_assets = false
@format = :default
@named_tags = nil
@filter = nil
@console_logger = true
@action_message_format = nil
end
end
end
39 changes: 39 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,45 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest
}
)
end

it "customize action message" do
old_action_message_format = RailsSemanticLogger::ActionController::LogSubscriber.action_message_format
RailsSemanticLogger::ActionController::LogSubscriber.action_message_format = -> (message, payload) do
"#{message} #{payload[:controller]}##{payload[:action]}"
end

messages = semantic_logger_events do
post articles_url(params: params)
end
assert_equal 5, messages.count, messages

assert_semantic_logger_event(
messages[0],
message: "Started"
)

assert_semantic_logger_event(
messages[1],
message: "Processing ArticlesController#create"
)

assert_semantic_logger_event(
messages[2],
message: "Rendering"
)

assert_semantic_logger_event(
messages[3],
message: "Rendered"
)

assert_semantic_logger_event(
messages[4],
message: "Completed ArticlesController#create",
)
ensure
RailsSemanticLogger::ActionController::LogSubscriber.action_message_format = old_action_message_format
end
end

describe "#show" do
Expand Down

0 comments on commit f8f1084

Please sign in to comment.