researchable_loggable 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55ac4f81b15d9f88b44cd0e358fdf9ed04734a512900fdb859ce9cb87a470aaa
4
- data.tar.gz: 8ba8d690974a6b46fd41271f4c912982536df60726f9ba22fb4696e2f19923bb
3
+ metadata.gz: 26d7a5d5e93a66cb06484a698b260df9ac1867adef2dbb61085a9f420881da2e
4
+ data.tar.gz: 0b59c63f4400c28f3c1f562928f42d3a2de4c458248744ae265149079d310294
5
5
  SHA512:
6
- metadata.gz: d1f27347e3330fd2354d91546b943687a1ad8d4ee7cef1fcb532e0937bed62291ec8b2445dd60f8ab5cc25fb7ba3e76cc9f373b4d9bbafdb1855b552f4fd04bf
7
- data.tar.gz: accd88f616138d0a66e1a434b543f574f37eba588edac1a135f853777945613988ccc5b5449a38cae418fc55c5efcc733c2105760136ddb0e2758ca3f7ce1dfc
6
+ metadata.gz: a0b20c2fdae4e7cc063453d3e225bdfcb4049e4253c203f40a517c5d08679f0dd1233f5d4a1bf2ea84235a6cca70689e6d92189ed87fb6aafb4041ec68566dab
7
+ data.tar.gz: d9ddd309f6d6e38af84acf6aad3854e27519be1af7aaa6f3539dc1c736c0ae7520ba9af3557948e3db75176d09efdf29d8265afa4f68e6c853d4838b35f10fe6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.7.0](https://gitlab.com/researchable/general/gems/loggable/compare/v1.6.0...v1.7.0) (2025-02-27)
2
+
3
+
4
+ ### Features
5
+
6
+ * add disable_open_telemetry option ([7aa227b](https://gitlab.com/researchable/general/gems/loggable/commit/7aa227b4d0c4cc5392660581e541da0e408c0619))
7
+
8
+ # [1.6.0](https://gitlab.com/researchable/general/gems/loggable/compare/v1.5.1...v1.6.0) (2025-01-15)
9
+
10
+
11
+ ### Features
12
+
13
+ * **logging:** add request.id, file, and line to log payload ([f3db220](https://gitlab.com/researchable/general/gems/loggable/commit/f3db2209b6046aa6ce6ab9f0f052d9316b4508b9))
14
+
1
15
  ## [1.5.1](https://gitlab.com/researchable/general/gems/loggable/compare/v1.5.0...v1.5.1) (2024-12-05)
2
16
 
3
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- researchable_loggable (1.3.1)
4
+ researchable_loggable (1.5.1)
5
5
  lograge
6
6
  logstash-event
7
7
  opentelemetry-exporter-otlp (~> 0.28)
data/README.md CHANGED
@@ -30,12 +30,12 @@ readable form. Development logs remain unaffected (see [Usage](#usage) for more
30
30
 
31
31
  ## How it works
32
32
  Loggable is a Railtie that overrides the default rails logger configuration. Right from the initialization process, the
33
- logger is set to `::EcsLogging::Logger` and the default log level is set to `INFO` (this can be overridden in
34
- application.rb or your environment configuration). It also configures lograge automatically. Note that the
33
+ logger is set to `::EcsLogging::Logger` and the default log level is set to `INFO` (this can be overridden in
34
+ application.rb or your environment configuration). It also configures lograge automatically. Note that the
35
35
  base_controller_class is set to `'ApplicationController'`. All your controllers should extend from this class.
36
36
 
37
37
  By default lograge and ::EcsLogging::Logger are not mutually compatible. The ECS formatter will try to render the output
38
- of lograge as the message, however some of the fields in the lograge output are metadata. To fix this, this gem uses a
38
+ of lograge as the message, however some of the fields in the lograge output are metadata. To fix this, this gem uses a
39
39
  monkey patch on the ECS gem that flattens the lograge output onto the Ecs object.
40
40
 
41
41
  ## Installation
@@ -65,11 +65,11 @@ gem will automatically configure the Rails logger and lograge to follow the Rese
65
65
  ### Parameters
66
66
  This are the parameters that can be configured in the `config.loggable` namespace:
67
67
 
68
- `production_like`. By default loggable will only
69
- modify how the logs look on production. This is because the format that this gem enforces is slightly less
70
- human readable (in favour of machine readability and compatibility with the Elastic Common Schema) than the default
68
+ `production_like`. By default loggable will only
69
+ modify how the logs look on production. This is because the format that this gem enforces is slightly less
70
+ human readable (in favour of machine readability and compatibility with the Elastic Common Schema) than the default
71
71
  Rails one. For development, the more human readable format is preferred. However, if you want to override this and
72
- log in the production format while on development (e.g. because you are troubleshooting a problem with the logs or you
72
+ log in the production format while on development (e.g. because you are troubleshooting a problem with the logs or you
73
73
  are developing against this gem) then you may add the following to your `config/environment/development.rb` file:
74
74
  ```ruby
75
75
  config.loggable.production_like = true
@@ -81,17 +81,22 @@ config.loggable.current_user_method = :my_current_user_method
81
81
  ```
82
82
  This method is used for ECS logging, if the method is not found, the user.id field will be set to nil.
83
83
 
84
+ `disable_open_telemetry`. By default, loggable will try to inject the open telemetry trace ids into the log line. If you want to disable this, you may add the following to your `config/application.rb` file:
85
+ ```ruby
86
+ config.loggable.disable_open_telemetry = true
87
+ ```
88
+
84
89
  No other configuration options are provided because this gem seeks to enforce consistency across all applications.
85
90
 
86
91
  For applications that already had some level of log customization, some cleanup might be needed:
87
92
 
88
- 1. If lograge was added to the Gemfile already, it may be removed. Loggable relies on lograge internally, and will
93
+ 1. If lograge was added to the Gemfile already, it may be removed. Loggable relies on lograge internally, and will
89
94
  make it available to the Rails application.
90
95
  2. Consequently, remove `config/lograge.rb` and all lograge related configuration.
91
- 3. Remove all existing log configuration, specially for production environments. Places to look for this
92
- configuration are `config/application.rb` and `config/environment/production.rb`. If you chose to enable
93
- `production_like` mode on your development environment, you might also want to cleanup
94
- `config/environment/development.rb`. Things to look for when cleaning up are statements like `config.logger` and
96
+ 3. Remove all existing log configuration, specially for production environments. Places to look for this
97
+ configuration are `config/application.rb` and `config/environment/production.rb`. If you chose to enable
98
+ `production_like` mode on your development environment, you might also want to cleanup
99
+ `config/environment/development.rb`. Things to look for when cleaning up are statements like `config.logger` and
95
100
  `config.log_formatter` and anything with the word `log` or `logger` in it.
96
101
 
97
102
  ## Development
@@ -24,7 +24,13 @@ module Loggable
24
24
  # Custom formatter class that renders logs as in a key-value style using Logfmt formatting
25
25
  class KeyValueFormatter < ::Logger::Formatter
26
26
  def call(severity, timestamp, progname, msg)
27
- %(time=#{datetime(timestamp)} severity=#{severity.ljust(5)}#{progname(progname)} #{message(msg)}\n)
27
+ file, line = extract_caller_info
28
+ file = relative_to_rails_root(file)
29
+
30
+ file_info = format_tag('file', file)
31
+ line_info = format_tag('line', line)
32
+
33
+ %(time=#{datetime(timestamp)} severity=#{severity.ljust(5)}#{file_info}#{line_info}#{progname(progname)} #{message(msg)}\n) # rubocop:disable Layout/LineLength
28
34
  end
29
35
 
30
36
  private
@@ -80,6 +86,47 @@ module Loggable
80
86
  value
81
87
  end
82
88
  end
89
+
90
+ # Extracts caller information (file and line number) for the log message
91
+ def extract_caller_info
92
+ caller_locations.each do |loc|
93
+ # Skip frames that are part of internal or irrelevant paths (gems, vendor, etc.)
94
+ next if internal_frame?(loc.path)
95
+
96
+ # Return the first relevant application-level frame
97
+ return [loc.path, loc.lineno]
98
+ end
99
+
100
+ # Fallback if no relevant frame is found
101
+ %w[unknown unknown]
102
+ end
103
+
104
+ # Determines if a given file path corresponds to an internal or excluded frame
105
+ def internal_frame?(path)
106
+ excluded_patterns = %w[
107
+ /gems/
108
+ /vendor/
109
+ <internal:kernel>
110
+ loggable
111
+ ].freeze
112
+
113
+ excluded_patterns.any? { |pattern| path.include?(pattern) }
114
+ end
115
+
116
+ # Converts an absolute file path to a path relative to Rails.root
117
+ # If the path is outside of Rails.root, it returns the original path
118
+ def relative_to_rails_root(path)
119
+ rails_root = defined?(Rails) ? Rails.root.to_s : ''
120
+ path.start_with?(rails_root) ? path.sub(%r{^#{rails_root}/}, '') : path
121
+ end
122
+
123
+ # Formats a key-value tag for log output
124
+ # If the value is 'unknown', it returns an empty string to omit the tag
125
+ def format_tag(key, value)
126
+ return '' if value == 'unknown'
127
+
128
+ " #{key}=#{value}"
129
+ end
83
130
  end
84
131
  end
85
132
  end
@@ -18,6 +18,8 @@ module Loggable
18
18
  initializer :loggable_web, before: :initialize_logger do
19
19
  Rails.application.configure do
20
20
  production_like = Rails.env.production? || config.loggable.production_like
21
+ disable_open_telemetry = config.loggable.disable_open_telemetry
22
+
21
23
  if production_like
22
24
  if Rails.application.config.respond_to?(:assets) && Rails.application.config.assets.quiet
23
25
  Loggable::Logfmt::Logger.include_logger_silence
@@ -25,7 +27,9 @@ module Loggable
25
27
  Rails.logger = Loggable::Logfmt::Logger.new($stdout)
26
28
  Rails.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase || 'INFO')
27
29
 
28
- Loggable::OpenTelemetryConfig.configure(Rails.application.class.module_parent_name.underscore.dasherize)
30
+ unless disable_open_telemetry
31
+ Loggable::OpenTelemetryConfig.configure(Rails.application.class.module_parent_name.underscore.dasherize)
32
+ end
29
33
  end
30
34
 
31
35
  # lograge configuration
@@ -37,13 +41,22 @@ module Loggable
37
41
  user_id = controller.send(config.loggable.current_user_method).try(:id)
38
42
  end
39
43
 
40
- {
44
+ # Look for the request ID in headers
45
+ request_id = controller.request.headers['X-Request-ID'] || # Check for a custom request ID header
46
+ controller.request.headers['traceparent'] || # Check for the W3C Trace Context header
47
+ controller.request.env['HTTP_X_REQUEST_ID'] # Fallback to Rack's normalized header
48
+
49
+ payload = {
41
50
  'source.ip': controller.request.ip,
42
51
  'user.id': user_id,
43
- status: response_code,
44
- span_id: OpenTelemetry::Trace.current_span.context.hex_span_id,
45
- trace_id: OpenTelemetry::Trace.current_span.context.hex_trace_id
52
+ 'request.id': request_id,
53
+ status: response_code
46
54
  }
55
+ if production_like && !disable_open_telemetry
56
+ payload[:span_id] = OpenTelemetry::Trace.current_span.context.hex_span_id
57
+ payload[:trace_id] = OpenTelemetry::Trace.current_span.context.hex_trace_id
58
+ end
59
+ payload
47
60
  end
48
61
  config.lograge.custom_options = lambda do |event|
49
62
  return if event.payload[:params].blank?
@@ -66,8 +79,10 @@ module Loggable
66
79
  if Rails.env.production? || config.loggable.production_like
67
80
  Delayed::Worker.logger = Loggable::Logfmt::Logger.new($stdout)
68
81
  Delayed::Worker.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase || 'INFO')
69
- Loggable::OpenTelemetryConfig.configure("#{Rails.application.class.module_parent_name.underscore.dasherize}
70
- -worker")
82
+ unless config.loggable.disable_open_telemetry
83
+ worker_name = "#{Rails.application.class.module_parent_name.underscore.dasherize}-worker"
84
+ Loggable::OpenTelemetryConfig.configure(worker_name)
85
+ end
71
86
  else
72
87
  Delayed::Worker.logger = Logger.new($stdout)
73
88
  Delayed::Worker.logger.level = Logger::DEBUG
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loggable
4
- VERSION = '1.5.1'
4
+ VERSION = '1.7.0'
5
5
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestGem
4
- VERSION = '1.5.1'
4
+ VERSION = '1.7.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: researchable_loggable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Researchable
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-05 00:00:00.000000000 Z
11
+ date: 2025-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lograge
@@ -109,13 +109,13 @@ files:
109
109
  - node_modules/semantic-release-rubygem/src/__tests__/fixtures/prerelease/lib/test-gem/version.rb
110
110
  - node_modules/semantic-release-rubygem/src/__tests__/fixtures/valid/lib/test-gem/version.rb
111
111
  - sig/loggable.rbs
112
- homepage: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.5.1/README.md
112
+ homepage: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.7.0/README.md
113
113
  licenses:
114
114
  - MIT
115
115
  metadata:
116
- homepage_uri: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.5.1/README.md
116
+ homepage_uri: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.7.0/README.md
117
117
  source_code_uri: https://gitlab.com/researchable/general/gems/loggable
118
- changelog_uri: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.5.1/CHANGELOG.md
118
+ changelog_uri: https://gitlab.com/researchable/general/gems/loggable/-/blob/v1.7.0/CHANGELOG.md
119
119
  post_install_message:
120
120
  rdoc_options: []
121
121
  require_paths: