rails_api_logger 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 436fe06f5a60eb0cfe6e341c526958b5d2e568ce44eb5e0d45fc704d8cc0151c
4
+ data.tar.gz: b633e7b6ff50358f91f6096adaa0d693f5e39144316af68e6b821f06bf0858cc
5
+ SHA512:
6
+ metadata.gz: e17c8a4a95a6ef87c497257e5da27ed41b81fe9fede8b44767eeadaf8335ab68a63d15d6a27436dac72da6a3316b670927c72b3d7cb60a32a73361c8148e7654
7
+ data.tar.gz: 914a5cd684eac764b416441f1e1f1a78318e3e0eb68a842620a4f4a7f867d94aebe06fea37faceb49a8a4aa25b349748ef1c14bdae9bcb1e71972a6a69c05195
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,19 @@
1
+ version: v1.0
2
+ name: master-deploy
3
+ agent:
4
+ machine:
5
+ type: e1-standard-2
6
+ os_image: ubuntu1804
7
+
8
+ blocks:
9
+ - name: master-deploy
10
+ task:
11
+ secrets:
12
+ - name: rubygems-deploy
13
+ jobs:
14
+ - name: master-deploy
15
+ commands:
16
+ - checkout --use-cache
17
+ - gem build rails_api_logger
18
+ - chmod 0600 ~/.gem/credentials
19
+ - gem push rails_api_logger-*.gem
@@ -0,0 +1,31 @@
1
+ version: "v1.0"
2
+ name: rails_api_logger
3
+ agent:
4
+ machine:
5
+ type: e1-standard-2
6
+ os_image: ubuntu1804
7
+ auto_cancel:
8
+ running:
9
+ when: "true"
10
+
11
+ blocks:
12
+ - name: tests
13
+ execution_time_limit:
14
+ minutes: 10
15
+ task:
16
+ secrets:
17
+ - name: rails_api_logger
18
+ prologue:
19
+ commands:
20
+ - checkout --use-cache
21
+ - cache restore
22
+ - bundle install --deployment -j 4 --path vendor/bundle
23
+ - cache store
24
+ jobs:
25
+ - name: tests
26
+ commands:
27
+ - bundle exec standardrb
28
+ - bundle exec rspec
29
+ promotions:
30
+ - name: master
31
+ pipeline_file: master-deploy.yml
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ * Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alessandro.rodi@renuo.ch. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_api_logger.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,123 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_api_logger (0.1.1)
5
+ activerecord (>= 4.1.0)
6
+ railties (>= 4.1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (6.1.1)
12
+ actionview (= 6.1.1)
13
+ activesupport (= 6.1.1)
14
+ rack (~> 2.0, >= 2.0.9)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
+ actionview (6.1.1)
19
+ activesupport (= 6.1.1)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
+ activemodel (6.1.1)
25
+ activesupport (= 6.1.1)
26
+ activerecord (6.1.1)
27
+ activemodel (= 6.1.1)
28
+ activesupport (= 6.1.1)
29
+ activesupport (6.1.1)
30
+ concurrent-ruby (~> 1.0, >= 1.0.2)
31
+ i18n (>= 1.6, < 2)
32
+ minitest (>= 5.1)
33
+ tzinfo (~> 2.0)
34
+ zeitwerk (~> 2.3)
35
+ ast (2.4.2)
36
+ builder (3.2.4)
37
+ concurrent-ruby (1.1.8)
38
+ crass (1.0.6)
39
+ diff-lcs (1.4.4)
40
+ erubi (1.10.0)
41
+ i18n (1.8.8)
42
+ concurrent-ruby (~> 1.0)
43
+ loofah (2.9.0)
44
+ crass (~> 1.0.2)
45
+ nokogiri (>= 1.5.9)
46
+ method_source (1.0.0)
47
+ mini_portile2 (2.5.0)
48
+ minitest (5.14.3)
49
+ nokogiri (1.11.1)
50
+ mini_portile2 (~> 2.5.0)
51
+ racc (~> 1.4)
52
+ parallel (1.20.1)
53
+ parser (3.0.0.0)
54
+ ast (~> 2.4.1)
55
+ racc (1.5.2)
56
+ rack (2.2.3)
57
+ rack-test (1.1.0)
58
+ rack (>= 1.0, < 3)
59
+ rails-dom-testing (2.0.3)
60
+ activesupport (>= 4.2.0)
61
+ nokogiri (>= 1.6)
62
+ rails-html-sanitizer (1.3.0)
63
+ loofah (~> 2.3)
64
+ railties (6.1.1)
65
+ actionpack (= 6.1.1)
66
+ activesupport (= 6.1.1)
67
+ method_source
68
+ rake (>= 0.8.7)
69
+ thor (~> 1.0)
70
+ rainbow (3.0.0)
71
+ rake (12.3.3)
72
+ regexp_parser (2.0.3)
73
+ rexml (3.2.4)
74
+ rspec (3.10.0)
75
+ rspec-core (~> 3.10.0)
76
+ rspec-expectations (~> 3.10.0)
77
+ rspec-mocks (~> 3.10.0)
78
+ rspec-core (3.10.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-expectations (3.10.0)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.10.0)
83
+ rspec-mocks (3.10.0)
84
+ diff-lcs (>= 1.2.0, < 2.0)
85
+ rspec-support (~> 3.10.0)
86
+ rspec-support (3.10.0)
87
+ rubocop (1.10.0)
88
+ parallel (~> 1.10)
89
+ parser (>= 3.0.0.0)
90
+ rainbow (>= 2.2.2, < 4.0)
91
+ regexp_parser (>= 1.8, < 3.0)
92
+ rexml
93
+ rubocop-ast (>= 1.2.0, < 2.0)
94
+ ruby-progressbar (~> 1.7)
95
+ unicode-display_width (>= 1.4.0, < 3.0)
96
+ rubocop-ast (1.4.1)
97
+ parser (>= 2.7.1.5)
98
+ rubocop-performance (1.9.2)
99
+ rubocop (>= 0.90.0, < 2.0)
100
+ rubocop-ast (>= 0.4.0)
101
+ ruby-progressbar (1.11.0)
102
+ sqlite3 (1.4.2)
103
+ standard (0.13.0)
104
+ rubocop (= 1.10.0)
105
+ rubocop-performance (= 1.9.2)
106
+ thor (1.1.0)
107
+ tzinfo (2.0.4)
108
+ concurrent-ruby (~> 1.0)
109
+ unicode-display_width (2.0.0)
110
+ zeitwerk (2.4.2)
111
+
112
+ PLATFORMS
113
+ ruby
114
+
115
+ DEPENDENCIES
116
+ rails_api_logger!
117
+ rake (~> 12.0)
118
+ rspec (~> 3.0)
119
+ sqlite3 (~> 1.4.0)
120
+ standard (~> 0.13.0)
121
+
122
+ BUNDLED WITH
123
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Renuo AG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Rails API Logger
2
+
3
+ The Rails API logger gem introduces a set of tools to log and debug API requests.
4
+ It works on two sides:
5
+
6
+ * **Inbound requests**: API exposed by your application
7
+ * **Outbound requests**: API invoked by your application
8
+
9
+ This gem has been extracted from various Renuo projects, where we implemented this
10
+ technique multiple times successfully.
11
+
12
+ This gem creates two database tables to log the following information:
13
+
14
+ * **path** the path/url invoked
15
+ * **method** the method used to invoke the API (get, post, put, etc...)
16
+ * **request_body** what was included in the request body
17
+ * **response_body** what was included in the response body
18
+ * **response_code** the HTTP response code of the request
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'rails_api_logger'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ ```bash
31
+ bundle install
32
+ bundle exec rails generate rails_api_logger:install
33
+ bundle exec rails db:migrate
34
+ ```
35
+
36
+ This will generate two tables `inbound_request_logs` and `outbound_request_logs`.
37
+ These tables will contain the logs.
38
+
39
+ ## Log Outbound Requests
40
+
41
+ Given an outbound request in the following format:
42
+
43
+ ```ruby
44
+ uri = URI('http://example.com/some_path?query=string')
45
+ http = Net::HTTP.start(uri.host, uri.port)
46
+ request = Net::HTTP::Get.new(uri)
47
+ response = http.request(request)
48
+ ```
49
+
50
+ you can log it by doing the following:
51
+
52
+ ```ruby
53
+ uri = URI('http://example.com/some_path?query=string')
54
+ http = Net::HTTP.start(uri.host, uri.port)
55
+ request = Net::HTTP::Get.new(uri)
56
+
57
+ log = OutboundRequestLog.from_request(request)
58
+
59
+ response = http.request(request)
60
+
61
+ log.response_body = response.body
62
+ log.response_code = response.code
63
+ log.save!
64
+ ```
65
+
66
+ You can also use the provided logger class to do that in a simpler and safer manner:
67
+
68
+ ```ruby
69
+ uri = URI('http://example.com/some_path?query=string')
70
+ http = Net::HTTP.start(uri.host, uri.port)
71
+ request = Net::HTTP::Get.new(uri)
72
+ response = RailsApiLogger.call(uri, http, request)
73
+ ```
74
+
75
+ This will guarantee that the log is always persisted, even in case of errors.
76
+
77
+ ## Log Inbound Requests
78
+
79
+ If you are exposing some API you might be interested in logging the requests you receive.
80
+ You can do so in your controller by adding:
81
+
82
+ ```ruby
83
+ around_action :log_inbound_request
84
+ ```
85
+
86
+ you can also log only requests that have an impact in your system with:
87
+
88
+ ```ruby
89
+ around_action :log_inbound_request, if: :request_with_state_change?
90
+ ```
91
+
92
+ this will create a log entry only for POST, PUT, and PATCH calls.
93
+
94
+ In the implementation of your API, you can call any time `attach_inbound_request_loggable(model)`
95
+ to attach an already persisted model to the log record.
96
+
97
+ For example:
98
+ ```ruby
99
+
100
+ def create
101
+ @user = User.new(user_params)
102
+ if @user.save
103
+ attach_inbound_request_loggable(@user)
104
+ render json: { id: @user.id }, status: :created
105
+ else
106
+ render json: @user.errors.details, status: :unprocessable_entity
107
+ end
108
+ end
109
+ ```
110
+
111
+ in the User model you can define:
112
+
113
+ ```ruby
114
+ has_many :inbound_request_logs, inverse_of: :loggable, dependent: :destroy, as: :loggable
115
+ ```
116
+
117
+ to be able to access the logs attached to the model.
118
+
119
+ ## RailsAdmin integration
120
+
121
+ We provide here some code samples to integrate the models in [RailsAdmin](https://github.com/sferik/rails_admin).
122
+
123
+ This configuration will give you some nice views, and searches to work with the logs efficiently.
124
+ ```ruby
125
+ %w[InboundRequestLog OutboundRequestLog].each do |logging_model|
126
+ config.model logging_model do
127
+ list do
128
+ filters %i[method path response_code request_body response_body created_at]
129
+ scopes [nil, :failed]
130
+
131
+ include_fields :method, :path, :response_code, :created_at
132
+
133
+ field :request_body, :string do
134
+ visible false
135
+ searchable true
136
+ filterable true
137
+ end
138
+
139
+ field :response_body, :string do
140
+ visible false
141
+ searchable true
142
+ filterable true
143
+ end
144
+ end
145
+
146
+ show do
147
+ include_fields :loggable, :method, :path, :response_code
148
+ field(:created_at)
149
+ field(:request_body) do
150
+ formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].request_body)}</pre>".html_safe }
151
+ end
152
+ field(:response_body) do
153
+ formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].response_body)}</pre>".html_safe }
154
+ end
155
+ end
156
+ end
157
+ end
158
+ ```
159
+
160
+ you can also add this configuration directly in `rails_admin.rb` with
161
+
162
+ ```ruby
163
+ include RailsApiLoggerRailsAdminConfiguration
164
+ ```
165
+
166
+ ## Development
167
+
168
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
169
+
170
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
171
+
172
+ ## Contributing
173
+
174
+ Bug reports and pull requests are welcome on GitHub at https://github.com/renuo/rails_api_logger.
175
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
176
+ the [code of conduct](https://github.com/renuo/rails_api_logger/blob/master/CODE_OF_CONDUCT.md).
177
+
178
+ ## License
179
+
180
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
181
+
182
+ ## Code of Conduct
183
+
184
+ Everyone interacting in the RailsApiLogger project's codebases, issue trackers, chat rooms and mailing lists is
185
+ expected to follow the [code of conduct](https://github.com/renuo/rails_api_logger/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_api_logger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+ require "rails/generators/migration"
5
+
6
+ module RailsApiLogger
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("../../templates", __FILE__)
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ def self.next_migration_number(dirname)
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
17
+ end
18
+
19
+ desc "Copy migrations to your application."
20
+ def copy_migrations
21
+ migration_template "create_rails_api_logger_table.rb", "db/migrate/create_rails_api_logger_table.rb"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def change
3
+ create_table :inbound_request_logs do |t|
4
+ t.string :method
5
+ t.string :path
6
+ t.text :request_body
7
+ t.text :response_body
8
+ t.integer :response_code
9
+ t.references :loggable, index: true, polymorphic: true
10
+ t.timestamps null: false
11
+ end
12
+
13
+ create_table :outbound_request_logs do |t|
14
+ t.string :method
15
+ t.string :path
16
+ t.text :request_body
17
+ t.text :response_body
18
+ t.integer :response_code
19
+ t.references :loggable, index: true, polymorphic: true
20
+ t.timestamps null: false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require "active_record"
2
+ require "rails_api_logger/version"
3
+ require "rails_api_logger/request_log"
4
+ require "rails_api_logger/inbound_request_log"
5
+ require "rails_api_logger/outbound_request_log"
6
+ require "rails_api_logger/inbound_requests_logger"
7
+
8
+ module RailsApiLogger
9
+ class Error < StandardError; end
10
+
11
+ def self.call(uri, http, request)
12
+ log = OutboundRequestLog.from_request(request)
13
+
14
+ http.request(request).tap do |response|
15
+ log.response_code = response.code
16
+ log.response_body = response.body
17
+ end
18
+ rescue => e
19
+ log.response_body = {error: e.message}
20
+ raise
21
+ ensure
22
+ log.save!
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ class InboundRequestLog < RequestLog
2
+ end
@@ -0,0 +1,31 @@
1
+ module InboundRequestsLogger
2
+ extend ActiveSupport::Concern
3
+
4
+ private
5
+
6
+ def log_inbound_request
7
+ @inbound_request_log = InboundRequestLog.from_request(request)
8
+ yield
9
+ @inbound_request_log.update(response_body: JSON.parse(response.body), response_code: response.code)
10
+ end
11
+
12
+ def request_with_state_change?
13
+ request.post? || request.put? || request.patch? || request.delete?
14
+ end
15
+
16
+ def request_without_body?
17
+ request.get? || request.head? || request.options? || request.delete?
18
+ end
19
+
20
+ def attach_inbound_request_loggable(loggable)
21
+ @inbound_request_log.loggable = loggable if loggable&.persisted?
22
+ end
23
+ end
24
+
25
+ ActiveSupport.on_load(:action_controller) do
26
+ include InboundRequestsLogger
27
+ end
28
+
29
+ ActiveSupport.on_load(:action_controller_api) do
30
+ include InboundRequestsLogger
31
+ end
@@ -0,0 +1,2 @@
1
+ class OutboundRequestLog < RequestLog
2
+ end
@@ -0,0 +1,33 @@
1
+ %w[InboundRequestLog OutboundRequestLog].each do |logging_model|
2
+ config.model logging_model do
3
+ list do
4
+ filters %i[method path response_code request_body response_body created_at]
5
+ scopes [nil, :failed]
6
+
7
+ include_fields :method, :path, :response_code, :created_at
8
+
9
+ field :request_body, :string do
10
+ visible false
11
+ searchable true
12
+ filterable true
13
+ end
14
+
15
+ field :response_body, :string do
16
+ visible false
17
+ searchable true
18
+ filterable true
19
+ end
20
+ end
21
+
22
+ show do
23
+ include_fields :loggable, :method, :path, :response_code
24
+ field(:created_at)
25
+ field(:request_body) do
26
+ formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].request_body)}</pre>".html_safe }
27
+ end
28
+ field(:response_body) do
29
+ formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].response_body)}</pre>".html_safe }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ class RequestLog < ActiveRecord::Base
2
+ self.abstract_class = true
3
+
4
+ serialize :request_body, JSON
5
+ serialize :response_body, JSON
6
+
7
+ belongs_to :loggable, optional: true, polymorphic: true
8
+
9
+ scope :failed, -> { where("response_code not like '2%'") }
10
+
11
+ validates :request_body, presence: true
12
+ validates :method, presence: true
13
+ validates :path, presence: true
14
+
15
+ def self.from_request(request)
16
+ body = (request.body.respond_to?(:read) ? request.body.read : request.body).force_encoding("UTF-8")
17
+ begin
18
+ body = JSON.parse(body) if body.present?
19
+ rescue JSON::ParserError
20
+ body
21
+ end
22
+ create(path: request.path, request_body: body, method: request.method)
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RailsApiLogger
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "lib/rails_api_logger/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rails_api_logger"
5
+ spec.version = RailsApiLogger::VERSION
6
+ spec.authors = ["Alessandro Rodi"]
7
+ spec.email = ["alessandro.rodi@renuo.ch"]
8
+
9
+ spec.summary = "Log API requests like a king \u{1F451} "
10
+ spec.description = "Log inbound and outbound API requests in your Rails application"
11
+ spec.homepage = "https://github.com/renuo/rails_api_logger"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/renuo/rails_api_logger"
17
+ spec.metadata["changelog_uri"] = "https://github.com/renuo/rails_api_logger/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency("railties", ">= 4.1.0")
29
+ spec.add_dependency("activerecord", ">= 4.1.0")
30
+
31
+ spec.add_development_dependency "sqlite3", "~> 1.4.0"
32
+ spec.add_development_dependency "standard", "~> 0.13.0"
33
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_api_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Rodi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.0
69
+ description: Log inbound and outbound API requests in your Rails application
70
+ email:
71
+ - alessandro.rodi@renuo.ch
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-version"
79
+ - ".semaphore/master-deploy.yml"
80
+ - ".semaphore/semaphore.yml"
81
+ - CHANGELOG.md
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/generators/rails_api_logger/install_generator.rb
91
+ - lib/generators/templates/create_rails_api_logger_table.rb.tt
92
+ - lib/rails_api_logger.rb
93
+ - lib/rails_api_logger/inbound_request_log.rb
94
+ - lib/rails_api_logger/inbound_requests_logger.rb
95
+ - lib/rails_api_logger/outbound_request_log.rb
96
+ - lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb
97
+ - lib/rails_api_logger/request_log.rb
98
+ - lib/rails_api_logger/version.rb
99
+ - rails_api_logger.gemspec
100
+ homepage: https://github.com/renuo/rails_api_logger
101
+ licenses:
102
+ - MIT
103
+ metadata:
104
+ homepage_uri: https://github.com/renuo/rails_api_logger
105
+ source_code_uri: https://github.com/renuo/rails_api_logger
106
+ changelog_uri: https://github.com/renuo/rails_api_logger/CHANGELOG.md
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.3.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.1.4
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: "Log API requests like a king \U0001F451"
126
+ test_files: []