mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Squashed commits: Add Logging Generates logging when renders a serializer. Tunning performance on notify_active_support - Use yield over block.call - Freeze the event name string Organize the logger architeture * Keep only the `ActiveModel::Serializer.logger` to follow the same public API we have for example to config, like `ActiveModel::Serializer.config.adapter` and remove the `ActiveModelSerializers.logger` API. * Define the logger on the load of the AMS, following the Rails convention on Railties [1], [2] and [3]. This way on non Rails apps we have a default logger and on Rails apps we will use the `Rails.logger` the same way that Active Job do [4]. [1]:2ad9afe4ff/activejob/lib/active_job/railtie.rb (L9-L11)[2]:2ad9afe4ff/activerecord/lib/active_record/railtie.rb (L75-L77)[3]:2ad9afe4ff/actionview/lib/action_view/railtie.rb (L19-L21)[4]:2ad9afe4ff/activejob/lib/active_job/logging.rb (L10-L11)Performance tunning on LogSubscriber#render Move the definition of locals to inside the `info` block this way the code is executed only when the logger is called. Remove not needed check on SerializableResource Use SerializableResource on ActionController integration On the ActionController was using a adapter, and since the instrumentation is made on the SerializableResource we need to use the SerializableResource over the adapter directly. Otherwise the logger is not called on a Rails app. Use SerializableResource on the ActionController, since this is the main interface to create and call a serializer. Using always the SerializableResource we can keep the adapter code more easy to mantain since no Adapter will need to call the instrumentation, only the SerializableResource care about this. Add docs about logging Add a CHANGELOG entry Keep the ActiveModelSerializers.logger Better wording on Logging docs [ci skip] Add doc about instrumentation [ci skip] Use ActiveModel::Callbacks on the SerializableResource
78 lines
2.2 KiB
Ruby
78 lines
2.2 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActiveModel
|
|
class Serializer
|
|
class LoggingTest < Minitest::Test
|
|
class TestLogger < ActiveSupport::Logger
|
|
def initialize
|
|
@file = StringIO.new
|
|
super(@file)
|
|
end
|
|
|
|
def messages
|
|
@file.rewind
|
|
@file.read
|
|
end
|
|
end
|
|
|
|
def setup
|
|
@author = Author.new(name: 'Steve K.')
|
|
@post = Post.new(title: 'New Post', body: 'Body')
|
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
@post.comments = [@comment]
|
|
@comment.post = @post
|
|
@post.author = @author
|
|
@author.posts = [@post]
|
|
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
|
|
|
@old_logger = ActiveModelSerializers.logger
|
|
@logger = ActiveSupport::TaggedLogging.new(TestLogger.new)
|
|
logger @logger
|
|
end
|
|
|
|
def teardown
|
|
logger @old_logger
|
|
end
|
|
|
|
def logger(logger)
|
|
ActiveModelSerializers.logger = logger
|
|
end
|
|
|
|
def test_uses_ams_as_tag
|
|
ActiveModel::SerializableResource.new(@post).serializable_hash
|
|
assert_match(/\[AMS\]/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_when_call_serializable_hash
|
|
ActiveModel::SerializableResource.new(@post).serializable_hash
|
|
assert_match(/Rendered/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_when_call_as_json
|
|
ActiveModel::SerializableResource.new(@post).as_json
|
|
assert_match(/Rendered/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_when_call_to_json
|
|
ActiveModel::SerializableResource.new(@post).to_json
|
|
assert_match(/Rendered/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_correct_serializer
|
|
ActiveModel::SerializableResource.new(@post).serializable_hash
|
|
assert_match(/PostSerializer/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_correct_adapter
|
|
ActiveModel::SerializableResource.new(@post).serializable_hash
|
|
assert_match(/ActiveModel::Serializer::Adapter::Attributes/, @logger.messages)
|
|
end
|
|
|
|
def test_logs_the_duration
|
|
ActiveModel::SerializableResource.new(@post).serializable_hash
|
|
assert_match(/\(\d+\.\d+ms\)/, @logger.messages)
|
|
end
|
|
end
|
|
end
|
|
end
|