Class: BrainzLab::Rails::LogFormatter
- Inherits:
-
Object
- Object
- BrainzLab::Rails::LogFormatter
- Defined in:
- lib/brainzlab/rails/log_formatter.rb
Constant Summary collapse
- ASSET_PATHS =
%w[/assets /packs /vite /images /fonts /stylesheets /javascripts].freeze
- ASSET_EXTENSIONS =
%w[.css .js .map .png .jpg .jpeg .gif .svg .ico .woff .woff2 .ttf .eot].freeze
- SIMPLE_PATHS =
%w[/up /health /healthz /ready /readiness /live /liveness /ping /favicon.ico /apple-touch-icon.png /apple-touch-icon-precomposed.png /robots.txt /sitemap.xml].freeze
- IGNORED_PATHS =
%w[/apple-touch-icon.png /apple-touch-icon-precomposed.png /favicon.ico].freeze
- SLOW_QUERY_MS =
Thresholds for highlighting
5.0- N_PLUS_ONE_THRESHOLD =
3- COLORS =
ANSI color codes
{ reset: "\e[0m", bold: "\e[1m", dim: "\e[2m", red: "\e[31m", green: "\e[32m", yellow: "\e[33m", blue: "\e[34m", magenta: "\e[35m", cyan: "\e[36m", white: "\e[37m", gray: "\e[90m" }.freeze
- BOX =
Box drawing characters
{ top_left: '┌', top_right: '─', bottom_left: '└', bottom_right: '─', vertical: '│', horizontal: '─' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #default_config ⇒ Object
- #detect_terminal_width ⇒ Object
-
#end_request(request_id) ⇒ Object
Called when request ends - returns formatted output.
-
#error(request_id, exception) ⇒ Object
Called when an error occurs.
-
#format_request(data) ⇒ Object
Format a complete request.
-
#initialize(config = {}) ⇒ LogFormatter
constructor
A new instance of LogFormatter.
-
#process_action(request_id, data = {}) ⇒ Object
Called when controller processes action.
-
#render_layout(request_id, layout: nil, duration: 0) ⇒ Object
Called when layout is rendered.
-
#render_partial(request_id, template: nil, duration: 0, count: nil) ⇒ Object
Called when partial is rendered.
-
#render_template(request_id, template: nil, duration: 0, layout: nil) ⇒ Object
Called when template is rendered.
-
#sql_query(request_id, name: nil, duration: 0, sql: nil, sql_pattern: nil, cached: false, source: nil) ⇒ Object
Called when SQL query is executed.
-
#start_request(request_id, data = {}) ⇒ Object
Called when a request starts.
Constructor Details
#initialize(config = {}) ⇒ LogFormatter
Returns a new instance of LogFormatter.
43 44 45 46 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 43 def initialize(config = {}) @config = default_config.merge(config) @request_data = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
41 42 43 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 41 def config @config end |
Instance Method Details
#default_config ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 48 def default_config { enabled: true, colors: $stdout.tty?, hide_assets: false, hide_ignored: true, compact_assets: true, show_params: true, show_sql_count: true, show_sql_details: true, show_sql_queries: true, # Show actual SQL queries show_views: true, slow_query_threshold: SLOW_QUERY_MS, n_plus_one_threshold: N_PLUS_ONE_THRESHOLD, line_width: detect_terminal_width } end |
#detect_terminal_width ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 66 def detect_terminal_width # Try to get terminal width, fallback to 120 width = ENV['COLUMNS']&.to_i return width if width&.positive? if $stdout.tty? && IO.respond_to?(:console) && IO.console _rows, cols = IO.console.winsize return cols if cols.positive? end 120 # Default to 120 for wider output rescue StandardError 120 end |
#end_request(request_id) ⇒ Object
Called when request ends - returns formatted output
173 174 175 176 177 178 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 173 def end_request(request_id) data = @request_data.delete(request_id) return nil unless data format_request(data) end |
#error(request_id, exception) ⇒ Object
Called when an error occurs
165 166 167 168 169 170 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 165 def error(request_id, exception) return unless @request_data[request_id] @request_data[request_id][:error] = exception.class.name @request_data[request_id][:error_message] = exception. end |
#format_request(data) ⇒ Object
Format a complete request
181 182 183 184 185 186 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 181 def format_request(data) return nil if should_ignore?(data) return format_simple(data) if should_be_simple?(data) format_full(data) end |
#process_action(request_id, data = {}) ⇒ Object
Called when controller processes action
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 102 def process_action(request_id, data = {}) return unless @request_data[request_id] @request_data[request_id].merge!( controller: data[:controller], action: data[:action], status: data[:status], duration: data[:duration], view_runtime: data[:view_runtime], db_runtime: data[:db_runtime] ) end |
#render_layout(request_id, layout: nil, duration: 0) ⇒ Object
Called when layout is rendered
154 155 156 157 158 159 160 161 162 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 154 def render_layout(request_id, layout: nil, duration: 0) return unless @request_data[request_id] @request_data[request_id][:views] << { type: :layout, template: layout, duration: duration } end |
#render_partial(request_id, template: nil, duration: 0, count: nil) ⇒ Object
Called when partial is rendered
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 142 def render_partial(request_id, template: nil, duration: 0, count: nil) return unless @request_data[request_id] @request_data[request_id][:views] << { type: :partial, template: template, duration: duration, count: count } end |
#render_template(request_id, template: nil, duration: 0, layout: nil) ⇒ Object
Called when template is rendered
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 130 def render_template(request_id, template: nil, duration: 0, layout: nil) return unless @request_data[request_id] @request_data[request_id][:views] << { type: :template, template: template, duration: duration, layout: layout } end |
#sql_query(request_id, name: nil, duration: 0, sql: nil, sql_pattern: nil, cached: false, source: nil) ⇒ Object
Called when SQL query is executed
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 116 def sql_query(request_id, name: nil, duration: 0, sql: nil, sql_pattern: nil, cached: false, source: nil) return unless @request_data[request_id] @request_data[request_id][:sql_queries] << { name: name, duration: duration, sql: sql, sql_pattern: sql_pattern, cached: cached, source: source } end |
#start_request(request_id, data = {}) ⇒ Object
Called when a request starts
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/brainzlab/rails/log_formatter.rb', line 82 def start_request(request_id, data = {}) @request_data[request_id] = { started_at: Time.current, method: data[:method], path: data[:path], params: data[:params] || {}, controller: nil, action: nil, status: nil, duration: nil, view_runtime: nil, db_runtime: nil, sql_queries: [], views: [], error: nil, error_message: nil } end |