Skip to content

Commit b786ad5

Browse files
committed
Adding a disabled config that will disable the logger
1 parent ccf33c3 commit b786ad5

File tree

3 files changed

+146
-138
lines changed

3 files changed

+146
-138
lines changed

lib/rails_semantic_logger.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "semantic_logger"
2-
require "rails_semantic_logger/extensions/rails/server" if defined?(Rails::Server)
32
require "rails_semantic_logger/engine"
43

54
module RailsSemanticLogger

lib/rails_semantic_logger/engine.rb

Lines changed: 144 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -27,181 +27,189 @@ class Engine < ::Rails::Engine
2727
# additional features
2828

2929
# Replace Rails logger initializer
30-
Rails::Application::Bootstrap.initializers.delete_if { |i| i.name == :initialize_logger }
30+
#Rails::Application::Bootstrap.initializers.delete_if { |i| i.name == :initialize_logger }
3131

3232
initializer :initialize_logger, group: :all do
3333
config = Rails.application.config
3434

35-
# Set the default log level based on the Rails config
36-
SemanticLogger.default_level = config.log_level
35+
if ! config.rails_semantic_logger.disabled
3736

38-
if defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
39-
config.middleware.swap(Rails::Rack::Logger, RailsSemanticLogger::Rack::Logger, config.log_tags)
40-
end
37+
# Set the default log level based on the Rails config
38+
SemanticLogger.default_level = config.log_level
4139

42-
# Existing loggers are ignored because servers like trinidad supply their
43-
# own file loggers which would result in duplicate logging to the same log file
44-
Rails.logger = config.logger =
45-
begin
46-
if config.rails_semantic_logger.add_file_appender
47-
path = config.paths["log"].first
48-
FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(File.dirname(path))
49-
50-
# Add the log file to the list of appenders
51-
# Use the colorized formatter if Rails colorized logs are enabled
52-
ap_options = config.rails_semantic_logger.ap_options
53-
formatter = config.rails_semantic_logger.format
54-
formatter = {color: {ap: ap_options}} if (formatter == :default) && (config.colorize_logging != false)
55-
56-
# Set internal logger to log to file only, in case another appender experiences errors during writes
57-
appender = SemanticLogger::Appender::File.new(
58-
file_name: path,
59-
level: config.log_level,
60-
formatter: formatter
61-
)
62-
appender.name = "SemanticLogger"
63-
SemanticLogger::Processor.logger = appender
40+
if defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
41+
config.middleware.swap(Rails::Rack::Logger, RailsSemanticLogger::Rack::Logger, config.log_tags)
42+
end
6443

65-
# Check for previous file or stdout loggers
66-
SemanticLogger.appenders.each { |app| app.formatter = formatter if app.is_a?(SemanticLogger::Appender::File) }
67-
SemanticLogger.add_appender(file_name: path, formatter: formatter, filter: config.rails_semantic_logger.filter)
44+
# Existing loggers are ignored because servers like trinidad supply their
45+
# own file loggers which would result in duplicate logging to the same log file
46+
Rails.logger = config.logger =
47+
begin
48+
if config.rails_semantic_logger.add_file_appender
49+
path = config.paths["log"].first
50+
FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(File.dirname(path))
51+
52+
# Add the log file to the list of appenders
53+
# Use the colorized formatter if Rails colorized logs are enabled
54+
ap_options = config.rails_semantic_logger.ap_options
55+
formatter = config.rails_semantic_logger.format
56+
formatter = {color: {ap: ap_options}} if (formatter == :default) && (config.colorize_logging != false)
57+
58+
# Set internal logger to log to file only, in case another appender experiences errors during writes
59+
appender = SemanticLogger::Appender::File.new(
60+
file_name: path,
61+
level: config.log_level,
62+
formatter: formatter
63+
)
64+
appender.name = "SemanticLogger"
65+
SemanticLogger::Processor.logger = appender
66+
67+
# Check for previous file or stdout loggers
68+
SemanticLogger.appenders.each { |app| app.formatter = formatter if app.is_a?(SemanticLogger::Appender::File) }
69+
SemanticLogger.add_appender(file_name: path, formatter: formatter, filter: config.rails_semantic_logger.filter)
70+
end
71+
72+
SemanticLogger[Rails]
73+
rescue StandardError => e
74+
# If not able to log to file, log to standard error with warning level only
75+
SemanticLogger.default_level = :warn
76+
77+
SemanticLogger::Processor.logger = SemanticLogger::Appender::File.new(io: STDERR)
78+
SemanticLogger.add_appender(io: STDERR)
79+
80+
logger = SemanticLogger[Rails]
81+
logger.warn(
82+
"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " \
83+
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.",
84+
e
85+
)
86+
logger
6887
end
6988

70-
SemanticLogger[Rails]
71-
rescue StandardError => e
72-
# If not able to log to file, log to standard error with warning level only
73-
SemanticLogger.default_level = :warn
74-
75-
SemanticLogger::Processor.logger = SemanticLogger::Appender::File.new(io: STDERR)
76-
SemanticLogger.add_appender(io: STDERR)
77-
78-
logger = SemanticLogger[Rails]
79-
logger.warn(
80-
"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " \
81-
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.",
82-
e
83-
)
84-
logger
89+
# Replace Rails loggers
90+
%i[active_record action_controller action_mailer action_view].each do |name|
91+
ActiveSupport.on_load(name) { include SemanticLogger::Loggable }
8592
end
86-
87-
# Replace Rails loggers
88-
%i[active_record action_controller action_mailer action_view].each do |name|
89-
ActiveSupport.on_load(name) { include SemanticLogger::Loggable }
93+
ActiveSupport.on_load(:action_cable) { self.logger = SemanticLogger["ActionCable"] }
9094
end
91-
ActiveSupport.on_load(:action_cable) { self.logger = SemanticLogger["ActionCable"] }
9295
end
9396

9497
# Before any initializers run, but after the gems have been loaded
9598
config.before_initialize do
96-
if config.respond_to?(:assets) && defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
97-
config.rails_semantic_logger.quiet_assets = true if config.assets.quiet
99+
if ! config.rails_semantic_logger.disabled
100+
if config.respond_to?(:assets) && defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
101+
config.rails_semantic_logger.quiet_assets = true if config.assets.quiet
98102

99-
# Otherwise Sprockets can't find the Rails::Rack::Logger middleware
100-
config.assets.quiet = false
101-
end
103+
# Otherwise Sprockets can't find the Rails::Rack::Logger middleware
104+
config.assets.quiet = false
105+
end
102106

103-
# Replace the Mongo Loggers
104-
Mongoid.logger = SemanticLogger[Mongoid] if defined?(Mongoid)
105-
Moped.logger = SemanticLogger[Moped] if defined?(Moped)
106-
Mongo::Logger.logger = SemanticLogger[Mongo] if defined?(Mongo::Logger)
107+
# Replace the Mongo Loggers
108+
Mongoid.logger = SemanticLogger[Mongoid] if defined?(Mongoid)
109+
Moped.logger = SemanticLogger[Moped] if defined?(Moped)
110+
Mongo::Logger.logger = SemanticLogger[Mongo] if defined?(Mongo::Logger)
107111

108-
# Replace the Resque Logger
109-
Resque.logger = SemanticLogger[Resque] if defined?(Resque) && Resque.respond_to?(:logger)
112+
# Replace the Resque Logger
113+
Resque.logger = SemanticLogger[Resque] if defined?(Resque) && Resque.respond_to?(:logger)
110114

111-
# Replace the Sidekiq logger
112-
Sidekiq.logger = SemanticLogger[Sidekiq] if defined?(Sidekiq)
115+
# Replace the Sidekiq logger
116+
Sidekiq.logger = SemanticLogger[Sidekiq] if defined?(Sidekiq)
113117

114-
# Replace the Sidetiq logger
115-
Sidetiq.logger = SemanticLogger[Sidetiq] if defined?(Sidetiq)
118+
# Replace the Sidetiq logger
119+
Sidetiq.logger = SemanticLogger[Sidetiq] if defined?(Sidetiq)
116120

117-
# Replace the DelayedJob logger
118-
if defined?(Delayed::Worker)
119-
Delayed::Worker.logger = SemanticLogger[Delayed::Worker]
120-
Delayed::Worker.plugins << RailsSemanticLogger::DelayedJob::Plugin
121-
end
121+
# Replace the DelayedJob logger
122+
if defined?(Delayed::Worker)
123+
Delayed::Worker.logger = SemanticLogger[Delayed::Worker]
124+
Delayed::Worker.plugins << RailsSemanticLogger::DelayedJob::Plugin
125+
end
122126

123-
# Replace the Bugsnag logger
124-
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
127+
# Replace the Bugsnag logger
128+
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
129+
end
125130
end
126131

127132
# After any initializers run, but after the gems have been loaded
128133
config.after_initialize do
129-
# Replace the Bugsnag logger
130-
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
131-
132-
# Rails Patches
133-
require("rails_semantic_logger/extensions/action_cable/tagged_logger_proxy") if defined?(ActionCable)
134-
require("rails_semantic_logger/extensions/action_controller/live") if defined?(ActionController::Live)
135-
require("rails_semantic_logger/extensions/action_dispatch/debug_exceptions") if defined?(ActionDispatch::DebugExceptions)
136-
if defined?(ActionView::StreamingTemplateRenderer::Body)
137-
require("rails_semantic_logger/extensions/action_view/streaming_template_renderer")
138-
end
139-
require("rails_semantic_logger/extensions/active_job/logging") if defined?(ActiveJob)
140-
require("rails_semantic_logger/extensions/active_model_serializers/logging") if defined?(ActiveModelSerializers)
134+
if ! config.rails_semantic_logger.disabled
135+
# Replace the Bugsnag logger
136+
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
137+
138+
# Rails Patches
139+
require("rails_semantic_logger/extensions/action_cable/tagged_logger_proxy") if defined?(ActionCable)
140+
require("rails_semantic_logger/extensions/action_controller/live") if defined?(ActionController::Live)
141+
require("rails_semantic_logger/extensions/action_dispatch/debug_exceptions") if defined?(ActionDispatch::DebugExceptions)
142+
if defined?(ActionView::StreamingTemplateRenderer::Body)
143+
require("rails_semantic_logger/extensions/action_view/streaming_template_renderer")
144+
end
145+
require("rails_semantic_logger/extensions/active_job/logging") if defined?(ActiveJob)
146+
require("rails_semantic_logger/extensions/active_model_serializers/logging") if defined?(ActiveModelSerializers)
147+
require("rails_semantic_logger/extensions/rails/server") if defined?(Rails::Server)
148+
149+
if config.rails_semantic_logger.semantic
150+
# Active Job
151+
if defined?(::ActiveJob)
152+
RailsSemanticLogger.swap_subscriber(
153+
::ActiveJob::Logging::LogSubscriber,
154+
RailsSemanticLogger::ActiveJob::LogSubscriber,
155+
:active_job
156+
)
157+
end
158+
159+
# Active Record
160+
if defined?(::ActiveRecord)
161+
require "active_record/log_subscriber"
162+
163+
RailsSemanticLogger.swap_subscriber(
164+
::ActiveRecord::LogSubscriber,
165+
RailsSemanticLogger::ActiveRecord::LogSubscriber,
166+
:active_record
167+
)
168+
end
169+
170+
# Rack
171+
RailsSemanticLogger::Rack::Logger.started_request_log_level = :info if config.rails_semantic_logger.started
172+
173+
# Silence asset logging by applying a filter to the Rails logger itself, not any of the appenders.
174+
if config.rails_semantic_logger.quiet_assets && config.assets.prefix
175+
assets_regex = %r(\A/{0,2}#{config.assets.prefix})
176+
RailsSemanticLogger::Rack::Logger.logger.filter = ->(log) { log.payload[:path] !~ assets_regex if log.payload }
177+
end
141178

142-
if config.rails_semantic_logger.semantic
143-
# Active Job
144-
if defined?(::ActiveJob)
179+
# Action View
180+
RailsSemanticLogger::ActionView::LogSubscriber.rendered_log_level = :info if config.rails_semantic_logger.rendered
145181
RailsSemanticLogger.swap_subscriber(
146-
::ActiveJob::Logging::LogSubscriber,
147-
RailsSemanticLogger::ActiveJob::LogSubscriber,
148-
:active_job
182+
::ActionView::LogSubscriber,
183+
RailsSemanticLogger::ActionView::LogSubscriber,
184+
:action_view
149185
)
150-
end
151-
152-
# Active Record
153-
if defined?(::ActiveRecord)
154-
require "active_record/log_subscriber"
155186

187+
# Action Controller
156188
RailsSemanticLogger.swap_subscriber(
157-
::ActiveRecord::LogSubscriber,
158-
RailsSemanticLogger::ActiveRecord::LogSubscriber,
159-
:active_record
189+
::ActionController::LogSubscriber,
190+
RailsSemanticLogger::ActionController::LogSubscriber,
191+
:action_controller
160192
)
161193
end
162194

163-
# Rack
164-
RailsSemanticLogger::Rack::Logger.started_request_log_level = :info if config.rails_semantic_logger.started
195+
#
196+
# Forking Frameworks
197+
#
165198

166-
# Silence asset logging by applying a filter to the Rails logger itself, not any of the appenders.
167-
if config.rails_semantic_logger.quiet_assets && config.assets.prefix
168-
assets_regex = %r(\A/{0,2}#{config.assets.prefix})
169-
RailsSemanticLogger::Rack::Logger.logger.filter = ->(log) { log.payload[:path] !~ assets_regex if log.payload }
199+
# Passenger provides the :starting_worker_process event for executing
200+
# code after it has forked, so we use that and reconnect immediately.
201+
if defined?(PhusionPassenger)
202+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
203+
SemanticLogger.reopen if forked
204+
end
170205
end
171206

172-
# Action View
173-
RailsSemanticLogger::ActionView::LogSubscriber.rendered_log_level = :info if config.rails_semantic_logger.rendered
174-
RailsSemanticLogger.swap_subscriber(
175-
::ActionView::LogSubscriber,
176-
RailsSemanticLogger::ActionView::LogSubscriber,
177-
:action_view
178-
)
179-
180-
# Action Controller
181-
RailsSemanticLogger.swap_subscriber(
182-
::ActionController::LogSubscriber,
183-
RailsSemanticLogger::ActionController::LogSubscriber,
184-
:action_controller
185-
)
186-
end
187-
188-
#
189-
# Forking Frameworks
190-
#
207+
# Re-open appenders after Resque has forked a worker
208+
Resque.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Resque)
191209

192-
# Passenger provides the :starting_worker_process event for executing
193-
# code after it has forked, so we use that and reconnect immediately.
194-
if defined?(PhusionPassenger)
195-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
196-
SemanticLogger.reopen if forked
197-
end
210+
# Re-open appenders after Spring has forked a process
211+
Spring.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Spring.after_fork)
198212
end
199-
200-
# Re-open appenders after Resque has forked a worker
201-
Resque.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Resque)
202-
203-
# Re-open appenders after Spring has forked a process
204-
Spring.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Spring.after_fork)
205213
end
206214
end
207215
end

lib/rails_semantic_logger/options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module RailsSemanticLogger
9898
# config.rails_semantic_logger.named_tags = nil
9999
class Options
100100
attr_accessor :semantic, :started, :processing, :rendered, :ap_options, :add_file_appender,
101-
:quiet_assets, :format, :named_tags, :filter
101+
:quiet_assets, :format, :named_tags, :filter, :disabled
102102

103103
# Setup default values
104104
def initialize
@@ -112,6 +112,7 @@ def initialize
112112
@format = :default
113113
@named_tags = nil
114114
@filter = nil
115+
@disabled = false
115116
end
116117
end
117118
end

0 commit comments

Comments
 (0)