mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 14:29:31 +00:00
Keep Logging in the ActiveModelSerializers namespace
This commit is contained in:
parent
51424963da
commit
21bb306d38
@ -1,18 +1,19 @@
|
||||
# Instrumentation
|
||||
|
||||
AMS uses the instrumentation API provided by Active Support this way we
|
||||
can choose to be notified when AMS events occur inside our application.
|
||||
ActiveModelSerializers uses the ActiveSupport::Notification API, which
|
||||
allows for subscribing to events, such as for logging.
|
||||
|
||||
## render.active_model_serializers
|
||||
## Events
|
||||
|
||||
|key | value |
|
||||
|-------------|----------------------|
|
||||
|:serializer | The serializer class |
|
||||
|:adapter | The adapter instance |
|
||||
Name:
|
||||
|
||||
`render.active_model_serializers`
|
||||
|
||||
Payload (example):
|
||||
|
||||
```ruby
|
||||
{
|
||||
serializer: PostSerializer,
|
||||
adapter: #<ActiveModel::Serializer::Adapter::Attributes:0x007f96e81eb730>
|
||||
adapter: ActiveModel::Serializer::Adapter::Attributes
|
||||
}
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# Logging
|
||||
|
||||
If we are using AMS on Rails app by default the `Rails.logger` will be used.
|
||||
If we are using ActiveModel::Serializers on Rails app by default the `Rails.logger` will be used.
|
||||
|
||||
On a non Rails enviroment by default the `ActiveSupport::TaggedLogging` will be
|
||||
used.
|
||||
@ -8,5 +8,5 @@ used.
|
||||
If we need to customize the logger we can define this in an initializer:
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.logger = Logger.new(STDOUT)
|
||||
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
||||
```
|
||||
|
||||
@ -2,15 +2,7 @@ require 'set'
|
||||
module ActiveModel
|
||||
class SerializableResource
|
||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
|
||||
extend ActiveModel::Callbacks
|
||||
|
||||
define_model_callbacks :render
|
||||
|
||||
around_render do |_, block, _|
|
||||
notify_active_support do
|
||||
block.call
|
||||
end
|
||||
end
|
||||
include ActiveModelSerializers::Logging
|
||||
|
||||
# Primary interface to composing a resource with a serializer and adapter.
|
||||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
||||
@ -89,13 +81,5 @@ module ActiveModel
|
||||
protected
|
||||
|
||||
attr_reader :resource, :adapter_opts, :serializer_opts
|
||||
|
||||
def notify_active_support
|
||||
event_name = 'render.active_model_serializers'.freeze
|
||||
payload = { serializer: serializer, adapter: adapter }
|
||||
ActiveSupport::Notifications.instrument(event_name, payload) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,7 +6,6 @@ require 'active_model/serializer/associations'
|
||||
require 'active_model/serializer/configuration'
|
||||
require 'active_model/serializer/fieldset'
|
||||
require 'active_model/serializer/lint'
|
||||
require 'active_model/serializer/logging'
|
||||
|
||||
# ActiveModel::Serializer is an abstract class that is
|
||||
# reified when subclassed to decorate a resource.
|
||||
@ -14,7 +13,6 @@ module ActiveModel
|
||||
class Serializer
|
||||
include Configuration
|
||||
include Associations
|
||||
include Logging
|
||||
require 'active_model/serializer/adapter'
|
||||
|
||||
# Matches
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Logging
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
def render(event)
|
||||
logger.tagged('AMS') do
|
||||
info do
|
||||
serializer = event.payload[:serializer]
|
||||
adapter = event.payload[:adapter]
|
||||
duration = event.duration.round(2)
|
||||
"Rendered #{serializer.name} with #{adapter.class} (#{duration}ms)"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveModelSerializers.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveModel::Serializer::Logging::LogSubscriber.attach_to :active_model_serializers
|
||||
@ -1,9 +1,11 @@
|
||||
require 'active_model_serializers'
|
||||
require 'rails/railtie'
|
||||
|
||||
module ActiveModel
|
||||
class Railtie < Rails::Railtie
|
||||
initializer 'active_model_serializers.logger' do
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
ActiveModelSerializers.logger = ActionController::Base.logger
|
||||
ActiveSupport.on_load(:active_model_serializers) do
|
||||
self.logger = ActionController::Base.logger
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
require 'logger'
|
||||
require 'active_model'
|
||||
require 'active_support'
|
||||
require 'active_support/tagged_logging'
|
||||
require 'active_support/logger'
|
||||
require 'action_controller'
|
||||
require 'action_controller/railtie'
|
||||
module ActiveModelSerializers
|
||||
mattr_accessor :logger
|
||||
self.logger = Rails.logger || Logger.new(IO::NULL)
|
||||
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :Model
|
||||
autoload :Logging
|
||||
|
||||
module_function
|
||||
|
||||
@ -50,6 +51,7 @@ require 'active_model/serializer/version'
|
||||
|
||||
require 'action_controller/serialization'
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
|
||||
include ::ActionController::Serialization
|
||||
ActionDispatch::Reloader.to_prepare do
|
||||
ActiveModel::Serializer.serializers_cache.clear
|
||||
|
||||
45
lib/active_model_serializers/logging.rb
Normal file
45
lib/active_model_serializers/logging.rb
Normal file
@ -0,0 +1,45 @@
|
||||
##
|
||||
# Adapted from:
|
||||
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
|
||||
module ActiveModelSerializers::Logging
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
extend ActiveModel::Callbacks
|
||||
define_model_callbacks :render
|
||||
around_render do |_, block, _|
|
||||
notify_active_support do
|
||||
block.call
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def notify_active_support
|
||||
event_name = 'render.active_model_serializers'.freeze
|
||||
payload = { serializer: serializer, adapter: adapter }
|
||||
ActiveSupport::Notifications.instrument(event_name, payload) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
def render(event)
|
||||
logger.tagged('AMS') do
|
||||
info do
|
||||
serializer = event.payload[:serializer]
|
||||
adapter = event.payload[:adapter]
|
||||
duration = event.duration.round(2)
|
||||
"Rendered #{serializer.name} with #{adapter.class} (#{duration}ms)"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveModelSerializers.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveModelSerializers::Logging::LogSubscriber.attach_to :active_model_serializers
|
||||
Loading…
Reference in New Issue
Block a user