log_bench 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e097c5b1822d427d807fe466addf85d9036141cad8922b37c0db0ae79dee994d
4
+ data.tar.gz: 4f207f350da8fc94ae2fcbfff2a0b4bfd8021bc477e9093e87c4bb2fb0371c45
5
+ SHA512:
6
+ metadata.gz: c732968d82ef380e7cff012fef5d71ad0996a3b075143226a949881a26e918f72da5776d1fb5b34cf23c346afc3bd4a84dfbc780c93abcb2a2da745801e82d00
7
+ data.tar.gz: c543a20c55ed6270612d9ad369c4fe527b94520d0c9d5d7feb9df3c811416476ea9d6b9d08aa4519adc39a4886cb4eb59738aa02e389abdcb4786ca44ba54e21
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-05
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Benjamín Silva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,230 @@
1
+ # LogBench
2
+
3
+ A powerful TUI (Terminal User Interface) for analyzing Rails application logs in real-time. LogBench provides an intuitive interface to view HTTP requests, SQL queries, and performance metrics from your Rails logs.
4
+
5
+ ![LogBench Preview](logbench-preview.png)
6
+
7
+ ## Features
8
+
9
+ - 🚀 **Real-time log analysis** with auto-scroll
10
+ - 📊 **Request correlation** - groups SQL queries with HTTP requests
11
+ - 🔍 **Advanced filtering** by method, path, status, controller, and more
12
+ - 📈 **Performance insights** - duration, allocations, query analysis
13
+ - 🎨 **Beautiful TUI** with syntax highlighting and ANSI color support
14
+ - ⚡ **Fast parsing** of JSON-formatted logs
15
+ - 🔧 **Custom logging** support for background jobs and events
16
+
17
+ ## Installation
18
+
19
+ Add LogBench to your Rails application's Gemfile:
20
+
21
+ ```ruby
22
+ # Gemfile
23
+ group :development do
24
+ gem 'log_bench'
25
+ end
26
+ ```
27
+
28
+ Then run:
29
+
30
+ ```bash
31
+ bundle install
32
+ ```
33
+
34
+ ## Configuration
35
+
36
+ ### 1. Configure Rails Logging
37
+
38
+ Add this configuration to your `config/environments/development.rb`:
39
+
40
+ ```ruby
41
+ # config/environments/development.rb
42
+ require "lograge"
43
+
44
+ Rails.application.configure do
45
+ # ... other configuration ...
46
+
47
+ # LogBench: Configure structured logging
48
+ config.lograge.enabled = true
49
+ config.lograge.formatter = Lograge::Formatters::Json.new
50
+ config.logger ||= ActiveSupport::Logger.new(config.default_log_file)
51
+ config.logger.formatter = LogBench::JsonFormatter.new
52
+ end
53
+ ```
54
+
55
+ ### 2. Set Up Request ID Tracking
56
+
57
+ Create or update your `Current` model to track request IDs:
58
+
59
+ ```ruby
60
+ # app/models/current.rb
61
+ # frozen_string_literal: true
62
+
63
+ class Current < ActiveSupport::CurrentAttributes
64
+ attribute :request_id
65
+ end
66
+ ```
67
+
68
+ Add request ID tracking to your ApplicationController:
69
+
70
+ ```ruby
71
+ # app/controllers/application_controller.rb
72
+ class ApplicationController < ActionController::Base
73
+ before_action :set_current_request_identifier
74
+
75
+ protected
76
+
77
+ def set_current_request_identifier
78
+ Current.request_id = request.request_id
79
+ end
80
+ end
81
+ ```
82
+
83
+ ### 3. Restart Your Rails Server
84
+
85
+ After configuration, restart your Rails development server:
86
+
87
+ ## Usage
88
+
89
+ ### Basic Usage
90
+
91
+ View your development logs:
92
+
93
+ ```bash
94
+ log_bench
95
+ # or explicitly for a specific log file
96
+ log_bench log/development.log
97
+ ```
98
+
99
+ ### TUI Controls
100
+
101
+ - **Navigation**: `↑↓` or `jk` to navigate requests
102
+ - **Pane switching**: `←→` or `hl` to switch between request list and details
103
+ - **Filtering**: `f` to open filter dialog
104
+ - **Sorting**: `s` to cycle through sort options (timestamp, duration, status)
105
+ - **Auto-scroll**: `a` to toggle auto-scroll mode
106
+ - **Clear**: `c` to clear current logs
107
+ - **Quit**: `q` to exit
108
+
109
+ ### Filtering
110
+
111
+ Press `f` to open the filter dialog. You can filter by:
112
+
113
+ - **Method**: GET, POST, PUT, DELETE, etc.
114
+ - **Path**: URL path patterns
115
+ - **Status**: HTTP status codes (200, 404, 500, etc.)
116
+ - **Controller**: Controller name
117
+ - **Action**: Action name
118
+ - **Duration**: Minimum request duration in ms
119
+
120
+ Examples:
121
+ - Filter by method: `GET`
122
+ - Filter by path: `/api/users`
123
+ - Filter by status: `500`
124
+ - Filter by slow requests: duration `>100`
125
+
126
+ ## Log Format
127
+
128
+ LogBench works with JSON-formatted logs. Each log entry should include:
129
+
130
+ **Required fields for HTTP requests:**
131
+ - `method`: HTTP method (GET, POST, etc.)
132
+ - `path`: Request path
133
+ - `status`: HTTP status code
134
+ - `request_id`: Unique request identifier
135
+ - `duration`: Request duration in milliseconds
136
+
137
+ **Optional fields:**
138
+ - `controller`: Controller name
139
+ - `action`: Action name
140
+ - `allocations`: Memory allocations
141
+ - `view`: View rendering time
142
+ - `db`: Database query time
143
+
144
+ **Other query logs:**
145
+ - `message`: SQL query with timing information
146
+ - `request_id`: Links query to HTTP request
147
+
148
+ ## Testing
149
+
150
+ LogBench includes a comprehensive test suite to ensure reliability and correctness.
151
+
152
+ ### Running Tests
153
+
154
+ ```bash
155
+ # Run all tests
156
+ bundle exec rake test
157
+
158
+ # Run specific test files
159
+ bundle exec ruby test/test_log_entry.rb
160
+ bundle exec ruby test/test_request.rb
161
+ bundle exec ruby test/test_json_formatter.rb
162
+
163
+ # Run tests with verbose output
164
+ bundle exec rake test TESTOPTS="-v"
165
+ ```
166
+
167
+ ### Test Coverage
168
+
169
+ The test suite covers:
170
+
171
+ - **Log parsing**: JSON format detection and parsing
172
+ - **Request correlation**: Grouping SQL queries with HTTP requests
173
+ - **Filtering**: Method, path, status, and duration filters
174
+ - **JsonFormatter**: Custom logging format handling
175
+ - **TUI components**: Screen rendering and user interactions
176
+ - **Edge cases**: Malformed logs, missing fields, performance scenarios
177
+
178
+ ### Writing Tests
179
+
180
+ When contributing, please include tests for new features:
181
+
182
+ ```ruby
183
+ # test/test_new_feature.rb
184
+ require "test_helper"
185
+
186
+ class TestNewFeature < Minitest::Test
187
+ def test_feature_works
188
+ # Your test code here
189
+ assert_equal expected, actual
190
+ end
191
+ end
192
+ ```
193
+
194
+ ## Troubleshooting
195
+
196
+ ### No requests found
197
+
198
+ 1. **Check log file exists**: Ensure the log file path is correct
199
+ 2. **Verify lograge configuration**: Make sure lograge is enabled and configured
200
+ 3. **Check log format**: LogBench requires JSON-formatted logs
201
+ 4. **Generate some requests**: Make HTTP requests to your Rails app to generate logs
202
+
203
+ ### SQL queries not showing
204
+
205
+ 1. **Check request_id correlation**: Ensure SQL queries and HTTP requests share the same `request_id`
206
+ 2. **Verify Current model**: Make sure `Current.request_id` is being set properly
207
+ 3. **Check JsonFormatter**: Ensure the JsonFormatter is configured for your Rails logger
208
+
209
+ ### Performance issues
210
+
211
+ 1. **Large log files**: LogBench loads the entire log file into memory. For very large files, consider rotating logs more frequently
212
+ 2. **Real-time parsing**: Use auto-scroll mode (`a`) for better performance with actively growing log files
213
+
214
+ ## Contributing
215
+
216
+ 1. Fork the repository
217
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
218
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
219
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
220
+ 5. Open a Pull Request
221
+
222
+ ## License
223
+
224
+ This gem is available as open source under the terms of the [MIT License](LICENSE).
225
+
226
+ ## Support
227
+
228
+ - 🐛 **Bug reports**: [GitHub Issues](https://github.com/silva96/log_bench/issues)
229
+ - 💡 **Feature requests**: [GitHub Discussions](https://github.com/silva96/log_bench/discussions)
230
+ - 📖 **Documentation**: [GitHub Wiki](https://github.com/silva96/log_bench/wiki)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
data/exe/log_bench ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/log_bench"
5
+
6
+ def show_help
7
+ puts <<~HELP
8
+ LogBench - Rails Log Viewer
9
+
10
+ Usage:
11
+ bundle exec log_bench [log_file]
12
+ bundle exec log_bench --help
13
+ bundle exec log_bench --version
14
+
15
+ Examples:
16
+ bundle exec log_bench # View log/development.log
17
+ bundle exec log_bench log/production.log # View specific log file
18
+ bundle exec log_bench /path/to/app/log/test.log # View log from another app
19
+
20
+ Setup (for Rails apps):
21
+ See README.md for complete configuration instructions:
22
+ https://github.com/silva96/log_bench#installation
23
+
24
+ Custom Logging:
25
+ # Create JSON loggers for background jobs, events, etc.
26
+ logger = Logger.new('log/jobs.log')
27
+ logger.formatter = LogBench::JsonFormatter.new
28
+ logger.info({ message: "Job completed", job_id: 123 })
29
+
30
+ # Then view with LogBench
31
+ bundle exec log_bench log/jobs.log
32
+
33
+ Requirements:
34
+ - Lograge gem with JSON formatter
35
+ - Rails application logs in lograge format
36
+
37
+ For more info: https://github.com/silva96/log_bench
38
+ HELP
39
+ end
40
+
41
+ def show_version
42
+ puts "LogBench #{LogBench::VERSION}"
43
+ end
44
+
45
+ # Handle command line arguments
46
+ case ARGV[0]
47
+ when "--help", "-h"
48
+ show_help
49
+ exit 0
50
+ when "--version", "-v"
51
+ show_version
52
+ exit 0
53
+ end
54
+
55
+ begin
56
+ log_file = ARGV[0] || "log/development.log"
57
+
58
+ # Check if log file exists
59
+ unless File.exist?(log_file)
60
+ puts "❌ Log file not found: #{log_file}"
61
+ puts
62
+ puts "Make sure:"
63
+ puts " 1. You're in a Rails application directory"
64
+ puts " 2. The log file exists and has content"
65
+ puts " 3. Lograge is configured (see README.md for setup)"
66
+ puts
67
+ puts "For help: bundle exec log_bench --help"
68
+ exit 1
69
+ end
70
+
71
+ LogBench::App::Main.new(log_file).run
72
+ rescue Interrupt
73
+ Curses.close_screen if defined?(Curses)
74
+ puts "\nGoodbye! 👋"
75
+ exit 0
76
+ rescue => e
77
+ Curses.close_screen if defined?(Curses)
78
+ puts "❌ Error: #{e.message}"
79
+ puts
80
+ puts "Common issues:"
81
+ puts " - Log file doesn't exist or is empty"
82
+ puts " - Lograge not configured (see README.md for setup)"
83
+ puts " - Log format not supported (LogBench requires lograge JSON format)"
84
+ puts
85
+ puts "For help: bundle exec log_bench --help"
86
+ exit 1
87
+ end
@@ -0,0 +1,60 @@
1
+ module LogBench
2
+ module App
3
+ class Filter
4
+ def initialize
5
+ self.text = ""
6
+ self.mode = false
7
+ end
8
+
9
+ def enter_mode
10
+ self.mode = true
11
+ end
12
+
13
+ def exit_mode
14
+ self.mode = false
15
+ end
16
+
17
+ def active?
18
+ mode
19
+ end
20
+
21
+ def present?
22
+ !text.empty?
23
+ end
24
+
25
+ def add_character(char)
26
+ self.text += char
27
+ end
28
+
29
+ def remove_character
30
+ self.text = text[0...-1] if text.length > 0
31
+ end
32
+
33
+ def clear
34
+ self.text = ""
35
+ end
36
+
37
+ def clear_and_exit
38
+ clear
39
+ exit_mode
40
+ end
41
+
42
+ def matches?(content)
43
+ return true if text.empty?
44
+ content.to_s.downcase.include?(text.downcase)
45
+ end
46
+
47
+ def display_text
48
+ text
49
+ end
50
+
51
+ def cursor_display
52
+ active? ? "#{text}█" : text
53
+ end
54
+
55
+ private
56
+
57
+ attr_accessor :text, :mode
58
+ end
59
+ end
60
+ end