@@ -32,176 +32,184 @@ class Engine < ::Rails::Engine
32
32
initializer :initialize_logger , group : :all do
33
33
config = Rails . application . config
34
34
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
37
36
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
41
39
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
64
43
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
68
87
end
69
88
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 }
85
92
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" ] }
90
94
end
91
- ActiveSupport . on_load ( :action_cable ) { self . logger = SemanticLogger [ "ActionCable" ] }
92
95
end
93
96
94
97
# Before any initializers run, but after the gems have been loaded
95
98
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
98
102
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
102
106
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 )
107
111
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 )
110
114
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 )
113
117
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 )
116
120
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
122
126
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
125
130
end
126
131
127
132
# After any initializers run, but after the gems have been loaded
128
133
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
141
178
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
145
181
RailsSemanticLogger . swap_subscriber (
146
- ::ActiveJob :: Logging ::LogSubscriber ,
147
- RailsSemanticLogger ::ActiveJob ::LogSubscriber ,
148
- :active_job
182
+ ::ActionView ::LogSubscriber ,
183
+ RailsSemanticLogger ::ActionView ::LogSubscriber ,
184
+ :action_view
149
185
)
150
- end
151
-
152
- # Active Record
153
- if defined? ( ::ActiveRecord )
154
- require "active_record/log_subscriber"
155
186
187
+ # Action Controller
156
188
RailsSemanticLogger . swap_subscriber (
157
- ::ActiveRecord ::LogSubscriber ,
158
- RailsSemanticLogger ::ActiveRecord ::LogSubscriber ,
159
- :active_record
189
+ ::ActionController ::LogSubscriber ,
190
+ RailsSemanticLogger ::ActionController ::LogSubscriber ,
191
+ :action_controller
160
192
)
161
193
end
162
194
163
- # Rack
164
- RailsSemanticLogger ::Rack ::Logger . started_request_log_level = :info if config . rails_semantic_logger . started
195
+ #
196
+ # Forking Frameworks
197
+ #
165
198
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
170
205
end
171
206
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 )
191
209
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 )
198
212
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 )
205
213
end
206
214
end
207
215
end
0 commit comments