log_bench 0.1.7 → 0.1.9

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: 6190e59783385726fb1b48af6daf05113619df53ab0f614927e40850fdb69d71
4
- data.tar.gz: fb9dad6de4675da021d49f9b2c6bf41e387b8bbbd7ae2637fa3345f7beb19682
3
+ metadata.gz: a57123bfa08abee40fdc9363b06794efddadd123c8b406fd8b6aea876f409d59
4
+ data.tar.gz: a0758d01ff5b148520a8d0ef02d6581641159b6065a3a04b61af0a3e15ca6f78
5
5
  SHA512:
6
- metadata.gz: '02058887b5e6192622b8397d163b53d9be4cf1b73b192a216284e40941b5833e7e485d144fe00c21c76face673e5c67036467ed28eaae95842109b81cce2b7b5'
7
- data.tar.gz: 5722c117f9eb0962007d0d21d8c38a3039df687b792cf9899531acfa438c3ac279637e2256bc6a04d5847634d52e386e18e7b1119c6c2278626ca09432660f3f
6
+ metadata.gz: e12b109df49933594454cf64aa0be3c6e345a1d0cbef3cc26fcd9b727f4c8dc181fefa332ad125f55cd9da2b0628c237315ac42c4fba751ba11e507dc6afa91c
7
+ data.tar.gz: c7c7d879edd623746f3b6a92ce8dd7a294a6c0c5a501ca985d71dc7b0fbe94c02e37393f543468531faa07122a3bacf1b008bee1a288948da8d1d00ec358721f
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
@@ -9,7 +9,7 @@ module LogBench
9
9
  # Application info
10
10
  APP_NAME = "LogBench"
11
11
  APP_SUBTITLE = "Rails Log Viewer"
12
- DEFAULT_LOG_FILENAME = "development.log"
12
+ VERSION = "(v#{LogBench::VERSION})"
13
13
 
14
14
  # Layout constants
15
15
  TITLE_X_OFFSET = 2
@@ -41,7 +41,7 @@ module LogBench
41
41
  def draw_title
42
42
  header_win.setpos(1, TITLE_X_OFFSET)
43
43
  header_win.attron(color_pair(HEADER_CYAN) | A_BOLD) { header_win.addstr(APP_NAME) }
44
- header_win.addstr(" - #{APP_SUBTITLE}")
44
+ header_win.addstr(" - #{APP_SUBTITLE} #{VERSION}")
45
45
  end
46
46
 
47
47
  def draw_file_name
@@ -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 = false
10
+ @base_controller_classes = %w[ApplicationController ActionController::Base]
11
+ @configure_lograge_automatically = true # Configure lograge by default
12
+ end
13
+ end
14
+ end
@@ -7,23 +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
- formatter: {
16
+ lograge_formatter: {
24
17
  title: "Wrong lograge formatter",
25
- description: "LogBench requires LogBench::JsonFormatter for proper log parsing.",
26
- fix: "config.lograge.formatter = LogBench::JsonFormatter.new"
18
+ description: "LogBench requires Lograge::Formatters::Json for lograge formatting."
19
+ },
20
+ logger_formatter: {
21
+ title: "Wrong Rails logger formatter",
22
+ description: "LogBench requires LogBench::JsonFormatter for Rails logger formatting."
27
23
  }
28
24
  }.freeze
29
25
 
@@ -33,10 +29,12 @@ module LogBench
33
29
 
34
30
  def validate_rails_config!
35
31
  return true unless defined?(Rails) && Rails.application
32
+ return true unless LogBench.configuration.enabled
36
33
 
37
34
  validate_lograge_enabled!
38
35
  validate_custom_options!
39
- validate_json_formatter!
36
+ validate_lograge_formatter!
37
+ validate_logger_formatter!
40
38
 
41
39
  true
42
40
  end
@@ -55,10 +53,21 @@ module LogBench
55
53
  end
56
54
  end
57
55
 
58
- def validate_json_formatter!
56
+ def validate_lograge_formatter!
59
57
  formatter = lograge_config&.formatter
58
+ unless formatter.is_a?(Lograge::Formatters::Json)
59
+ raise ConfigurationError, build_error_message(:lograge_formatter)
60
+ end
61
+ end
62
+
63
+ def validate_logger_formatter!
64
+ # Check Rails.logger directly since that's what we set now
65
+ logger = Rails.logger
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
60
69
  unless formatter.is_a?(LogBench::JsonFormatter)
61
- raise ConfigurationError, build_error_message(:formatter)
70
+ raise ConfigurationError, build_error_message(:logger_formatter)
62
71
  end
63
72
  end
64
73
 
@@ -75,8 +84,7 @@ module LogBench
75
84
 
76
85
  #{config[:description]}
77
86
 
78
- Add this to config/environments/development.rb:
79
- #{config[:fix]}
87
+ This should be automatically configured by LogBench, but something went wrong.
80
88
 
81
89
  For complete setup: https://github.com/silva96/log_bench#configuration
82
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,127 @@ 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|
22
+ # Run AFTER user initializers to pick up their configuration
23
+ initializer "log_bench.configure", after: :load_config_initializers do |app|
19
24
  LogBench.setup do |config|
25
+ # Enable by default in development for backward compatibility
26
+ config.enabled = if app.config.log_bench.enabled.nil?
27
+ Rails.env.development?
28
+ else
29
+ app.config.log_bench.enabled
30
+ end
31
+
20
32
  config.show_init_message = app.config.log_bench.show_init_message
21
33
  config.show_init_message = :full if config.show_init_message.nil?
34
+ config.base_controller_classes = app.config.log_bench.base_controller_classes if app.config.log_bench.base_controller_classes
35
+ config.configure_lograge_automatically = app.config.log_bench.configure_lograge_automatically unless app.config.log_bench.configure_lograge_automatically.nil?
22
36
  end
23
37
  end
24
38
 
25
- # Show installation instructions when Rails starts in development
39
+ # Show success message when Rails starts in development
26
40
  initializer "log_bench.show_instructions", after: :load_config_initializers do
27
- return unless Rails.env.development?
41
+ next unless Rails.env.development? && LogBench.configuration.enabled
28
42
 
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
43
+ print_configured_init_message
44
+ rescue => e
45
+ puts LINE
46
+ puts "⚠️ LogBench setup issue: #{e.message} at: \n#{e.backtrace.join("\n")}"
47
+ puts HELP_INSTRUCTIONS
48
+ puts LINE
49
+ end
50
+
51
+ # Single after_initialize for ALL setup that needs to happen after Rails is ready
52
+ config.after_initialize do |app|
53
+ if LogBench.configuration.enabled
54
+ LogBench::Railtie.setup_lograge(app)
55
+ LogBench::Railtie.setup_current_attributes
56
+ LogBench::Railtie.setup_rails_logger_final
57
+ LogBench::Railtie.validate_configuration!
58
+ end
59
+ end
60
+
61
+ class << self
62
+ def setup_lograge(app)
63
+ return unless LogBench.configuration.configure_lograge_automatically
64
+
65
+ app.config.lograge.enabled = true
66
+ app.config.lograge.formatter = Lograge::Formatters::Json.new
67
+ app.config.lograge.custom_options = lambda do |event|
68
+ params = event.payload[:params]&.except("controller", "action")
69
+ {params: params} if params.present?
70
+ end
71
+ end
72
+
73
+ def setup_current_attributes
74
+ # Inject Current.request_id into base controllers
75
+ LogBench.configuration.base_controller_classes.each do |controller_class_name|
76
+ controller_class = controller_class_name.safe_constantize
77
+ next unless controller_class
78
+
79
+ inject_current_request_id(controller_class)
80
+ end
81
+ end
82
+
83
+ # Setup Rails logger by re-wrapping with TaggedLogging
84
+ def setup_rails_logger_final
85
+ # Get the underlying logger (unwrap TaggedLogging if present)
86
+ base_logger = Rails.logger.respond_to?(:logger) ? Rails.logger.logger : Rails.logger
87
+ base_logger.formatter = LogBench::JsonFormatter.new
88
+ # Re-wrap with TaggedLogging to maintain Rails compatibility
89
+ Rails.logger = ActiveSupport::TaggedLogging.new(base_logger)
90
+ end
91
+
92
+ def inject_current_request_id(controller_class)
93
+ return if controller_class.method_defined?(:set_current_request_id)
94
+
95
+ controller_class.class_eval do
96
+ before_action :set_current_request_id
97
+
98
+ private
99
+
100
+ def set_current_request_id
101
+ LogBench::Current.request_id = request.request_id if defined?(LogBench::Current)
102
+ end
40
103
  end
104
+ end
105
+
106
+ # Validate that LogBench setup worked correctly
107
+ def validate_configuration!
108
+ ConfigurationValidator.validate_rails_config!
41
109
  rescue ConfigurationValidator::ConfigurationError => e
42
- # Lograge needs configuration
43
- print_configuration_instructions
44
- puts "⚠️ Configuration issue: #{e.message}"
45
- print_help_instructions
46
110
  puts LINE
111
+ puts "❌ LogBench Configuration Error:"
112
+ puts e.message
113
+ puts "LogBench will be disabled until this is fixed."
47
114
  puts LINE
48
115
  end
49
116
  end
50
117
 
51
118
  private
52
119
 
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"
120
+ def print_configured_init_message
121
+ case LogBench.configuration.show_init_message
122
+ when :full, nil
123
+ puts LINE, FULL_INIT_MESSAGE, HELP_INSTRUCTIONS, LINE
124
+ when :min
125
+ puts LINE, MIN_INIT_MESSAGE, LINE
126
+ end
79
127
  end
80
128
  end
81
129
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LogBench
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamín Silva
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-06-17 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