sapience 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eacddd2a363dd27232188e0576430593a3ea71de
4
- data.tar.gz: 06160fc823fa373fa7a0367541c23a1d825e8e9c
3
+ metadata.gz: a496d597a7e40d5578684eb7c6cb8dcd95f52e7e
4
+ data.tar.gz: 0f040171526449771d60df6b210bcefb0872a1db
5
5
  SHA512:
6
- metadata.gz: c7a9eb74f28e568bd0646410098bbea506b438e0db276c959e37ec1eec753c4303f748a76a9119fbb1628c3a16a9cc7f0593f383108b757ddcbf17a69f80efe5
7
- data.tar.gz: e44f3a521a0cff771a7f5598eee99f2f131dcbe7929e463f953787d78cbd629d532148202e4555f604dc3f82fd0e3ee4dd4e0a860f7c4f15f11eace2b17b6ce6
6
+ metadata.gz: b9a04aedb193c4a03b77e137c26c866b14961448c376a602301ce96cf5f30e70ef0d551f75aec3eef82c43a262b05529c79d4f2d3e42d04059d79467be334a1a
7
+ data.tar.gz: 0f3e82c77af85a5a2acd9554811ddd833c6cdbe38939ddc643637a28305eb7491057e0c8448401eab68fbb80f8cc689b08375aa5c2be883d73451a67090bcfed
@@ -1,3 +1,8 @@
1
+ ## v2.0.3
2
+
3
+ - Ability to configure ActiveRecord log level independently
4
+ - Fix issue where the Grape extension was incorrectly logging 500 response codes as 404's
5
+
1
6
  ## v2.0.2
2
7
 
3
8
  - Fix metrics collection for 2.0.1
data/README.md CHANGED
@@ -38,7 +38,7 @@ module Aslan
38
38
  class Base < Grape::API
39
39
  use Sapience::Extensions::Grape::Middleware::Logging, logger: Sapience[self]
40
40
 
41
- # To log all requests even when no route was found try the following:
41
+ # To log all requests even when no route was found add the following:
42
42
  route :any, "*path" do
43
43
  error!({ error: "No route found" }, 404)
44
44
  end
@@ -48,6 +48,38 @@ end
48
48
 
49
49
  ```
50
50
 
51
+ Also make sure you only use "rescue_from" when you want to return a 500 status. For any other status "dont" use
52
+ "rescue_from".
53
+
54
+ For example if you have some authentication code that raises an exception when the user is not authenticated,
55
+ dont use "rescue_from" to catch this exception and change the status to 403 within the rescue_from, handle this
56
+ exception instead on the "before" block, or alternatively within your endpoint, like below:
57
+
58
+
59
+ ```ruby
60
+ before do
61
+ begin
62
+ current_user
63
+ rescue ClientPortalApiClient::Unauthorized
64
+ error!("User is forbidden from accessing this endpoin", 403)
65
+ end
66
+ end
67
+ ```
68
+
69
+ Likewise, for capturing any other exception for which you want to return a code other than 500, capture your
70
+ exception in the "before" block or within your endpoint, but make sure "rescue_from" is "only" used for 500
71
+ status, as grape will call rescue_from once is gone through all the middleware, so if you change the status
72
+ in a rescue_from, Sapience would not be able to log it correctly. So the below is ok because the rescue_from
73
+ is using status 500:
74
+
75
+ ```ruby
76
+ rescue_from :all do |e|
77
+ error!(message: e.message, status: 500)
78
+ end
79
+ ```
80
+
81
+
82
+
51
83
  ### Configuration
52
84
 
53
85
  The sapience configuration can be controlled by a `config/sapience.yml` file or if you like us have many projects that use the same configuration you can create your own gem with a shared config. Have a look at [reevoo/reevoo_sapience-rb](https://github.com/reevoo/reevoo_sapience-rb)
@@ -57,6 +89,7 @@ The `app_name` is required to be configured. Sapience will fail on startup if ap
57
89
  ```ruby
58
90
  Sapience.configure do |config|
59
91
  config.default_level = :info
92
+ config.log_level_active_record = :debug
60
93
  config.backtrace_level = :error
61
94
  config.appenders = [
62
95
  { stream: { io: STDOUT, formatter: :color } },
@@ -91,6 +124,7 @@ test:
91
124
 
92
125
  development:
93
126
  log_executor: single_thread_executor
127
+ log_level_active_record: debug
94
128
  log_level: debug
95
129
  appenders:
96
130
  - stream:
@@ -275,6 +309,10 @@ Formatters can be specified by using the key `formatter: :camelized_formatter_na
275
309
 
276
310
  ## Running the tests
277
311
 
312
+ You need to create the test postgres db, by running the command below:
313
+
314
+ `createdb rails_app_test`
315
+
278
316
  `bin/tests`
279
317
 
280
318
  ## Environment variables
@@ -4,13 +4,14 @@ module Sapience
4
4
 
5
5
  # rubocop:disable ClassVars
6
6
  class Configuration
7
- attr_reader :default_level, :backtrace_level, :backtrace_level_index
7
+ attr_reader :default_level, :log_level_active_record, :backtrace_level, :backtrace_level_index
8
8
  attr_writer :host
9
9
  attr_accessor :app_name, :ap_options, :appenders, :log_executor, :filter_parameters, :metrics, :error_handler
10
10
 
11
11
  SUPPORTED_EXECUTORS = %i(single_thread_executor immediate_executor).freeze
12
12
  DEFAULT = {
13
- log_level: :info,
13
+ log_level: :info,
14
+ log_level_active_record: :info,
14
15
  host: nil,
15
16
  ap_options: { multiline: false },
16
17
  appenders: [{ stream: { io: STDOUT, formatter: :color } }],
@@ -26,7 +27,8 @@ module Sapience
26
27
  @options = DEFAULT.merge(options.dup.deep_symbolize_keyz!)
27
28
  @options[:log_executor] &&= @options[:log_executor].to_sym
28
29
  validate_log_executor!(@options[:log_executor])
29
- self.default_level = @options[:log_level].to_sym
30
+ self.default_level = @options[:log_level].to_sym
31
+ self.log_level_active_record = @options[:log_level_active_record].to_sym
30
32
  self.backtrace_level = @options[:log_level].to_sym
31
33
  self.host = @options[:host]
32
34
  self.app_name = @options[:app_name]
@@ -45,6 +47,13 @@ module Sapience
45
47
  @default_level_index = level_to_index(level)
46
48
  end
47
49
 
50
+ # Sets the active record log level
51
+ def log_level_active_record=(level)
52
+ @log_level_active_record = level
53
+ # For performance reasons pre-calculate the level index
54
+ @log_level_active_record_index = level_to_index(level)
55
+ end
56
+
48
57
  # Returns the symbolic level for the supplied level index
49
58
  def index_to_level(level_index)
50
59
  LEVELS[level_index]
@@ -18,6 +18,30 @@ module Sapience
18
18
  @logger = @options[:logger]
19
19
  end
20
20
 
21
+ protected
22
+
23
+ def call!(env)
24
+ @env = env
25
+ before
26
+ error = catch(:error) do
27
+ begin
28
+ @app_response = @app.call(@env)
29
+ rescue => e
30
+ after_exception(e)
31
+ raise e
32
+ end
33
+ nil
34
+ end
35
+ if error
36
+ after_failure(error)
37
+ throw(:error, error)
38
+ else
39
+ @status, _, _ = *@app_response
40
+ after
41
+ end
42
+ @app_response
43
+ end
44
+
21
45
  def before
22
46
  reset_db_runtime
23
47
  start_time
@@ -26,16 +50,18 @@ module Sapience
26
50
  def after
27
51
  stop_time
28
52
  @logger.info(parameters)
29
- nil
30
53
  end
31
54
 
32
- protected
55
+ def after_exception(e)
56
+ Sapience.push_tags(e.class.name, e.message)
57
+ @status = 500
58
+ after
59
+ end
33
60
 
34
- # TODO: Is NoMethodError :[] equal to `404`?
35
- def response
36
- super
37
- rescue
38
- nil
61
+ def after_failure(error)
62
+ Sapience.push_tags(error[:message])
63
+ @status = error[:status]
64
+ after
39
65
  end
40
66
 
41
67
  def parameters # rubocop:disable AbcSize
@@ -43,7 +69,7 @@ module Sapience
43
69
  method: request.request_method,
44
70
  request_path: request.path,
45
71
  format: request_format(request.env),
46
- status: response.try(:status) || 404,
72
+ status: @status,
47
73
  class_name: env["api.endpoint"].options[:for].to_s,
48
74
  action: "index",
49
75
  host: request.host,
@@ -90,6 +116,7 @@ module Sapience
90
116
  def stop_time
91
117
  @stop_time ||= Time.now
92
118
  end
119
+
93
120
  end
94
121
  end
95
122
  end
@@ -73,8 +73,9 @@ module Sapience
73
73
  Sapience::Extensions::ActionController::LogSubscriber.attach_to :action_controller
74
74
  # Sapience::Extensions::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer
75
75
  if defined?(ActiveRecord)
76
- Sapience::Extensions::ActiveRecord::LogSubscriber.attach_to :active_record
77
76
  Sapience::Extensions::ActiveRecord::Notifications.use
77
+ ActiveRecord::Base.logger.level =
78
+ Sapience.config.log_level_active_record if defined?(ActiveRecord::Base.logger.level)
78
79
  end
79
80
 
80
81
  Sapience::Extensions::ActionView::LogSubscriber.attach_to :action_view
@@ -1,3 +1,3 @@
1
1
  module Sapience
2
- VERSION = "2.0.2"
2
+ VERSION = "2.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sapience
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-31 00:00:00.000000000 Z
12
+ date: 2017-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby
@@ -378,7 +378,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
378
378
  version: '0'
379
379
  requirements: []
380
380
  rubyforge_project:
381
- rubygems_version: 2.6.7
381
+ rubygems_version: 2.5.1
382
382
  signing_key:
383
383
  specification_version: 4
384
384
  summary: Hasslefree autoconfiguration for logging, metrics and exception collection.