faultline-rails 0.4.0 → 0.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 +4 -4
- data/README.md +14 -0
- data/app/assets/stylesheets/faultline/application.css +0 -41
- data/app/assets/stylesheets/faultline/dashboard.css +961 -0
- data/app/controllers/faultline/logged_exceptions_controller.rb +81 -8
- data/app/helpers/faultline/logged_exceptions_helper.rb +84 -5
- data/app/javascript/controllers/faultline_controller.js +349 -7
- data/app/javascript/faultline/controllers/sidebar_controller.js +21 -0
- data/app/javascript/faultline/controllers/theme_controller.js +48 -0
- data/app/models/faultline/logged_exception.rb +10 -0
- data/app/views/faultline/logged_exceptions/_exceptions.html.erb +132 -84
- data/app/views/faultline/logged_exceptions/_show.html.erb +281 -60
- data/app/views/faultline/logged_exceptions/index.html.erb +192 -123
- data/app/views/faultline/logged_exceptions/show.html.erb +1 -1
- data/app/views/layouts/faultline/application.html.erb +116 -51
- data/faultline-rails.gemspec +1 -0
- data/lib/faultline/cleanup.rb +32 -0
- data/lib/faultline/configuration.rb +172 -4
- data/lib/faultline/engine.rb +22 -0
- data/lib/faultline/logger.rb +188 -0
- data/lib/faultline/notifications.rb +80 -0
- data/lib/faultline/ransack_config.rb +2 -24
- data/lib/faultline/version.rb +1 -1
- data/lib/faultline.rb +10 -0
- data/lib/generators/faultline/customize_generator.rb +117 -0
- data/lib/generators/faultline/install_generator.rb +15 -0
- data/lib/generators/faultline/templates/faultline.rb +70 -37
- data/lib/generators/faultline/templates/javascript/controllers/faultline_controller.js +310 -0
- data/lib/generators/faultline/templates/views/faultline/logged_exceptions/_exceptions.html.erb +124 -0
- data/lib/generators/faultline/templates/views/faultline/logged_exceptions/_show.html.erb +83 -0
- data/lib/generators/faultline/templates/views/faultline/logged_exceptions/index.html.erb +113 -0
- data/lib/generators/faultline/templates/views/layouts/faultline/application.html.erb +133 -0
- data/lib/tasks/faultline.rake +147 -0
- metadata +15 -1
|
@@ -2,32 +2,200 @@
|
|
|
2
2
|
|
|
3
3
|
module Faultline
|
|
4
4
|
class Configuration
|
|
5
|
+
# ─── General ───────────────────────────────────────────────
|
|
6
|
+
|
|
5
7
|
# Dashboard title shown in the header. Set to nil to use "Faultline".
|
|
6
8
|
attr_accessor :application_name
|
|
7
9
|
|
|
10
|
+
# Enable/disable the dashboard entirely (default: true).
|
|
11
|
+
attr_accessor :enabled
|
|
12
|
+
|
|
13
|
+
# Number of exceptions per page (default: 30).
|
|
14
|
+
attr_accessor :per_page
|
|
15
|
+
|
|
16
|
+
# ─── Authentication ────────────────────────────────────────
|
|
17
|
+
|
|
8
18
|
# Proc called with the controller instance before each request.
|
|
9
19
|
# Return false/nil to deny access; return true to allow.
|
|
10
20
|
# Example:
|
|
11
21
|
# config.auth_block = ->(controller) { controller.current_user&.admin? }
|
|
12
22
|
attr_accessor :auth_block
|
|
13
23
|
|
|
24
|
+
# ─── Exception Data ────────────────────────────────────────
|
|
25
|
+
|
|
14
26
|
# Proc called with the controller to attach extra data to each exception record.
|
|
15
27
|
# Example:
|
|
16
28
|
# config.exception_data = ->(controller) { { user_id: controller.current_user&.id } }
|
|
17
29
|
attr_accessor :exception_data
|
|
18
30
|
|
|
19
|
-
#
|
|
20
|
-
attr_accessor :per_page
|
|
31
|
+
# ─── Notifications ─────────────────────────────────────────
|
|
21
32
|
|
|
22
|
-
#
|
|
23
|
-
|
|
33
|
+
# Proc called with the LoggedException record after creation.
|
|
34
|
+
# Use for Telegram, Slack, email, or custom notifications.
|
|
35
|
+
# Example:
|
|
36
|
+
# config.after_create = ->(exception) { MyNotifier.alert(exception) }
|
|
37
|
+
attr_accessor :after_create
|
|
38
|
+
|
|
39
|
+
# Webhook URLs called on each new exception (POST with JSON body).
|
|
40
|
+
# Example:
|
|
41
|
+
# config.webhooks = ["https://hooks.slack.com/services/xxx"]
|
|
42
|
+
attr_accessor :webhooks
|
|
43
|
+
|
|
44
|
+
# Webhook timeout in seconds (default: 5).
|
|
45
|
+
attr_accessor :webhook_timeout
|
|
46
|
+
|
|
47
|
+
# Custom headers sent with webhook requests.
|
|
48
|
+
# Example:
|
|
49
|
+
# config.webhook_headers = { "Authorization" => "Bearer token123" }
|
|
50
|
+
attr_accessor :webhook_headers
|
|
51
|
+
|
|
52
|
+
# ─── Logging ───────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
# Enable structured logging (default: true).
|
|
55
|
+
attr_accessor :logging_enabled
|
|
56
|
+
|
|
57
|
+
# Log level for faultline logger (default: :info).
|
|
58
|
+
# Options: :debug, :info, :warn, :error, :fatal
|
|
59
|
+
attr_accessor :log_level
|
|
60
|
+
|
|
61
|
+
# Write logs to a JSON file (path or nil).
|
|
62
|
+
# Example:
|
|
63
|
+
# config.log_file = "log/faultline.log"
|
|
64
|
+
attr_accessor :log_file
|
|
65
|
+
|
|
66
|
+
# Include backtrace in logs (default: true).
|
|
67
|
+
attr_accessor :log_backtrace
|
|
68
|
+
|
|
69
|
+
# Include request params in logs (default: true).
|
|
70
|
+
attr_accessor :log_params
|
|
71
|
+
|
|
72
|
+
# Include user info in logs (default: true).
|
|
73
|
+
attr_accessor :log_user_info
|
|
74
|
+
|
|
75
|
+
# Maximum backtrace lines to include (default: 20).
|
|
76
|
+
attr_accessor :log_backtrace_limit
|
|
77
|
+
|
|
78
|
+
# Enable exception deduplication (default: false).
|
|
79
|
+
# When enabled, only logs unique exception class + controller combinations
|
|
80
|
+
# within a time window.
|
|
81
|
+
attr_accessor :deduplication_enabled
|
|
82
|
+
|
|
83
|
+
# Deduplication time window in seconds (default: 300 = 5 minutes).
|
|
84
|
+
attr_accessor :deduplication_window
|
|
85
|
+
|
|
86
|
+
# ─── Cleanup / Retention ───────────────────────────────────
|
|
87
|
+
|
|
88
|
+
# Automatically delete exceptions older than N days.
|
|
89
|
+
# Set to nil to disable (default: nil).
|
|
90
|
+
attr_accessor :retention_days
|
|
91
|
+
|
|
92
|
+
# ─── Appearance ────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
# Dashboard theme: "light", "dark", or "auto" (default: "auto").
|
|
95
|
+
attr_accessor :theme
|
|
96
|
+
|
|
97
|
+
# Use the host app's layout instead of faultline's built-in layout.
|
|
98
|
+
# Set to true to render inside the host app's sidebar/nav.
|
|
99
|
+
# Set to false (default) for a standalone dashboard.
|
|
100
|
+
attr_accessor :use_host_layout
|
|
101
|
+
|
|
102
|
+
# CSS class added to the dashboard body element.
|
|
103
|
+
attr_accessor :body_class
|
|
104
|
+
|
|
105
|
+
# ─── Sidebar Links ─────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
# Custom links shown in the dashboard sidebar.
|
|
108
|
+
# Example:
|
|
109
|
+
# config.sidebar_links = [
|
|
110
|
+
# { label: "GitHub", url: "https://github.com/myorg/myapp", icon: "code" },
|
|
111
|
+
# { label: "Docs", url: "/docs", icon: "book" }
|
|
112
|
+
# ]
|
|
113
|
+
attr_accessor :sidebar_links
|
|
114
|
+
|
|
115
|
+
# ─── Advanced UI ───────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
# Show stats overview cards on index page (default: true).
|
|
118
|
+
attr_accessor :show_stats
|
|
119
|
+
|
|
120
|
+
# Enable keyboard shortcuts in the dashboard (default: true).
|
|
121
|
+
attr_accessor :keyboard_shortcuts
|
|
122
|
+
|
|
123
|
+
# Enable exception grouping in the list (default: false).
|
|
124
|
+
# Groups exceptions by class + controller.
|
|
125
|
+
attr_accessor :group_exceptions
|
|
126
|
+
|
|
127
|
+
# Show metadata grid in detail view (default: true).
|
|
128
|
+
attr_accessor :show_metadata
|
|
129
|
+
|
|
130
|
+
# Show environment variables in detail view (default: false).
|
|
131
|
+
attr_accessor :show_environment
|
|
132
|
+
|
|
133
|
+
# Show request params in detail view (default: true).
|
|
134
|
+
attr_accessor :show_request
|
|
135
|
+
|
|
136
|
+
# Maximum number of backtrace frames to show before collapsing (default: 30).
|
|
137
|
+
attr_accessor :backtrace_limit
|
|
138
|
+
|
|
139
|
+
# Enable prev/next navigation in detail view (default: true).
|
|
140
|
+
attr_accessor :enable_navigation
|
|
141
|
+
|
|
142
|
+
# Columns to show in the exception table.
|
|
143
|
+
# Default: [:class, :controller, :action, :message, :time]
|
|
144
|
+
attr_accessor :table_columns
|
|
145
|
+
|
|
146
|
+
# Date format for display (default: nil = auto relative).
|
|
147
|
+
attr_accessor :date_format
|
|
148
|
+
|
|
149
|
+
# Rows per page options for the page size selector (default: [25, 50, 100]).
|
|
150
|
+
attr_accessor :page_size_options
|
|
24
151
|
|
|
25
152
|
def initialize
|
|
26
153
|
@application_name = nil
|
|
27
154
|
@auth_block = nil
|
|
28
155
|
@exception_data = nil
|
|
156
|
+
@after_create = nil
|
|
29
157
|
@per_page = 30
|
|
30
158
|
@enabled = true
|
|
159
|
+
|
|
160
|
+
# Notifications
|
|
161
|
+
@webhooks = []
|
|
162
|
+
@webhook_timeout = 5
|
|
163
|
+
@webhook_headers = {}
|
|
164
|
+
|
|
165
|
+
# Logging
|
|
166
|
+
@logging_enabled = true
|
|
167
|
+
@log_level = :info
|
|
168
|
+
@log_file = nil
|
|
169
|
+
@log_backtrace = true
|
|
170
|
+
@log_params = true
|
|
171
|
+
@log_user_info = true
|
|
172
|
+
@log_backtrace_limit = 20
|
|
173
|
+
@deduplication_enabled = false
|
|
174
|
+
@deduplication_window = 300
|
|
175
|
+
|
|
176
|
+
# Cleanup
|
|
177
|
+
@retention_days = nil
|
|
178
|
+
|
|
179
|
+
# Appearance
|
|
180
|
+
@theme = "auto"
|
|
181
|
+
@use_host_layout = false
|
|
182
|
+
@body_class = ""
|
|
183
|
+
|
|
184
|
+
# Sidebar
|
|
185
|
+
@sidebar_links = []
|
|
186
|
+
|
|
187
|
+
# Advanced UI
|
|
188
|
+
@show_stats = true
|
|
189
|
+
@keyboard_shortcuts = true
|
|
190
|
+
@group_exceptions = false
|
|
191
|
+
@show_metadata = true
|
|
192
|
+
@show_environment = false
|
|
193
|
+
@show_request = true
|
|
194
|
+
@backtrace_limit = 30
|
|
195
|
+
@enable_navigation = true
|
|
196
|
+
@table_columns = %i[class controller action message time].freeze
|
|
197
|
+
@date_format = nil
|
|
198
|
+
@page_size_options = [25, 50, 100].freeze
|
|
31
199
|
end
|
|
32
200
|
end
|
|
33
201
|
end
|
data/lib/faultline/engine.rb
CHANGED
|
@@ -29,5 +29,27 @@ module Faultline
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
|
+
|
|
33
|
+
# Load notifications and cleanup modules
|
|
34
|
+
initializer "faultline.notifications" do
|
|
35
|
+
require "faultline/notifications"
|
|
36
|
+
require "faultline/cleanup"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Register rake tasks
|
|
40
|
+
initializer "faultline.rake_tasks" do
|
|
41
|
+
load "tasks/faultline.rake" if Rake.respond_to?(:application)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Hook into LoggedException after_create for notifications
|
|
45
|
+
initializer "faultline.after_create" do
|
|
46
|
+
ActiveSupport.on_load(:active_record) do
|
|
47
|
+
Faultline::LoggedException.after_create do |exception|
|
|
48
|
+
Faultline::Notifications.notify(exception)
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
Rails.logger.error("[Faultline] Notification error: #{e.message}")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
32
54
|
end
|
|
33
55
|
end
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Faultline
|
|
4
|
+
class Logger
|
|
5
|
+
# Log levels
|
|
6
|
+
LEVELS = %i[debug info warn error fatal].freeze
|
|
7
|
+
|
|
8
|
+
# Default context that's attached to every log entry
|
|
9
|
+
DEFAULT_CONTEXT = {
|
|
10
|
+
gem: "faultline",
|
|
11
|
+
version: Faultline::VERSION
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
# Log an exception with full context
|
|
16
|
+
#
|
|
17
|
+
# Faultline::Logger.error(exception, controller: self, extra: { key: "value" })
|
|
18
|
+
#
|
|
19
|
+
def error(exception, controller: nil, extra: {}, **kwargs)
|
|
20
|
+
log(:error, exception, controller: controller, extra: extra, **kwargs)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def warn(exception, controller: nil, extra: {}, **kwargs)
|
|
24
|
+
log(:warn, exception, controller: controller, extra: extra, **kwargs)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def info(message, controller: nil, extra: {}, **kwargs)
|
|
28
|
+
log(:info, message, controller: controller, extra: extra, **kwargs)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def debug(message, controller: nil, extra: {}, **kwargs)
|
|
32
|
+
log(:debug, message, controller: controller, extra: extra, **kwargs)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Log a custom event (not an exception)
|
|
36
|
+
#
|
|
37
|
+
# Faultline::Logger.event("user.login", user_id: 123, method: "POST")
|
|
38
|
+
#
|
|
39
|
+
def event(name, data = {})
|
|
40
|
+
entry = build_entry(:info, name, nil, extra: data)
|
|
41
|
+
write_to_output(entry)
|
|
42
|
+
write_to_file(entry) if Faultline.configuration.log_file.present?
|
|
43
|
+
entry
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Query logs with filters
|
|
47
|
+
#
|
|
48
|
+
# Faultline::Logger.query(level: :error, since: 1.hour.ago, limit: 50)
|
|
49
|
+
#
|
|
50
|
+
def query(level: nil, since: nil, controller_name: nil, limit: 100)
|
|
51
|
+
scope = Faultline::LoggedException.order(created_at: :desc)
|
|
52
|
+
scope = scope.where("created_at >= ?", since) if since.present?
|
|
53
|
+
scope = scope.where(exception_class: level.to_s.camelize) if level.present?
|
|
54
|
+
scope = scope.where(controller_name: controller_name) if controller_name.present?
|
|
55
|
+
scope.limit(limit)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Get aggregated stats
|
|
59
|
+
#
|
|
60
|
+
# Faultline::Logger.stats(since: 24.hours.ago)
|
|
61
|
+
#
|
|
62
|
+
def stats(since: nil)
|
|
63
|
+
scope = Faultline::LoggedException.all
|
|
64
|
+
scope = scope.where("created_at >= ?", since) if since.present?
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
total: scope.count,
|
|
68
|
+
by_class: scope.group(:exception_class).count,
|
|
69
|
+
by_controller: scope.group(:controller_name).count,
|
|
70
|
+
by_hour: scope.group_by { |e| e.created_at.hour }.transform_values(&:count),
|
|
71
|
+
top_exceptions: scope.group(:exception_class).count.sort_by { |_, v| -v }.first(10)
|
|
72
|
+
}
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def log(level, exception_or_message, controller: nil, extra: {}, **kwargs)
|
|
78
|
+
message = exception_or_message.is_a?(Exception) ? exception_or_message.message : exception_or_message.to_s
|
|
79
|
+
exception_class = exception_or_message.is_a?(Exception) ? exception_or_message.class.name : nil
|
|
80
|
+
|
|
81
|
+
entry = build_entry(level, message, exception_class,
|
|
82
|
+
controller: controller,
|
|
83
|
+
extra: extra.merge(kwargs),
|
|
84
|
+
backtrace: exception_or_message.is_a?(Exception) ? exception_or_message.backtrace&.first(20)&.join("\n") : nil
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
write_to_output(entry)
|
|
88
|
+
write_to_file(entry) if Faultline.configuration.log_file.present?
|
|
89
|
+
entry
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def build_entry(level, message, exception_class, controller: nil, extra: {}, backtrace: nil)
|
|
93
|
+
entry = DEFAULT_CONTEXT.merge(
|
|
94
|
+
level: level,
|
|
95
|
+
message: message,
|
|
96
|
+
timestamp: Time.current.iso8601
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
entry[:exception_class] = exception_class if exception_class.present?
|
|
100
|
+
|
|
101
|
+
# Add controller context
|
|
102
|
+
if controller.present?
|
|
103
|
+
entry[:controller] = {
|
|
104
|
+
class: controller.class.name,
|
|
105
|
+
action: controller.action_name,
|
|
106
|
+
path: controller.request&.path,
|
|
107
|
+
method: controller.request&.request_method,
|
|
108
|
+
remote_ip: controller.request&.remote_ip,
|
|
109
|
+
user_agent: controller.request&.user_agent
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# Add user context
|
|
113
|
+
if controller.respond_to?(:current_user, true) && controller.current_user.present?
|
|
114
|
+
entry[:user] = {
|
|
115
|
+
id: controller.current_user.id,
|
|
116
|
+
email: controller.current_user.email
|
|
117
|
+
}
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Add request params (filtered)
|
|
121
|
+
if controller.request.present?
|
|
122
|
+
entry[:params] = filter_params(controller.request.filtered_parameters)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Add extra metadata
|
|
127
|
+
entry[:extra] = extra if extra.present?
|
|
128
|
+
entry[:backtrace] = backtrace if backtrace.present?
|
|
129
|
+
|
|
130
|
+
entry
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def write_to_output(entry)
|
|
134
|
+
level = entry[:level]
|
|
135
|
+
color = case level
|
|
136
|
+
when :debug then "\e[37m" # white
|
|
137
|
+
when :info then "\e[36m" # cyan
|
|
138
|
+
when :warn then "\e[33m" # yellow
|
|
139
|
+
when :error then "\e[31m" # red
|
|
140
|
+
when :fatal then "\e[35m" # magenta
|
|
141
|
+
else "\e[0m"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
reset = "\e[0m"
|
|
145
|
+
ts = entry[:timestamp].sub(/T/, " ").sub(/\+.*/, "")
|
|
146
|
+
msg = entry[:message]
|
|
147
|
+
cls = entry[:exception_class]
|
|
148
|
+
|
|
149
|
+
line = "#{color}[Faultline]#{reset} #{ts} #{level.to_s.upcase.ljust(5)}"
|
|
150
|
+
line += " #{cls}:" if cls.present?
|
|
151
|
+
line += " #{msg}"
|
|
152
|
+
|
|
153
|
+
if entry[:controller]
|
|
154
|
+
ctrl = entry[:controller]
|
|
155
|
+
line += " [#{ctrl[:class]}##{ctrl[:action]}]"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
Rails.logger.send(level, line)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def write_to_file(entry)
|
|
162
|
+
file = Faultline.configuration.log_file
|
|
163
|
+
return unless file.present?
|
|
164
|
+
|
|
165
|
+
File.open(file, "a") do |f|
|
|
166
|
+
f.puts(entry.to_json)
|
|
167
|
+
end
|
|
168
|
+
rescue StandardError => e
|
|
169
|
+
Rails.logger.error("[Faultline] Failed to write to log file: #{e.message}")
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def filter_params(params)
|
|
173
|
+
return {} unless params.is_a?(Hash)
|
|
174
|
+
|
|
175
|
+
rails_filters = Rails.application.config.filter_parameters
|
|
176
|
+
params.each_with_object({}) do |(key, value), result|
|
|
177
|
+
if rails_filters.any? { |f| key.to_s.include?(f.to_s) }
|
|
178
|
+
result[key] = "[FILTERED]"
|
|
179
|
+
elsif value.is_a?(Hash)
|
|
180
|
+
result[key] = filter_params(value)
|
|
181
|
+
else
|
|
182
|
+
result[key] = value
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module Faultline
|
|
8
|
+
module Notifications
|
|
9
|
+
class << self
|
|
10
|
+
# Called after a LoggedException is created.
|
|
11
|
+
# Triggers webhooks and the after_create callback.
|
|
12
|
+
def notify(exception_record)
|
|
13
|
+
config = Faultline.configuration
|
|
14
|
+
|
|
15
|
+
# Custom after_create callback
|
|
16
|
+
config.after_create&.call(exception_record)
|
|
17
|
+
|
|
18
|
+
# Webhooks
|
|
19
|
+
notify_webhooks(exception_record) if config.webhooks.any?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def notify_webhooks(exception_record)
|
|
25
|
+
config = Faultline.configuration
|
|
26
|
+
payload = build_webhook_payload(exception_record)
|
|
27
|
+
|
|
28
|
+
config.webhooks.each do |url|
|
|
29
|
+
deliver_webhook(url, payload, config)
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
Rails.logger.error("[Faultline] Webhook failed for #{url}: #{e.message}")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def deliver_webhook(url, payload, config)
|
|
36
|
+
uri = URI(url)
|
|
37
|
+
|
|
38
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
39
|
+
http.use_ssl = uri.scheme == "https"
|
|
40
|
+
http.open_timeout = config.webhook_timeout
|
|
41
|
+
http.read_timeout = config.webhook_timeout
|
|
42
|
+
|
|
43
|
+
request = Net::HTTP::Post.new(uri)
|
|
44
|
+
request["Content-Type"] = "application/json"
|
|
45
|
+
config.webhook_headers.each { |k, v| request[k] = v }
|
|
46
|
+
request.body = payload.to_json
|
|
47
|
+
|
|
48
|
+
response = http.request(request)
|
|
49
|
+
|
|
50
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
51
|
+
Rails.logger.error("[Faultline] Webhook #{url} returned #{response.code}")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def build_webhook_payload(exception_record)
|
|
56
|
+
{
|
|
57
|
+
event: "exception.logged",
|
|
58
|
+
timestamp: exception_record.created_at.iso8601,
|
|
59
|
+
exception: {
|
|
60
|
+
id: exception_record.id,
|
|
61
|
+
class: exception_record.exception_class,
|
|
62
|
+
message: exception_record.message&.truncate(500),
|
|
63
|
+
controller: exception_record.controller_name,
|
|
64
|
+
action: exception_record.action_name,
|
|
65
|
+
remote_ip: exception_record.remote_ip,
|
|
66
|
+
user_agent: exception_record.user_agent
|
|
67
|
+
},
|
|
68
|
+
dashboard_url: dashboard_url(exception_record)
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def dashboard_url(exception_record)
|
|
73
|
+
host = Rails.application.routes.default_url_options[:host] ||
|
|
74
|
+
ENV.fetch("APP_HOST", "localhost:3000")
|
|
75
|
+
protocol = Rails.env.production? ? "https" : "http"
|
|
76
|
+
"#{protocol}://#{host}/faultline/logged_exceptions/#{exception_record.id}"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -1,26 +1,4 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
# We just need to declare which attributes are searchable.
|
|
6
|
-
module Faultline
|
|
7
|
-
module RansackConfig
|
|
8
|
-
def self.apply!
|
|
9
|
-
return unless defined?(Ransack)
|
|
10
|
-
|
|
11
|
-
Faultline::LoggedException.class_eval do
|
|
12
|
-
def self.ransackable_attributes(auth_object = nil)
|
|
13
|
-
%w[action_name backtrace controller_name created_at environment
|
|
14
|
-
exception_class id message remote_ip request
|
|
15
|
-
updated_at user_agent user_info]
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def self.ransackable_associations(auth_object = nil)
|
|
19
|
-
[]
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
Faultline::RansackConfig.apply!
|
|
3
|
+
# Ransack configuration is now defined directly on Faultline::LoggedException
|
|
4
|
+
# in app/models/faultline/logged_exception.rb.
|
data/lib/faultline/version.rb
CHANGED
data/lib/faultline.rb
CHANGED
|
@@ -6,6 +6,7 @@ require "ipaddr"
|
|
|
6
6
|
require "socket"
|
|
7
7
|
require "faultline/version"
|
|
8
8
|
require "faultline/configuration"
|
|
9
|
+
require "faultline/logger"
|
|
9
10
|
require "faultline/engine"
|
|
10
11
|
|
|
11
12
|
module Faultline
|
|
@@ -28,6 +29,11 @@ module Faultline
|
|
|
28
29
|
def application_name=(value)
|
|
29
30
|
configuration.application_name = value
|
|
30
31
|
end
|
|
32
|
+
|
|
33
|
+
# Convenience logger accessor
|
|
34
|
+
def logger
|
|
35
|
+
Faultline::Logger
|
|
36
|
+
end
|
|
31
37
|
end
|
|
32
38
|
|
|
33
39
|
# Copyright (c) 2005 Jamis Buck
|
|
@@ -116,6 +122,10 @@ module Faultline
|
|
|
116
122
|
end
|
|
117
123
|
end if rails_filter_parameters.any?
|
|
118
124
|
|
|
125
|
+
# Log with structured logger
|
|
126
|
+
Faultline::Logger.error(exception, controller: self, extra: data)
|
|
127
|
+
|
|
128
|
+
# Create the record
|
|
119
129
|
LoggedException.create_from_exception(self, exception, data)
|
|
120
130
|
end
|
|
121
131
|
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module Faultline
|
|
6
|
+
module Generators
|
|
7
|
+
class CustomizeGenerator < Rails::Generators::Base
|
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
|
9
|
+
|
|
10
|
+
desc <<~DESC
|
|
11
|
+
Generate customizable Faultline dashboard files.
|
|
12
|
+
|
|
13
|
+
Creates modern views, host-app-integrated layout, Stimulus controller,
|
|
14
|
+
and initializer patches for your Rails application.
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
rails generate faultline:customize
|
|
18
|
+
rails generate faultline:customize --layout-only
|
|
19
|
+
rails generate faultline:customize --views-only
|
|
20
|
+
rails generate faultline:customize --stimulus-only
|
|
21
|
+
rails generate faultline:customize --initializer-only
|
|
22
|
+
DESC
|
|
23
|
+
|
|
24
|
+
class_option :layout_only, type: :boolean, default: false, desc: "Only generate the layout"
|
|
25
|
+
class_option :views_only, type: :boolean, default: false, desc: "Only generate the views"
|
|
26
|
+
class_option :stimulus_only, type: :boolean, default: false, desc: "Only generate the Stimulus controller"
|
|
27
|
+
class_option :initializer_only, type: :boolean, default: false, desc: "Only generate the initializer patches"
|
|
28
|
+
class_option :force, type: :boolean, default: false, desc: "Overwrite existing files"
|
|
29
|
+
|
|
30
|
+
def generate_layout
|
|
31
|
+
return unless generate_all? || options[:layout_only]
|
|
32
|
+
|
|
33
|
+
template "views/layouts/faultline/application.html.erb",
|
|
34
|
+
"app/views/layouts/faultline/application.html.erb"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def generate_views
|
|
38
|
+
return unless generate_all? || options[:views_only]
|
|
39
|
+
|
|
40
|
+
template "views/faultline/logged_exceptions/index.html.erb",
|
|
41
|
+
"app/views/faultline/logged_exceptions/index.html.erb"
|
|
42
|
+
|
|
43
|
+
template "views/faultline/logged_exceptions/_exceptions.html.erb",
|
|
44
|
+
"app/views/faultline/logged_exceptions/_exceptions.html.erb"
|
|
45
|
+
|
|
46
|
+
template "views/faultline/logged_exceptions/_show.html.erb",
|
|
47
|
+
"app/views/faultline/logged_exceptions/_show.html.erb"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def generate_stimulus
|
|
51
|
+
return unless generate_all? || options[:stimulus_only]
|
|
52
|
+
|
|
53
|
+
template "javascript/controllers/faultline_controller.js",
|
|
54
|
+
"app/javascript/controllers/faultline_controller.js"
|
|
55
|
+
|
|
56
|
+
run "bin/rails stimulus:manifest:update", capture: true if File.exist?("bin/rails")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def generate_initializer_patches
|
|
60
|
+
return unless generate_all? || options[:initializer_only]
|
|
61
|
+
|
|
62
|
+
append_to_file "config/initializers/faultline.rb" do
|
|
63
|
+
"\n# --- Faultline Customize Generator Patches ---\n" \
|
|
64
|
+
"\n" \
|
|
65
|
+
"# Make host app helpers available in faultline views.\n" \
|
|
66
|
+
"# Uncomment the helpers your app defines:\n" \
|
|
67
|
+
"# Faultline::LoggedExceptionsController.helper HeroiconHelper\n" \
|
|
68
|
+
"# Faultline::LoggedExceptionsController.helper ApplicationHelper\n" \
|
|
69
|
+
"\n" \
|
|
70
|
+
"# Fix for Rails 8.1+: params_filters must return plain hashes.\n" \
|
|
71
|
+
"# This is applied automatically by the gem (v0.5.1+).\n" \
|
|
72
|
+
"# Uncomment below if needed:\n" \
|
|
73
|
+
"# Faultline::LoggedExceptionsController.class_eval do\n" \
|
|
74
|
+
"# private\n" \
|
|
75
|
+
"# def params_filters\n" \
|
|
76
|
+
"# result = {}\n" \
|
|
77
|
+
"# result[:q] = params[:q].to_unsafe_h if params[:q].present?\n" \
|
|
78
|
+
"# result[:query] = params[:query] if params[:query].present?\n" \
|
|
79
|
+
"# result[:date_ranges_filter] = params[:date_ranges_filter] if params[:date_ranges_filter].present?\n" \
|
|
80
|
+
"# result[:exception_names_filter] = params[:exception_names_filter] if params[:exception_names_filter].present?\n" \
|
|
81
|
+
"# result[:controller_actions_filter] = params[:controller_actions_filter] if params[:controller_actions_filter].present?\n" \
|
|
82
|
+
"# result\n" \
|
|
83
|
+
"# end\n" \
|
|
84
|
+
"# end\n"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def display_post_install
|
|
89
|
+
return unless generate_all?
|
|
90
|
+
|
|
91
|
+
say ""
|
|
92
|
+
say "Faultline customization files generated!", :green
|
|
93
|
+
say ""
|
|
94
|
+
say "Generated files:"
|
|
95
|
+
say " - app/views/layouts/faultline/application.html.erb (host-app layout)"
|
|
96
|
+
say " - app/views/faultline/logged_exceptions/index.html.erb"
|
|
97
|
+
say " - app/views/faultline/logged_exceptions/_exceptions.html.erb"
|
|
98
|
+
say " - app/views/faultline/logged_exceptions/_show.html.erb"
|
|
99
|
+
say " - app/javascript/controllers/faultline_controller.js"
|
|
100
|
+
say ""
|
|
101
|
+
say "Next steps:"
|
|
102
|
+
say " 1. Edit the layout to match your host app's navigation"
|
|
103
|
+
say " 2. If using Tailwind, add to your CSS source paths:"
|
|
104
|
+
say ' @source "path/to/faultline/app/views/**/*.erb";'
|
|
105
|
+
say " 3. Run: bin/rails stimulus:manifest:update"
|
|
106
|
+
say ""
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def generate_all?
|
|
112
|
+
!options[:layout_only] && !options[:views_only] &&
|
|
113
|
+
!options[:stimulus_only] && !options[:initializer_only]
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|