Skip to content

Commit 68b36dc

Browse files
authored
Add logging (#55)
1 parent 3bd63fe commit 68b36dc

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Added
22

3+
* Configurable logger and logsubscriber for instrumented events.
34
* Default configuration and hooks for the `meta` parameter.
45
* Default configuration and hooks for the `cache` parameter.
56
* Default configuration and hooks for the `links` parameter.

Diff for: lib/generators/jsonapi/initializer/templates/initializer.rb

+6
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@
6868
#
6969
# # Set a default pagination scheme.
7070
# config.jsonapi_pagination = ->(_) { {} }
71+
#
72+
# # Set a logger.
73+
# config.logger = Logger.new(STDOUT)
74+
#
75+
# # Uncomment the following to disable logging.
76+
# config.logger = Logger.new('/dev/null')
7177
end

Diff for: lib/jsonapi/rails/configuration.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Configurable
3939

4040
DEFAULT_JSONAPI_PAGINATION = ->(_) { {} }
4141

42+
DEFAULT_LOGGER = Logger.new(STDERR)
43+
4244
DEFAULT_CONFIG = {
4345
jsonapi_class: DEFAULT_JSONAPI_CLASS,
4446
jsonapi_errors_class: DEFAULT_JSONAPI_ERRORS_CLASS,
@@ -49,7 +51,8 @@ module Configurable
4951
jsonapi_links: DEFAULT_JSONAPI_LINKS,
5052
jsonapi_meta: DEFAULT_JSONAPI_META,
5153
jsonapi_object: DEFAULT_JSONAPI_OBJECT,
52-
jsonapi_pagination: DEFAULT_JSONAPI_PAGINATION
54+
jsonapi_pagination: DEFAULT_JSONAPI_PAGINATION,
55+
logger: DEFAULT_LOGGER
5356
}.freeze
5457

5558
def configure

Diff for: lib/jsonapi/rails/controller.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ def deserializable_resource(key, options = {}, &block)
6767
Class.new(JSONAPI::Rails::Deserializable::Resource, &block)
6868

6969
before_action(options) do |controller|
70-
# TODO(lucas): Fail with helpful error message if _jsonapi not
71-
# present.
72-
hash = controller.params[:_jsonapi].to_unsafe_hash
70+
hash = controller.params.to_unsafe_hash[:_jsonapi]
71+
if hash.nil?
72+
JSONAPI::Rails.config[:logger].warn do
73+
"Unable to deserialize #{key} because no JSON API payload was" \
74+
" found. (#{controller.controller_name}##{params[:action]})"
75+
end
76+
next
77+
end
78+
7379
ActiveSupport::Notifications
74-
.instrument('parse.jsonapi', payload: hash, class: klass) do
80+
.instrument('parse.jsonapi-rails',
81+
key: key, payload: hash, class: klass) do
7582
JSONAPI::Parser::Resource.parse!(hash)
7683
resource = klass.new(hash[:data])
7784
controller.request.env[JSONAPI_POINTERS_KEY] =

Diff for: lib/jsonapi/rails/log_subscriber.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module JSONAPI
2+
module Rails
3+
# @private
4+
class LogSubscriber < ActiveSupport::LogSubscriber
5+
def render(event)
6+
info do
7+
"Completed JSON API rendering (#{event.duration.round(2)}ms)"
8+
end
9+
end
10+
11+
def parse(event)
12+
info do
13+
"Completed JSON API deserializing of #{event.payload[:key]}" \
14+
" (#{event.duration.round(2)})"
15+
end
16+
end
17+
18+
def logger
19+
JSONAPI::Rails.config[:logger]
20+
end
21+
end
22+
end
23+
end
24+
25+
JSONAPI::Rails::LogSubscriber.attach_to :'jsonapi-rails'

Diff for: lib/jsonapi/rails/railtie.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'action_controller'
33
require 'active_support'
44

5+
require 'jsonapi/rails/log_subscriber'
56
require 'jsonapi/rails/renderer'
67

78
module JSONAPI
@@ -52,7 +53,7 @@ def register_renderers
5253
# Renderer proc is evaluated in the controller context.
5354
self.content_type ||= Mime[:jsonapi]
5455

55-
ActiveSupport::Notifications.instrument('render.jsonapi',
56+
ActiveSupport::Notifications.instrument('render.jsonapi-rails',
5657
resources: resources,
5758
options: options) do
5859
renderer.render(resources, options, self).to_json

0 commit comments

Comments
 (0)