@@ -33,191 +33,184 @@ class Engine < ::Rails::Engine
33
33
initializer :initialize_logger , group : :all do
34
34
config = Rails . application . config
35
35
36
- unless config . rails_semantic_logger . disabled
37
- # Set the default log level based on the Rails config
38
- SemanticLogger . default_level = config . log_level
36
+ # Set the default log level based on the Rails config
37
+ SemanticLogger . default_level = config . log_level
39
38
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
39
+ if defined? ( Rails ::Rack ::Logger ) && config . rails_semantic_logger . semantic
40
+ config . middleware . swap ( Rails ::Rack ::Logger , RailsSemanticLogger ::Rack ::Logger , config . log_tags )
41
+ end
43
42
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
43
+ # Existing loggers are ignored because servers like trinidad supply their
44
+ # own file loggers which would result in duplicate logging to the same log file
45
+ Rails . logger = config . logger =
46
+ begin
47
+ if config . rails_semantic_logger . add_file_appender
48
+ path = config . paths [ "log" ] . first
49
+ FileUtils . mkdir_p ( File . dirname ( path ) ) unless File . exist? ( File . dirname ( path ) )
50
+
51
+ # Add the log file to the list of appenders
52
+ # Use the colorized formatter if Rails colorized logs are enabled
53
+ ap_options = config . rails_semantic_logger . ap_options
54
+ formatter = config . rails_semantic_logger . format
55
+ formatter = { color : { ap : ap_options } } if ( formatter == :default ) && ( config . colorize_logging != false )
56
+
57
+ # Set internal logger to log to file only, in case another appender experiences errors during writes
58
+ appender = SemanticLogger ::Appender ::File . new (
59
+ file_name : path ,
60
+ level : config . log_level ,
61
+ formatter : formatter
85
62
)
86
- logger
63
+ appender . name = "SemanticLogger"
64
+ SemanticLogger ::Processor . logger = appender
65
+
66
+ # Check for previous file or stdout loggers
67
+ SemanticLogger . appenders . each { |app | app . formatter = formatter if app . is_a? ( SemanticLogger ::Appender ::File ) }
68
+ SemanticLogger . add_appender ( file_name : path , formatter : formatter , filter : config . rails_semantic_logger . filter )
87
69
end
88
70
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 }
71
+ SemanticLogger [ Rails ]
72
+ rescue StandardError => e
73
+ # If not able to log to file, log to standard error with warning level only
74
+ SemanticLogger . default_level = :warn
75
+
76
+ SemanticLogger ::Processor . logger = SemanticLogger ::Appender ::File . new ( io : STDERR )
77
+ SemanticLogger . add_appender ( io : STDERR )
78
+
79
+ logger = SemanticLogger [ Rails ]
80
+ logger . warn (
81
+ "Rails Error: Unable to access log file. Please ensure that #{ path } exists and is chmod 0666. " \
82
+ "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." ,
83
+ e
84
+ )
85
+ logger
92
86
end
93
- ActiveSupport . on_load ( :action_cable ) { self . logger = SemanticLogger [ "ActionCable" ] }
87
+
88
+ # Replace Rails loggers
89
+ %i[ active_record action_controller action_mailer action_view ] . each do |name |
90
+ ActiveSupport . on_load ( name ) { include SemanticLogger ::Loggable }
94
91
end
92
+ ActiveSupport . on_load ( :action_cable ) { self . logger = SemanticLogger [ "ActionCable" ] }
95
93
end
96
94
97
95
# Before any initializers run, but after the gems have been loaded
98
96
config . before_initialize do
99
- unless 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
97
+ if config . respond_to? ( :assets ) && defined? ( Rails ::Rack ::Logger ) && config . rails_semantic_logger . semantic
98
+ config . rails_semantic_logger . quiet_assets = true if config . assets . quiet
102
99
103
- # Otherwise Sprockets can't find the Rails::Rack::Logger middleware
104
- config . assets . quiet = false
105
- end
106
-
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 )
100
+ # Otherwise Sprockets can't find the Rails::Rack::Logger middleware
101
+ config . assets . quiet = false
102
+ end
111
103
112
- # Replace the Resque Logger
113
- Resque . logger = SemanticLogger [ Resque ] if defined? ( Resque ) && Resque . respond_to? ( :logger )
104
+ # Replace the Mongo Loggers
105
+ Mongoid . logger = SemanticLogger [ Mongoid ] if defined? ( Mongoid )
106
+ Moped . logger = SemanticLogger [ Moped ] if defined? ( Moped )
107
+ Mongo ::Logger . logger = SemanticLogger [ Mongo ] if defined? ( Mongo ::Logger )
114
108
115
- # Replace the Sidekiq logger
116
- Sidekiq . logger = SemanticLogger [ Sidekiq ] if defined? ( Sidekiq )
109
+ # Replace the Resque Logger
110
+ Resque . logger = SemanticLogger [ Resque ] if defined? ( Resque ) && Resque . respond_to? ( :logger )
117
111
118
- # Replace the Sidetiq logger
119
- Sidetiq . logger = SemanticLogger [ Sidetiq ] if defined? ( Sidetiq )
112
+ # Replace the Sidekiq logger
113
+ Sidekiq . logger = SemanticLogger [ Sidekiq ] if defined? ( Sidekiq )
120
114
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
115
+ # Replace the Sidetiq logger
116
+ Sidetiq . logger = SemanticLogger [ Sidetiq ] if defined? ( Sidetiq )
126
117
127
- # Replace the Bugsnag logger
128
- Bugsnag . configure { |config | config . logger = SemanticLogger [ Bugsnag ] } if defined? ( Bugsnag )
118
+ # Replace the DelayedJob logger
119
+ if defined? ( Delayed ::Worker )
120
+ Delayed ::Worker . logger = SemanticLogger [ Delayed ::Worker ]
121
+ Delayed ::Worker . plugins << RailsSemanticLogger ::DelayedJob ::Plugin
129
122
end
123
+
124
+ # Replace the Bugsnag logger
125
+ Bugsnag . configure { |config | config . logger = SemanticLogger [ Bugsnag ] } if defined? ( Bugsnag )
130
126
end
131
127
132
128
# After any initializers run, but after the gems have been loaded
133
129
config . after_initialize do
134
- unless 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 ) && defined? ( ::ActiveJob ::Logging ::LogSubscriber )
152
- RailsSemanticLogger . swap_subscriber (
153
- ::ActiveJob ::Logging ::LogSubscriber ,
154
- RailsSemanticLogger ::ActiveJob ::LogSubscriber ,
155
- :active_job
156
- )
157
- end
158
-
159
- if defined? ( ::ActiveJob ) && defined? ( ::ActiveJob ::LogSubscriber )
160
- RailsSemanticLogger . swap_subscriber (
161
- ::ActiveJob ::LogSubscriber ,
162
- RailsSemanticLogger ::ActiveJob ::LogSubscriber ,
163
- :active_job
164
- )
165
- end
166
-
167
- # Active Record
168
- if defined? ( ::ActiveRecord )
169
- require "active_record/log_subscriber"
170
-
171
- RailsSemanticLogger . swap_subscriber (
172
- ::ActiveRecord ::LogSubscriber ,
173
- RailsSemanticLogger ::ActiveRecord ::LogSubscriber ,
174
- :active_record
175
- )
176
- end
177
-
178
- # Rack
179
- RailsSemanticLogger ::Rack ::Logger . started_request_log_level = :info if config . rails_semantic_logger . started
130
+ # Replace the Bugsnag logger
131
+ Bugsnag . configure { |config | config . logger = SemanticLogger [ Bugsnag ] } if defined? ( Bugsnag )
132
+
133
+ # Rails Patches
134
+ require ( "rails_semantic_logger/extensions/action_cable/tagged_logger_proxy" ) if defined? ( ActionCable )
135
+ require ( "rails_semantic_logger/extensions/action_controller/live" ) if defined? ( ActionController ::Live )
136
+ require ( "rails_semantic_logger/extensions/action_dispatch/debug_exceptions" ) if defined? ( ActionDispatch ::DebugExceptions )
137
+ if defined? ( ActionView ::StreamingTemplateRenderer ::Body )
138
+ require ( "rails_semantic_logger/extensions/action_view/streaming_template_renderer" )
139
+ end
140
+ require ( "rails_semantic_logger/extensions/active_job/logging" ) if defined? ( ::ActiveJob )
141
+ require ( "rails_semantic_logger/extensions/active_model_serializers/logging" ) if defined? ( ActiveModelSerializers )
180
142
181
- # Silence asset logging by applying a filter to the Rails logger itself, not any of the appenders.
182
- if config . rails_semantic_logger . quiet_assets && config . assets . prefix
183
- assets_regex = %r(\A /{0,2}#{ config . assets . prefix } )
184
- RailsSemanticLogger ::Rack ::Logger . logger . filter = -> ( log ) { log . payload [ :path ] !~ assets_regex if log . payload }
185
- end
143
+ if config . rails_semantic_logger . semantic
144
+ # Active Job
145
+ if defined? ( ::ActiveJob ) && defined? ( ::ActiveJob ::Logging ::LogSubscriber )
146
+ RailsSemanticLogger . swap_subscriber (
147
+ ::ActiveJob ::Logging ::LogSubscriber ,
148
+ RailsSemanticLogger ::ActiveJob ::LogSubscriber ,
149
+ :active_job
150
+ )
151
+ end
186
152
187
- # Action View
188
- RailsSemanticLogger ::ActionView ::LogSubscriber . rendered_log_level = :info if config . rails_semantic_logger . rendered
153
+ if defined? ( ::ActiveJob ) && defined? ( ::ActiveJob ::LogSubscriber )
189
154
RailsSemanticLogger . swap_subscriber (
190
- ::ActionView ::LogSubscriber ,
191
- RailsSemanticLogger ::ActionView ::LogSubscriber ,
192
- :action_view
155
+ ::ActiveJob ::LogSubscriber ,
156
+ RailsSemanticLogger ::ActiveJob ::LogSubscriber ,
157
+ :active_job
193
158
)
159
+ end
160
+
161
+ # Active Record
162
+ if defined? ( ::ActiveRecord )
163
+ require "active_record/log_subscriber"
194
164
195
- # Action Controller
196
165
RailsSemanticLogger . swap_subscriber (
197
- ::ActionController ::LogSubscriber ,
198
- RailsSemanticLogger ::ActionController ::LogSubscriber ,
199
- :action_controller
166
+ ::ActiveRecord ::LogSubscriber ,
167
+ RailsSemanticLogger ::ActiveRecord ::LogSubscriber ,
168
+ :active_record
200
169
)
201
170
end
202
171
203
- #
204
- # Forking Frameworks
205
- #
172
+ # Rack
173
+ RailsSemanticLogger ::Rack ::Logger . started_request_log_level = :info if config . rails_semantic_logger . started
206
174
207
- # Passenger provides the :starting_worker_process event for executing
208
- # code after it has forked, so we use that and reconnect immediately.
209
- if defined? ( PhusionPassenger )
210
- PhusionPassenger . on_event ( :starting_worker_process ) do |forked |
211
- SemanticLogger . reopen if forked
212
- end
175
+ # Silence asset logging by applying a filter to the Rails logger itself, not any of the appenders.
176
+ if config . rails_semantic_logger . quiet_assets && config . assets . prefix
177
+ assets_regex = %r(\A /{0,2}#{ config . assets . prefix } )
178
+ RailsSemanticLogger ::Rack ::Logger . logger . filter = -> ( log ) { log . payload [ :path ] !~ assets_regex if log . payload }
213
179
end
214
180
215
- # Re-open appenders after Resque has forked a worker
216
- Resque . after_fork { |_job | ::SemanticLogger . reopen } if defined? ( Resque )
181
+ # Action View
182
+ RailsSemanticLogger ::ActionView ::LogSubscriber . rendered_log_level = :info if config . rails_semantic_logger . rendered
183
+ RailsSemanticLogger . swap_subscriber (
184
+ ::ActionView ::LogSubscriber ,
185
+ RailsSemanticLogger ::ActionView ::LogSubscriber ,
186
+ :action_view
187
+ )
188
+
189
+ # Action Controller
190
+ RailsSemanticLogger . swap_subscriber (
191
+ ::ActionController ::LogSubscriber ,
192
+ RailsSemanticLogger ::ActionController ::LogSubscriber ,
193
+ :action_controller
194
+ )
195
+ end
217
196
218
- # Re-open appenders after Spring has forked a process
219
- Spring . after_fork { |_job | ::SemanticLogger . reopen } if defined? ( Spring . after_fork )
197
+ #
198
+ # Forking Frameworks
199
+ #
200
+
201
+ # Passenger provides the :starting_worker_process event for executing
202
+ # code after it has forked, so we use that and reconnect immediately.
203
+ if defined? ( PhusionPassenger )
204
+ PhusionPassenger . on_event ( :starting_worker_process ) do |forked |
205
+ SemanticLogger . reopen if forked
206
+ end
220
207
end
208
+
209
+ # Re-open appenders after Resque has forked a worker
210
+ Resque . after_fork { |_job | ::SemanticLogger . reopen } if defined? ( Resque )
211
+
212
+ # Re-open appenders after Spring has forked a process
213
+ Spring . after_fork { |_job | ::SemanticLogger . reopen } if defined? ( Spring . after_fork )
221
214
end
222
215
end
223
216
end
0 commit comments