log_bench 0.1.8 → 0.2.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: cf603f1b73dcf743535f1732e8e1d8faa88a79eb3c54d549afd07faae49a5592
4
- data.tar.gz: 84dc80f53389ac160f3d03153a23a214fd737dc1df7600b1c57c52506d06d142
3
+ metadata.gz: 4f43bf244b5ac534d395472a7e2c785946512c25f7afda4376781eb2694b4469
4
+ data.tar.gz: e584eb93c784014e96b3814d1898fe00f41c5606909688b1bfdbc7fb1ddbf511
5
5
  SHA512:
6
- metadata.gz: f15102b09c8c19776338a9a40fd88482893f488d645be42c2c43192549f3786bf3268e86a2bd52d81c3159af2f6abd6334a09d62409fbc7cfd6d51fd439f8ce9
7
- data.tar.gz: aa1bb3b7decbbf3f18abbd75759b40a9857582b9e9d99037dce82eb56c17a77f652c42ec799ce822a149ac40a7efd96bea1f5da68eedb6366ae383008914a39f
6
+ metadata.gz: 20da922103a9ad3b33ef124218c91ff8167ff04daff9194aff400b52757fa17405f49705c676d53ac2ae0259541bf63a19ae29bb299e6f194bb86837e13ccfbe
7
+ data.tar.gz: dfa5c5a737c9b087dc2d276b58bbc4da10c96a022ce9314dfe9f139165bf68150bac1267c1eb2fde7111913fd37c7397c686c578dbaf5e012981e06b19557ef8
data/README.md CHANGED
@@ -32,61 +32,65 @@ bundle install
32
32
 
33
33
  ## Configuration
34
34
 
35
- ### 1. Configure Rails Logging
35
+ LogBench is **automatically enabled in development environment** for backward compatibility. This gem heavily relies on [lograge](https://github.com/roidrage/lograge) gem for request tracking, we add lograge to your Rails application and configure it automatically, but you can also configure lograge manually if you want.
36
36
 
37
- Add this configuration to your `config/environments/development.rb`:
37
+ ### Default Behavior
38
38
 
39
- ```ruby
40
- # config/environments/development.rb
41
- require "lograge"
39
+ LogBench works out of the box! Just add the gem and restart your Rails server.
42
40
 
43
- Rails.application.configure do
44
- # ... other configuration ...
41
+ It automatically configures:
42
+ - Lograge with JSON formatter and custom options
43
+ - Rails logger with LogBench::JsonFormatter
44
+ - Request ID tracking via LogBench::Current
45
+ - Automatic injection of request_id into ApplicationController
45
46
 
46
- # LogBench: Configure structured logging
47
- config.lograge.enabled = true
48
- config.lograge.formatter = Lograge::Formatters::Json.new
49
- config.log_bench.show_init_message = :full # or :min or :none
50
- config.lograge.custom_options = lambda do |event|
51
- params = event.payload[:params]&.except("controller", "action")
52
- { params: params } if params.present?
53
- end
54
- config.logger ||= ActiveSupport::Logger.new(config.default_log_file)
55
- config.logger.formatter = LogBench::JsonFormatter.new
56
- end
57
- ```
47
+ No configuration needed in development!
58
48
 
59
- ### 2. Set Up Request ID Tracking
49
+ ### Custom Configuration (Optional)
60
50
 
61
- Create or update your `Current` model to track request IDs:
51
+ To customize LogBench behavior, create `config/initializers/log_bench.rb`:
62
52
 
63
53
  ```ruby
64
- # app/models/current.rb
65
- # frozen_string_literal: true
54
+ # config/initializers/log_bench.rb
55
+ LogBench.setup do |config|
56
+ # Enable/disable LogBench (default: true in development, false elsewhere)
57
+ config.enabled = Rails.env.development? # or any other condition
58
+
59
+ # Disable automatic lograge configuration (if you want to configure lograge manually)
60
+ # config.configure_lograge_automatically = false # (default: true)
66
61
 
67
- class Current < ActiveSupport::CurrentAttributes
68
- attribute :request_id
62
+ # Customize initialization message
63
+ # config.show_init_message = :min # :full, :min, or :none (default: :full)
64
+
65
+ # Specify which controllers to inject request_id tracking
66
+ # config.base_controller_classes = %w[CustomBaseController] # (default: %w[ApplicationController, ActionController::Base])
69
67
  end
70
68
  ```
71
69
 
72
- Add request ID tracking to your ApplicationController:
70
+ ### Manual Lograge Configuration
71
+
72
+ If you already have lograge configured or want to manage it manually:
73
73
 
74
74
  ```ruby
75
- # app/controllers/application_controller.rb
76
- class ApplicationController < ActionController::Base
77
- before_action :set_current_request_identifier
75
+ # config/initializers/log_bench.rb
76
+ LogBench.setup do |config|
77
+ # ... other config ...
78
+ config.configure_lograge_automatically = false # Don't touch my lograge config!
79
+ end
78
80
 
79
- protected
81
+ # Then configure lograge yourself in config/environments/development.rb or an initializer,
80
82
 
81
- def set_current_request_identifier
82
- Current.request_id = request.request_id
83
+ Rails.application.configure do
84
+ config.lograge.enabled = true
85
+ config.lograge.formatter = Lograge::Formatters::Json.new
86
+ config.lograge.custom_options = lambda do |event|
87
+ # Your custom lograge configuration
88
+ params = event.payload[:params]&.except("controller", "action")
89
+ { params: params } if params.present?
83
90
  end
84
91
  end
85
92
  ```
86
-
87
- ### 3. Restart Your Rails Server
88
-
89
- After configuration, restart your Rails development server:
93
+ For more information about lograge configuration, see [Lograge's documentation](https://github.com/roidrage/lograge).
90
94
 
91
95
  ## Usage
92
96
 
data/exe/log_bench CHANGED
@@ -18,12 +18,11 @@ def show_help
18
18
  log_bench /path/to/app/log/test.log # View log from another app
19
19
 
20
20
  Setup (for Rails apps):
21
- See README.md for complete configuration instructions:
22
- https://github.com/silva96/log_bench#installation
21
+ LogBench is automatically enabled in development!
22
+ Just restart your Rails server after installing the gem.
23
23
 
24
- Requirements:
25
- - Lograge gem with JSON formatter
26
- - Rails application logs in lograge format
24
+ To customize or enable in other environments:
25
+ For complete setup: https://github.com/silva96/log_bench#configuration
27
26
 
28
27
  For more info: https://github.com/silva96/log_bench
29
28
  HELP
@@ -21,7 +21,6 @@ module LogBench
21
21
  self.log_file_path = find_log_file(log_file_path)
22
22
  self.state = State.new
23
23
  validate_log_file!
24
- validate_configuration!
25
24
  end
26
25
 
27
26
  def run
@@ -57,13 +56,6 @@ module LogBench
57
56
  File.basename(log_file_path)
58
57
  end
59
58
 
60
- def validate_configuration!
61
- ConfigurationValidator.validate_rails_config!
62
- rescue ConfigurationValidator::ConfigurationError => e
63
- puts e.message
64
- exit 1
65
- end
66
-
67
59
  def setup_screen
68
60
  self.screen = Screen.new
69
61
  screen.setup
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LogBench
4
+ class Configuration
5
+ attr_accessor :show_init_message, :enabled, :base_controller_classes, :configure_lograge_automatically
6
+
7
+ def initialize
8
+ @show_init_message = :full
9
+ @enabled = Rails.env.development?
10
+ @base_controller_classes = %w[ApplicationController ActionController::Base]
11
+ @configure_lograge_automatically = true # Configure lograge by default
12
+ end
13
+ end
14
+ end
@@ -7,28 +7,19 @@ module LogBench
7
7
  ERROR_CONFIGS = {
8
8
  enabled: {
9
9
  title: "Lograge is not enabled",
10
- description: "LogBench requires lograge to be enabled in your Rails application.",
11
- fix: "config.lograge.enabled = true"
10
+ description: "LogBench requires lograge to be enabled in your Rails application."
12
11
  },
13
12
  options: {
14
13
  title: "Lograge custom_options missing",
15
- description: "LogBench needs custom_options to include params fields.",
16
- fix: <<~FIX.strip
17
- config.lograge.custom_options = lambda do |event|
18
- params = event.payload[:params]&.except("controller", "action")
19
- { params: params } if params.present?
20
- end
21
- FIX
14
+ description: "LogBench needs custom_options to include params fields."
22
15
  },
23
16
  lograge_formatter: {
24
17
  title: "Wrong lograge formatter",
25
- description: "LogBench requires Lograge::Formatters::Json for lograge formatting.",
26
- fix: "config.lograge.formatter = Lograge::Formatters::Json.new"
18
+ description: "LogBench requires Lograge::Formatters::Json for lograge formatting."
27
19
  },
28
20
  logger_formatter: {
29
21
  title: "Wrong Rails logger formatter",
30
- description: "LogBench requires LogBench::JsonFormatter for Rails logger formatting.",
31
- fix: "config.logger.formatter = LogBench::JsonFormatter.new"
22
+ description: "LogBench requires LogBench::JsonFormatter for Rails logger formatting."
32
23
  }
33
24
  }.freeze
34
25
 
@@ -38,6 +29,7 @@ module LogBench
38
29
 
39
30
  def validate_rails_config!
40
31
  return true unless defined?(Rails) && Rails.application
32
+ return true unless LogBench.configuration.enabled
41
33
 
42
34
  validate_lograge_enabled!
43
35
  validate_custom_options!
@@ -69,8 +61,11 @@ module LogBench
69
61
  end
70
62
 
71
63
  def validate_logger_formatter!
72
- logger = Rails.application.config.logger
64
+ # Check Rails.logger directly since that's what we set now
65
+ logger = Rails.logger
73
66
  formatter = logger&.formatter
67
+ # Allow LogBench::JsonFormatter or any custom JSON formatter
68
+ # Users might have their own JSON formatters that work with LogBench
74
69
  unless formatter.is_a?(LogBench::JsonFormatter)
75
70
  raise ConfigurationError, build_error_message(:logger_formatter)
76
71
  end
@@ -89,8 +84,7 @@ module LogBench
89
84
 
90
85
  #{config[:description]}
91
86
 
92
- Add this to config/environments/development.rb:
93
- #{config[:fix]}
87
+ This should be automatically configured by LogBench, but something went wrong.
94
88
 
95
89
  For complete setup: https://github.com/silva96/log_bench#configuration
96
90
  ERROR
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(ActiveSupport::CurrentAttributes)
4
+ module LogBench
5
+ class Current < ActiveSupport::CurrentAttributes
6
+ attribute :request_id
7
+ end
8
+ end
9
+ end
@@ -66,7 +66,9 @@ module LogBench
66
66
  def current_request_id
67
67
  request_id = nil
68
68
 
69
- if defined?(Current) && Current.respond_to?(:request_id)
69
+ if defined?(LogBench::Current) && LogBench::Current.respond_to?(:request_id)
70
+ request_id = LogBench::Current.request_id
71
+ elsif defined?(Current) && Current.respond_to?(:request_id)
70
72
  request_id = Current.request_id
71
73
  elsif defined?(RequestStore) && RequestStore.exist?(:request_id)
72
74
  request_id = RequestStore.read(:request_id)
@@ -3,79 +3,115 @@ require "rails/railtie"
3
3
  module LogBench
4
4
  class Railtie < Rails::Railtie
5
5
  LINE = "=" * 70
6
+ HELP_INSTRUCTIONS = "For help: log_bench --help"
7
+ MIN_INIT_MESSAGE = "✅ LogBench is ready to use!"
8
+ FULL_INIT_MESSAGE = <<~MSG.chomp
9
+ #{MIN_INIT_MESSAGE}
10
+ View your logs: log_bench log/development.log
11
+ MSG
6
12
 
7
13
  railtie_name :log_bench
8
14
 
9
15
  config.log_bench = ActiveSupport::OrderedOptions.new
10
16
 
11
- # LogBench uses manual configuration (see README.md)
12
-
13
17
  # Provide helpful rake tasks
14
18
  rake_tasks do
15
19
  load "tasks/log_bench.rake"
16
20
  end
17
21
 
18
- initializer "log_bench.configure" do |app|
19
- LogBench.setup do |config|
20
- config.show_init_message = app.config.log_bench.show_init_message
21
- config.show_init_message = :full if config.show_init_message.nil?
22
- end
22
+ # Run AFTER user initializers to pick up their configuration
23
+ initializer "log_bench.configure", after: :load_config_initializers do |app|
24
+ LogBench.setup
23
25
  end
24
26
 
25
- # Show installation instructions when Rails starts in development
27
+ # Show success message when Rails starts in development
26
28
  initializer "log_bench.show_instructions", after: :load_config_initializers do
27
- return unless Rails.env.development?
29
+ next unless Rails.env.development? && LogBench.configuration.enabled
28
30
 
29
- # Use configuration validator to check lograge setup
30
- begin
31
- ConfigurationValidator.validate_rails_config!
32
- # Lograge is properly configured
33
- if LogBench.configuration.show_init_message.eql? :full
34
- print_full_init_message
35
- print_help_instructions
36
- puts LINE
37
- puts LINE
38
- elsif LogBench.configuration.show_init_message.eql? :min
39
- print_min_init_message
31
+ print_configured_init_message
32
+ rescue => e
33
+ puts LINE
34
+ puts "⚠️ LogBench setup issue: #{e.message} at: \n#{e.backtrace.join("\n")}"
35
+ puts HELP_INSTRUCTIONS
36
+ puts LINE
37
+ end
38
+
39
+ # Single after_initialize for ALL setup that needs to happen after Rails is ready
40
+ config.after_initialize do |app|
41
+ if LogBench.configuration.enabled
42
+ LogBench::Railtie.setup_lograge(app)
43
+ LogBench::Railtie.setup_current_attributes
44
+ LogBench::Railtie.setup_rails_logger_final
45
+ LogBench::Railtie.validate_configuration!
46
+ end
47
+ end
48
+
49
+ class << self
50
+ def setup_lograge(app)
51
+ return unless LogBench.configuration.configure_lograge_automatically
52
+
53
+ app.config.lograge.enabled = true
54
+ app.config.lograge.formatter = Lograge::Formatters::Json.new
55
+ app.config.lograge.custom_options = lambda do |event|
56
+ params = event.payload[:params]&.except("controller", "action")
57
+ {params: params} if params.present?
40
58
  end
59
+ end
60
+
61
+ def setup_current_attributes
62
+ # Inject Current.request_id into base controllers
63
+ LogBench.configuration.base_controller_classes.each do |controller_class_name|
64
+ controller_class = controller_class_name.safe_constantize
65
+ next unless controller_class
66
+
67
+ inject_current_request_id(controller_class)
68
+ end
69
+ end
70
+
71
+ # Setup Rails logger by re-wrapping with TaggedLogging
72
+ def setup_rails_logger_final
73
+ # Get the underlying logger (unwrap TaggedLogging if present)
74
+ base_logger = Rails.logger.respond_to?(:logger) ? Rails.logger.logger : Rails.logger
75
+ base_logger.formatter = LogBench::JsonFormatter.new
76
+ # Re-wrap with TaggedLogging to maintain Rails compatibility
77
+ Rails.logger = ActiveSupport::TaggedLogging.new(base_logger)
78
+ end
79
+
80
+ def inject_current_request_id(controller_class)
81
+ return if controller_class.method_defined?(:set_current_request_id)
82
+
83
+ controller_class.class_eval do
84
+ before_action :set_current_request_id
85
+
86
+ private
87
+
88
+ def set_current_request_id
89
+ LogBench::Current.request_id = request.request_id if defined?(LogBench::Current)
90
+ end
91
+ end
92
+ end
93
+
94
+ # Validate that LogBench setup worked correctly
95
+ def validate_configuration!
96
+ ConfigurationValidator.validate_rails_config!
41
97
  rescue ConfigurationValidator::ConfigurationError => e
42
- # Lograge needs configuration
43
- print_configuration_instructions
44
- puts "⚠️ Configuration issue: #{e.message}"
45
- print_help_instructions
46
98
  puts LINE
99
+ puts "❌ LogBench Configuration Error:"
100
+ puts e.message
101
+ puts "LogBench will be disabled until this is fixed."
47
102
  puts LINE
48
103
  end
49
104
  end
50
105
 
51
106
  private
52
107
 
53
- def print_configuration_instructions
54
- puts "🚀 LogBench is ready to configure!"
55
- puts LINE
56
- puts "To start using LogBench:"
57
- puts " 1. See README.md for configuration instructions"
58
- puts " 2. Configure lograge in config/environments/development.rb"
59
- puts " 3. Restart your Rails server"
60
- puts " 4. Make some requests to generate logs"
61
- puts " 5. View logs: log_bench log/development.log"
62
- puts ""
63
- end
64
-
65
- def print_min_init_message
66
- puts "✅ LogBench is ready to use!"
67
- end
68
-
69
- def print_full_init_message
70
- puts "\n" + LINE
71
- puts "\n" + LINE
72
- puts "✅ LogBench is ready to use!"
73
- puts LINE
74
- puts "View your logs: log_bench log/development.log"
75
- end
76
-
77
- def print_help_instructions
78
- puts "For help: log_bench --help"
108
+ def print_configured_init_message
109
+ case LogBench.configuration.show_init_message
110
+ when :full, nil
111
+ puts LINE, FULL_INIT_MESSAGE, HELP_INSTRUCTIONS, LINE
112
+ when :min
113
+ puts LINE, MIN_INIT_MESSAGE, LINE
114
+ end
79
115
  end
80
116
  end
81
117
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LogBench
4
- VERSION = "0.1.8"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/log_bench.rb CHANGED
@@ -4,7 +4,6 @@ require "zeitwerk"
4
4
  require "json"
5
5
  require "time"
6
6
  require "curses"
7
- require_relative "log_bench/version"
8
7
 
9
8
  loader = Zeitwerk::Loader.for_gem
10
9
  loader.ignore("#{__dir__}/generators")
@@ -13,24 +12,13 @@ loader.setup
13
12
  module LogBench
14
13
  class Error < StandardError; end
15
14
 
16
- class Configuration
17
- attr_accessor :show_init_message
18
- end
19
-
20
15
  class << self
21
16
  attr_accessor :configuration
22
17
 
23
- # Parse log lines and return an array of entries
24
- def parse(lines)
25
- lines = [lines] if lines.is_a?(String)
26
-
27
- collection = Log::Collection.new(lines)
28
- collection.requests
29
- end
30
-
31
18
  def setup
32
19
  self.configuration ||= Configuration.new
33
- yield(configuration)
20
+ yield(configuration) if block_given?
21
+ configuration
34
22
  end
35
23
  end
36
24
  end
@@ -3,14 +3,11 @@ namespace :log_bench do
3
3
  task :install do
4
4
  puts "LogBench Configuration Instructions:"
5
5
  puts
6
- puts "Please see the README.md for complete setup instructions:"
7
- puts "https://github.com/silva96/log_bench#configuration"
6
+ puts "LogBench is automatically enabled in development!"
7
+ puts "Just restart your Rails server and it will work."
8
8
  puts
9
- puts "Quick setup:"
10
- puts "1. Add 'require \"lograge\"' to config/environments/development.rb"
11
- puts "2. Configure lograge and JsonFormatter (see README)"
12
- puts "3. Set up Current model and ApplicationController"
13
- puts "4. Restart Rails server"
9
+ puts "For customization or other environments, see:"
10
+ puts "https://github.com/silva96/log_bench#configuration"
14
11
  end
15
12
 
16
13
  desc "Show LogBench usage instructions"
@@ -25,7 +22,7 @@ namespace :log_bench do
25
22
  2. Run bundle install:
26
23
  bundle install
27
24
 
28
- 3. Configure lograge (see README.md):
25
+ 3. Optionally configure log_bench (see README.md):
29
26
  https://github.com/silva96/log_bench#configuration
30
27
 
31
28
  4. Restart your Rails server
@@ -46,11 +43,7 @@ namespace :log_bench do
46
43
  - Interactive TUI with dual panes
47
44
  - Request filtering and sorting
48
45
  - SQL query analysis
49
- - Color-coded HTTP methods and status codes
50
-
51
- Requirements:
52
- - Lograge gem with JSON formatter
53
- - Rails application logs in lograge format
46
+ - Color-coded HTTP methods and status codess
54
47
  HELP
55
48
  end
56
49
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_bench
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamín Silva
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-06-18 00:00:00.000000000 Z
10
+ date: 2025-06-25 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: zeitwerk
@@ -139,7 +139,9 @@ files:
139
139
  - lib/log_bench/app/screen.rb
140
140
  - lib/log_bench/app/sort.rb
141
141
  - lib/log_bench/app/state.rb
142
+ - lib/log_bench/configuration.rb
142
143
  - lib/log_bench/configuration_validator.rb
144
+ - lib/log_bench/current.rb
143
145
  - lib/log_bench/json_formatter.rb
144
146
  - lib/log_bench/log/cache_entry.rb
145
147
  - lib/log_bench/log/call_line_entry.rb