appydays 0.5.0 → 0.6.0

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
  SHA256:
3
- metadata.gz: ab72febd501e6dada1c4738598be500f24b98ab57183a8674e93e6622a82aaf4
4
- data.tar.gz: c906e8cc5af47912035269aada22d832dfcb338079ac210207c2cfa26ac3bc64
3
+ metadata.gz: 91b081811b26e7dee2ab9ec7fb8641ef890124d72694a3cfe858ed7f72011afb
4
+ data.tar.gz: 7cc0dd2e62d643695208f542029c25363a906bb60058fd9fbb47c48f06dc69ee
5
5
  SHA512:
6
- metadata.gz: 65b183daf0a109fe1dc95f06a4bb1e842489541fd1fab7ec1c2794766a664334dd53e8b7adb8da5a80446fffe46bad7ff41c8e6076465d4fea05182c0956906b
7
- data.tar.gz: 4608508c05777a02fa77983b9d91cb597e4f995cd1b78af7ea363a301d91ec31245357f97610f658e25a577d9a96778a6fd5b798af3f62ef9a81cd6a8a450187
6
+ metadata.gz: f39eac94f128c900cb873d1d3a9f486a55b86ff7a38859ef32bb375cd4842cb0fd5fb39544aa172c6cd5b659a9bf6b1dc5d8a120dde08660ef1cea5a83d77aa6
7
+ data.tar.gz: 1d8ad64ba366d89fd75bf471895a1e16b88fa74346bae806f4a69205fee83be8371dc9f693439e327b53d13263806b60266eac1222244a00370929f43b834e99
@@ -49,10 +49,14 @@ module Appydays::Configurable
49
49
  end
50
50
 
51
51
  ##
52
- # Restore all settings back to the values they were at config time.
53
- # Ie, undoes any manual attribute writes.
54
- def reset_configuration
55
- self._configuration_installer._reset
52
+ # Restore all settings back to the values they were at config time
53
+ # (undoes any manual attribute writes), and runs after_configured hooks.
54
+ #
55
+ # overrides can be passed, to apply new manual overrides
56
+ # before running after_configured hooks.
57
+ # This is very useful when testing classes that have an after_configured hook.
58
+ def reset_configuration(overrides={})
59
+ self._configuration_installer._reset(overrides)
56
60
  end
57
61
 
58
62
  ##
@@ -154,9 +158,10 @@ module Appydays::Configurable
154
158
  end
155
159
  end
156
160
 
157
- def _reset
161
+ def _reset(overrides)
158
162
  @settings.each do |k, v|
159
- @target.send("#{k}=".to_sym, v)
163
+ real_v = overrides.fetch(k, v)
164
+ @target.send("#{k}=".to_sym, real_v)
160
165
  end
161
166
  self._run_after_configured
162
167
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+
5
+ # Formatter that sends structred log information to HTTParty.
6
+ # After requiring this module, use
7
+ # `HTTParty.<method>(..., logger: semantic_logger, log_format: :appydays)`
8
+ # to write out a nice structured log.
9
+ # You can also subclass this formatter to use your own message (default to httparty_request),
10
+ # and modify the fields (override #fields).
11
+ class Appydays::Loggable::HTTPartyFormatter
12
+ attr_accessor :level, :logger, :message
13
+ attr_reader :request, :response
14
+
15
+ def initialize(logger, level)
16
+ @logger = logger
17
+ @level = level.to_sym
18
+ @message = "httparty_request"
19
+ end
20
+
21
+ def format(request, response)
22
+ @request = request
23
+ @response = response
24
+ self.logger.public_send(self.level, self.message, **self.fields)
25
+ end
26
+
27
+ def fields
28
+ return {
29
+ "content_length" => content_length || "-",
30
+ "http_method" => http_method,
31
+ "path" => path,
32
+ "response_code" => response.code,
33
+ }
34
+ end
35
+
36
+ def http_method
37
+ @http_method ||= request.http_method.name.split("::").last.upcase
38
+ end
39
+
40
+ def path
41
+ @path ||= request.path.to_s
42
+ end
43
+
44
+ def content_length
45
+ @content_length ||= response.respond_to?(:headers) ? response.headers["Content-Length"] : response["Content-Length"]
46
+ end
47
+ end
48
+
49
+ HTTParty::Logger.add_formatter(:appydays, Appydays::Loggable::HTTPartyFormatter)
@@ -4,6 +4,32 @@
4
4
  require "sequel/database/logging"
5
5
 
6
6
  class Sequel::Database
7
+ # Helpers for the Appydays Sequel logger.
8
+ # Very long messages may end up getting logged; the logger will truncate anything
9
+ # longer than +truncate_messages_over+, with +truncation_context+ number of chars
10
+ # at the beginning and at the end.
11
+ #
12
+ # If a message is truncated, the full message is logged at +log_full_message_level+ (default :debug).
13
+ # Use nil to disable the full message logging.
14
+ module AppydaysLogger
15
+ class << self
16
+ attr_accessor :truncate_messages_over, :truncation_message, :truncation_context, :log_full_message_level
17
+
18
+ def setdefaults
19
+ @truncate_messages_over = 2000
20
+ @truncation_message = "<truncated, full message logged at debug>"
21
+ @truncation_context = 200
22
+ end
23
+
24
+ def truncate_message(message)
25
+ return message if message.size <= self.truncate_messages_over
26
+ msg = message[...self.truncation_context] + self.truncation_message + message[-self.truncation_context..]
27
+ return msg
28
+ end
29
+ end
30
+ end
31
+ AppydaysLogger.setdefaults
32
+
7
33
  def log_exception(exception, message)
8
34
  level = message.match?(/^SELECT NULL AS "?nil"? FROM .* LIMIT 1$/i) ? :debug : :error
9
35
  log_each(
@@ -30,10 +56,25 @@ class Sequel::Database
30
56
  # warn level if duration is greater than log_warn_duration.
31
57
  def log_duration(duration, message)
32
58
  lwd = log_warn_duration
59
+ was_truncated = false
33
60
  log_each(
34
61
  lwd && (duration >= lwd) ? :warn : sql_log_level,
35
62
  proc { "(#{'%0.6fs' % duration}) #{message}" },
36
- proc { ["sequel_query", {duration: duration * 1000, query: message}] },
63
+ proc do
64
+ query = AppydaysLogger.truncate_message(message)
65
+ params = {duration: duration * 1000, query: query}
66
+ if query != message
67
+ params[:truncated] = true
68
+ was_truncated = true
69
+ end
70
+ ["sequel_query", params]
71
+ end,
72
+ )
73
+ return unless was_truncated && Sequel::Database::AppydaysLogger.log_full_message_level
74
+ log_each(
75
+ Sequel::Database::AppydaysLogger.log_full_message_level,
76
+ nil,
77
+ proc { ["sequel_query_debug", {duration: duration * 1000, query: message}] },
37
78
  )
38
79
  end
39
80
 
@@ -41,7 +82,7 @@ class Sequel::Database
41
82
  @loggers.each do |logger|
42
83
  if logger.is_a?(SemanticLogger::Base)
43
84
  logger.public_send(level, *semantic.call)
44
- else
85
+ elsif std
45
86
  logger.public_send(level, std.call)
46
87
  end
47
88
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Appydays
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appydays
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lithic Tech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2023-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.20'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.20'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: monetize
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +220,20 @@ dependencies:
206
220
  - - "~>"
207
221
  - !ruby/object:Gem::Version
208
222
  version: '6.0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: webmock
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '3.1'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '3.1'
209
237
  description: 'appydays provides support for env-based configuration, and common structured
210
238
  logging capabilities
211
239
 
@@ -218,6 +246,7 @@ files:
218
246
  - lib/appydays/configurable.rb
219
247
  - lib/appydays/dotenviable.rb
220
248
  - lib/appydays/loggable.rb
249
+ - lib/appydays/loggable/httparty_formatter.rb
221
250
  - lib/appydays/loggable/request_logger.rb
222
251
  - lib/appydays/loggable/sequel_logger.rb
223
252
  - lib/appydays/loggable/sidekiq_job_logger.rb