Skip to content

Commit b112ca1

Browse files
committed
Tame excessive logs in dev
1 parent 6af6cae commit b112ca1

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

config/environments/development.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
config.action_mailer.delivery_method = :test
7272
end
7373

74+
# Allow setting log level via environment variable (default to :debug)
75+
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "debug").to_sym
76+
7477
# Print deprecation notices to the Rails logger.
7578
config.active_support.deprecation = :log
7679

@@ -110,12 +113,13 @@
110113
# config.generators.apply_rubocop_autocorrect_after_generate!
111114

112115
config.after_initialize do
113-
Bullet.enable = true
114-
Bullet.alert = false
115-
Bullet.bullet_logger = true
116-
Bullet.console = true
117-
Bullet.rails_logger = true
118-
Bullet.add_footer = true
116+
# Bullet configuration with fine-grained control via environment variables
117+
Bullet.enable = ENV.fetch("BULLET_ENABLED", "true") == "true"
118+
Bullet.alert = ENV.fetch("BULLET_ALERT", "false") == "true"
119+
Bullet.bullet_logger = ENV.fetch("BULLET_LOGGER", "true") == "true"
120+
Bullet.console = ENV.fetch("BULLET_CONSOLE", "true") == "true"
121+
Bullet.rails_logger = ENV.fetch("BULLET_RAILS_LOGGER", "true") == "true"
122+
Bullet.add_footer = ENV.fetch("BULLET_FOOTER", "true") == "true"
119123

120124
# Safelist for known false positives:
121125
# PropPhoto => image_attachment is eagerly loaded for ActiveStorage images,

config/initializers/lograge.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# Lograge configuration for structured logging
44
# Converts Rails logs from verbose multi-line format to single-line JSON
5+
#
6+
# Control via environment variables:
7+
# LOGRAGE_ENABLED=true - Force enable lograge (e.g. in development)
8+
# LOGRAGE_KEEP_ORIGINAL=true - Keep original Rails logs alongside Lograge
59

610
Rails.application.configure do
711
# Enable lograge for all environments except development (optional)
@@ -78,7 +82,7 @@
7882
end
7983

8084
# Keep original Rails logger for other logs
81-
config.lograge.keep_original_rails_log = false
85+
config.lograge.keep_original_rails_log = ENV['LOGRAGE_KEEP_ORIGINAL'] == 'true'
8286

8387
# Ignore certain paths (health checks, assets)
8488
config.lograge.ignore_actions = [

config/initializers/rails_performance.rb

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
#
1212
# Data is stored in Redis and never sent externally.
1313

14-
# Disable the resource monitor logging (CPU/memory/disk every minute)
15-
# The data is still collected for the dashboard, but not logged to stdout
16-
RailsPerformance._resource_monitor_enabled = false unless ENV['RAILS_PERFORMANCE_RESOURCE_MONITOR'] == 'true'
14+
# Disable the resource monitor logging (CPU/memory/disk every minute) by default
15+
# This prevents noisy "Server: ..., Context: rails, Role: web, data: ..." logs
16+
# To enable, set RAILS_PERFORMANCE_RESOURCE_MONITOR=true
17+
RailsPerformance._resource_monitor_enabled = ENV['RAILS_PERFORMANCE_RESOURCE_MONITOR'] == 'true'
18+
19+
# If the engine already started the monitor, stop it
20+
if !RailsPerformance._resource_monitor_enabled && RailsPerformance._resource_monitor
21+
RailsPerformance._resource_monitor.stop_monitoring rescue nil
22+
end
1723

1824
RailsPerformance.setup do |config|
1925
# Redis configuration - uses the same Redis as the rest of the app
@@ -25,8 +31,9 @@
2531
# Default is 4 hours, we keep 7 days for trend analysis
2632
config.duration = 168.hours
2733

28-
# Enable debug mode in development
29-
config.debug = Rails.env.development?
34+
# Debug mode enables verbose logging (e.g. [SAVE] entries for every record)
35+
# Controlled via environment variable for easier toggling without code changes
36+
config.debug = ENV['RAILS_PERFORMANCE_DEBUG'] == 'true'
3037

3138
# Ignore certain paths from tracking
3239
config.ignored_endpoints = [
@@ -81,3 +88,55 @@
8188
# Enable custom events tracking
8289
config.include_custom_events = true
8390
end
91+
92+
# Fine-grained logging control for Rails Performance
93+
# This allows us to suppress noisy [SAVE] messages while still seeing other debug info
94+
module RailsPerformance
95+
class << self
96+
def log(message)
97+
return unless RailsPerformance.debug
98+
99+
# Skip noisy [SAVE] logs unless specifically requested via environment variable
100+
# These logs occur every time a record is saved to Redis
101+
if message.include?("[SAVE]") && ENV['RAILS_PERFORMANCE_VERBOSE'] != 'true'
102+
return
103+
end
104+
105+
# Prefix the log message for easier filtering/identification
106+
formatted_message = "[RailsPerformance] #{message}"
107+
108+
if ::Rails.logger
109+
::Rails.logger.debug(formatted_message)
110+
else
111+
puts(formatted_message)
112+
end
113+
end
114+
end
115+
end
116+
117+
# Monkey-patch ResourceMonitor to use our unified logging
118+
# This ensures "Server: ..., Context: rails..." logs respect the debug setting
119+
if defined?(RailsPerformance::SystemMonitor::ResourcesMonitor)
120+
module RailsPerformance
121+
module SystemMonitor
122+
class ResourcesMonitor
123+
def store_data(data)
124+
# Use our unified log method instead of direct Rails.logger.info
125+
# This allows us to suppress these logs unless RAILS_PERFORMANCE_DEBUG is true
126+
RailsPerformance.log("Server: #{server_id}, Context: #{context}, Role: #{role}, data: #{data}")
127+
128+
now = RailsPerformance::Utils.kind_of_now
129+
now = now.change(sec: 0, usec: 0)
130+
RailsPerformance::Models::ResourceRecord.new(
131+
server: server_id,
132+
context: context,
133+
role: role,
134+
datetime: now.strftime(RailsPerformance::FORMAT),
135+
datetimei: now.to_i,
136+
json: data
137+
).save
138+
end
139+
end
140+
end
141+
end
142+
end

0 commit comments

Comments
 (0)