mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Keep Logging in the ActiveModelSerializers namespace
This commit is contained in:
parent
51424963da
commit
21bb306d38
@ -1,18 +1,19 @@
|
|||||||
# Instrumentation
|
# Instrumentation
|
||||||
|
|
||||||
AMS uses the instrumentation API provided by Active Support this way we
|
ActiveModelSerializers uses the ActiveSupport::Notification API, which
|
||||||
can choose to be notified when AMS events occur inside our application.
|
allows for subscribing to events, such as for logging.
|
||||||
|
|
||||||
## render.active_model_serializers
|
## Events
|
||||||
|
|
||||||
|key | value |
|
Name:
|
||||||
|-------------|----------------------|
|
|
||||||
|:serializer | The serializer class |
|
`render.active_model_serializers`
|
||||||
|:adapter | The adapter instance |
|
|
||||||
|
Payload (example):
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
{
|
{
|
||||||
serializer: PostSerializer,
|
serializer: PostSerializer,
|
||||||
adapter: #<ActiveModel::Serializer::Adapter::Attributes:0x007f96e81eb730>
|
adapter: ActiveModel::Serializer::Adapter::Attributes
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# Logging
|
# 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
|
On a non Rails enviroment by default the `ActiveSupport::TaggedLogging` will be
|
||||||
used.
|
used.
|
||||||
@ -8,5 +8,5 @@ used.
|
|||||||
If we need to customize the logger we can define this in an initializer:
|
If we need to customize the logger we can define this in an initializer:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
ActiveModel::Serializer.logger = Logger.new(STDOUT)
|
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -2,15 +2,7 @@ require 'set'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class SerializableResource
|
class SerializableResource
|
||||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
|
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
|
||||||
extend ActiveModel::Callbacks
|
include ActiveModelSerializers::Logging
|
||||||
|
|
||||||
define_model_callbacks :render
|
|
||||||
|
|
||||||
around_render do |_, block, _|
|
|
||||||
notify_active_support do
|
|
||||||
block.call
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Primary interface to composing a resource with a serializer and adapter.
|
# Primary interface to composing a resource with a serializer and adapter.
|
||||||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
||||||
@ -89,13 +81,5 @@ module ActiveModel
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :resource, :adapter_opts, :serializer_opts
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -6,7 +6,6 @@ require 'active_model/serializer/associations'
|
|||||||
require 'active_model/serializer/configuration'
|
require 'active_model/serializer/configuration'
|
||||||
require 'active_model/serializer/fieldset'
|
require 'active_model/serializer/fieldset'
|
||||||
require 'active_model/serializer/lint'
|
require 'active_model/serializer/lint'
|
||||||
require 'active_model/serializer/logging'
|
|
||||||
|
|
||||||
# ActiveModel::Serializer is an abstract class that is
|
# ActiveModel::Serializer is an abstract class that is
|
||||||
# reified when subclassed to decorate a resource.
|
# reified when subclassed to decorate a resource.
|
||||||
@ -14,7 +13,6 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
include Configuration
|
include Configuration
|
||||||
include Associations
|
include Associations
|
||||||
include Logging
|
|
||||||
require 'active_model/serializer/adapter'
|
require 'active_model/serializer/adapter'
|
||||||
|
|
||||||
# Matches
|
# 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'
|
require 'rails/railtie'
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Railtie < Rails::Railtie
|
class Railtie < Rails::Railtie
|
||||||
initializer 'active_model_serializers.logger' do
|
initializer 'active_model_serializers.logger' do
|
||||||
ActiveSupport.on_load(:action_controller) do
|
ActiveSupport.on_load(:active_model_serializers) do
|
||||||
ActiveModelSerializers.logger = ActionController::Base.logger
|
self.logger = ActionController::Base.logger
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
require 'logger'
|
|
||||||
require 'active_model'
|
require 'active_model'
|
||||||
require 'active_support'
|
require 'active_support'
|
||||||
|
require 'active_support/tagged_logging'
|
||||||
|
require 'active_support/logger'
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
require 'action_controller/railtie'
|
require 'action_controller/railtie'
|
||||||
module ActiveModelSerializers
|
module ActiveModelSerializers
|
||||||
mattr_accessor :logger
|
mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
|
||||||
self.logger = Rails.logger || Logger.new(IO::NULL)
|
|
||||||
|
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
autoload :Model
|
autoload :Model
|
||||||
|
autoload :Logging
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
@ -50,6 +51,7 @@ require 'active_model/serializer/version'
|
|||||||
|
|
||||||
require 'action_controller/serialization'
|
require 'action_controller/serialization'
|
||||||
ActiveSupport.on_load(:action_controller) do
|
ActiveSupport.on_load(:action_controller) do
|
||||||
|
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModelSerializers)
|
||||||
include ::ActionController::Serialization
|
include ::ActionController::Serialization
|
||||||
ActionDispatch::Reloader.to_prepare do
|
ActionDispatch::Reloader.to_prepare do
|
||||||
ActiveModel::Serializer.serializers_cache.clear
|
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