rails_semantic_logger 4.1.3 → 4.4.1
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 +5 -5
- data/README.md +13 -6
- data/Rakefile +2 -2
- data/lib/rails_semantic_logger/action_controller/log_subscriber.rb +114 -0
- data/lib/rails_semantic_logger/action_view/log_subscriber.rb +108 -0
- data/lib/rails_semantic_logger/active_record/log_subscriber.rb +186 -0
- data/lib/rails_semantic_logger/delayed_job/plugin.rb +11 -0
- data/lib/rails_semantic_logger/engine.rb +82 -151
- data/lib/rails_semantic_logger/extensions/action_controller/live.rb +5 -3
- data/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb +8 -6
- data/lib/rails_semantic_logger/extensions/action_view/streaming_template_renderer.rb +8 -5
- data/lib/rails_semantic_logger/extensions/active_job/logging.rb +129 -5
- data/lib/rails_semantic_logger/extensions/active_model_serializers/logging.rb +11 -8
- data/lib/rails_semantic_logger/extensions/rails/server.rb +2 -2
- data/lib/rails_semantic_logger/options.rb +117 -0
- data/lib/rails_semantic_logger/rack/logger.rb +103 -0
- data/lib/rails_semantic_logger/version.rb +2 -2
- data/lib/rails_semantic_logger.rb +37 -0
- metadata +16 -17
- data/lib/rails_semantic_logger/extensions/action_controller/log_subscriber.rb +0 -107
- data/lib/rails_semantic_logger/extensions/action_controller/log_subscriber_processing.rb +0 -28
- data/lib/rails_semantic_logger/extensions/action_view/log_subscriber.rb +0 -12
- data/lib/rails_semantic_logger/extensions/active_record/log_subscriber.rb +0 -44
- data/lib/rails_semantic_logger/extensions/rails/rack/logger.rb +0 -63
- data/lib/rails_semantic_logger/extensions/rails/rack/logger_info_as_debug.rb +0 -30
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
require 'action_controller/log_subscriber'
|
|
2
|
+
require 'rails_semantic_logger/options'
|
|
3
|
+
|
|
1
4
|
module RailsSemanticLogger
|
|
2
5
|
class Engine < ::Rails::Engine
|
|
3
6
|
# Make the SemanticLogger config available in the Rails application config
|
|
@@ -16,92 +19,7 @@ module RailsSemanticLogger
|
|
|
16
19
|
# end
|
|
17
20
|
config.semantic_logger = ::SemanticLogger
|
|
18
21
|
|
|
19
|
-
config.rails_semantic_logger
|
|
20
|
-
|
|
21
|
-
# Convert Action Controller and Active Record text messages to semantic data
|
|
22
|
-
# Rails -- Started -- { :ip => "127.0.0.1", :method => "GET", :path => "/dashboards/inquiry_recent_activity" }
|
|
23
|
-
# UserController -- Completed #index -- { :action => "index", :db_runtime => 54.64, :format => "HTML", :method => "GET", :mongo_runtime => 0.0, :path => "/users", :status => 200, :status_message => "OK", :view_runtime => 709.88 }
|
|
24
|
-
config.rails_semantic_logger.semantic = true
|
|
25
|
-
|
|
26
|
-
# Change Rack started message to debug so that it does not appear in production
|
|
27
|
-
config.rails_semantic_logger.started = false
|
|
28
|
-
|
|
29
|
-
# Change Processing message to debug so that it does not appear in production
|
|
30
|
-
config.rails_semantic_logger.processing = false
|
|
31
|
-
|
|
32
|
-
# Change Action View render log messages to debug so that they do not appear in production
|
|
33
|
-
# ActionView::Base -- Rendered data/search/_user.html.haml (46.7ms)
|
|
34
|
-
config.rails_semantic_logger.rendered = false
|
|
35
|
-
|
|
36
|
-
# Override the Awesome Print options for logging Hash data as text:
|
|
37
|
-
#
|
|
38
|
-
# Any valid AwesomePrint option for rendering data.
|
|
39
|
-
# The defaults can changed be creating a `~/.aprc` file.
|
|
40
|
-
# See: https://github.com/michaeldv/awesome_print
|
|
41
|
-
#
|
|
42
|
-
# Note: The option :multiline is set to false if not supplied.
|
|
43
|
-
# Note: Has no effect if Awesome Print is not installed.
|
|
44
|
-
config.rails_semantic_logger.ap_options = {multiline: false}
|
|
45
|
-
|
|
46
|
-
# Whether to automatically add an environment specific log file appender.
|
|
47
|
-
# For Example: 'log/development.log'
|
|
48
|
-
#
|
|
49
|
-
# Note:
|
|
50
|
-
# When Semantic Logger fails to log to an appender it logs the error to an
|
|
51
|
-
# internal logger, which by default writes to STDERR.
|
|
52
|
-
# Example, change the default internal logger to log to stdout:
|
|
53
|
-
# SemanticLogger::Processor.logger = SemanticLogger::Appender::File.new(io: STDOUT, level: :warn)
|
|
54
|
-
config.rails_semantic_logger.add_file_appender = true
|
|
55
|
-
|
|
56
|
-
# Silence asset logging
|
|
57
|
-
config.rails_semantic_logger.quiet_assets = false
|
|
58
|
-
|
|
59
|
-
# Override the output format for the primary Rails log file.
|
|
60
|
-
#
|
|
61
|
-
# Valid options:
|
|
62
|
-
# * :default
|
|
63
|
-
# Plain text output with no color.
|
|
64
|
-
# * :color
|
|
65
|
-
# Plain text output with color.
|
|
66
|
-
# * :json
|
|
67
|
-
# JSON output format.
|
|
68
|
-
# * class
|
|
69
|
-
#
|
|
70
|
-
# * Proc
|
|
71
|
-
# A block that will be called to format the output.
|
|
72
|
-
# It is supplied with the `log` entry and should return the formatted data.
|
|
73
|
-
#
|
|
74
|
-
# Note:
|
|
75
|
-
# * `:default` is automatically changed to `:color` if `config.colorize_logging` is `true`.
|
|
76
|
-
#
|
|
77
|
-
# JSON Example, in `application.rb`:
|
|
78
|
-
#
|
|
79
|
-
# config.rails_semantic_logger.format = :json
|
|
80
|
-
#
|
|
81
|
-
# Custom Example, create `app/lib/my_formatter.rb`:
|
|
82
|
-
#
|
|
83
|
-
# # My Custom colorized formatter
|
|
84
|
-
# class MyFormatter < SemanticLogger::Formatters::Color
|
|
85
|
-
# # Return the complete log level name in uppercase
|
|
86
|
-
# def level
|
|
87
|
-
# "#{color}log.level.upcase#{color_map.clear}"
|
|
88
|
-
# end
|
|
89
|
-
# end
|
|
90
|
-
#
|
|
91
|
-
# # In application.rb:
|
|
92
|
-
# config.rails_semantic_logger.format = MyFormatter.new
|
|
93
|
-
config.rails_semantic_logger.format = :default
|
|
94
|
-
|
|
95
|
-
# DEPRECATED
|
|
96
|
-
# Instead, supply a Hash to config.log_tags
|
|
97
|
-
config.rails_semantic_logger.named_tags = nil
|
|
98
|
-
|
|
99
|
-
# Add a filter to the file logger [Regexp|Proc]
|
|
100
|
-
# RegExp: Only include log messages where the class name matches the supplied
|
|
101
|
-
# regular expression. All other messages will be ignored.
|
|
102
|
-
# Proc: Only include log messages where the supplied Proc returns true.
|
|
103
|
-
# The Proc must return true or false.
|
|
104
|
-
config.rails_semantic_logger.filter = nil
|
|
22
|
+
config.rails_semantic_logger = RailsSemanticLogger::Options.new
|
|
105
23
|
|
|
106
24
|
# Initialize SemanticLogger. In a Rails environment it will automatically
|
|
107
25
|
# insert itself above the configured rails logger to add support for its
|
|
@@ -111,14 +29,18 @@ module RailsSemanticLogger
|
|
|
111
29
|
Rails::Application::Bootstrap.initializers.delete_if { |i| i.name == :initialize_logger }
|
|
112
30
|
|
|
113
31
|
initializer :initialize_logger, group: :all do
|
|
114
|
-
config
|
|
32
|
+
config = Rails.application.config
|
|
115
33
|
|
|
116
34
|
# Set the default log level based on the Rails config
|
|
117
35
|
SemanticLogger.default_level = config.log_level
|
|
118
36
|
|
|
37
|
+
if defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
|
|
38
|
+
config.middleware.swap(Rails::Rack::Logger, RailsSemanticLogger::Rack::Logger, config.log_tags)
|
|
39
|
+
end
|
|
40
|
+
|
|
119
41
|
# Existing loggers are ignored because servers like trinidad supply their
|
|
120
42
|
# own file loggers which would result in duplicate logging to the same log file
|
|
121
|
-
Rails.logger
|
|
43
|
+
Rails.logger = config.logger = begin
|
|
122
44
|
if config.rails_semantic_logger.add_file_appender
|
|
123
45
|
path = config.paths['log'].first
|
|
124
46
|
FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(File.dirname(path))
|
|
@@ -130,16 +52,16 @@ module RailsSemanticLogger
|
|
|
130
52
|
formatter = {color: {ap: ap_options}} if (formatter == :default) && (config.colorize_logging != false)
|
|
131
53
|
|
|
132
54
|
# Set internal logger to log to file only, in case another appender experiences errors during writes
|
|
133
|
-
appender
|
|
55
|
+
appender = SemanticLogger::Appender::File.new(
|
|
56
|
+
file_name: path,
|
|
57
|
+
level: config.log_level,
|
|
58
|
+
formatter: formatter
|
|
59
|
+
)
|
|
134
60
|
appender.name = 'SemanticLogger'
|
|
135
61
|
SemanticLogger::Processor.logger = appender
|
|
136
62
|
|
|
137
63
|
# Check for previous file or stdout loggers
|
|
138
|
-
if SemanticLogger::
|
|
139
|
-
SemanticLogger.appenders.each { |appender| appender.formatter = formatter if appender.is_a?(SemanticLogger::Appender::File) }
|
|
140
|
-
elsif config.colorize_logging == false
|
|
141
|
-
SemanticLogger.appenders.each { |appender| appender.formatter = SemanticLogger::Formatters::Default.new if appender.is_a?(SemanticLogger::Appender::File) }
|
|
142
|
-
end
|
|
64
|
+
SemanticLogger.appenders.each { |app| app.formatter = formatter if app.is_a?(SemanticLogger::Appender::File) }
|
|
143
65
|
SemanticLogger.add_appender(file_name: path, formatter: formatter, filter: config.rails_semantic_logger.filter)
|
|
144
66
|
end
|
|
145
67
|
|
|
@@ -153,54 +75,29 @@ module RailsSemanticLogger
|
|
|
153
75
|
|
|
154
76
|
logger = SemanticLogger[Rails]
|
|
155
77
|
logger.warn(
|
|
156
|
-
"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. "
|
|
157
|
-
|
|
78
|
+
"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " \
|
|
79
|
+
'The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.',
|
|
158
80
|
exc
|
|
159
81
|
)
|
|
160
82
|
logger
|
|
161
83
|
end
|
|
162
84
|
|
|
163
85
|
# Replace Rails loggers
|
|
164
|
-
[
|
|
86
|
+
%i[active_record action_controller action_mailer action_view].each do |name|
|
|
165
87
|
ActiveSupport.on_load(name) { include SemanticLogger::Loggable }
|
|
166
88
|
end
|
|
167
89
|
ActiveSupport.on_load(:action_cable) { self.logger = SemanticLogger['ActionCable'] }
|
|
168
90
|
end
|
|
169
91
|
|
|
170
|
-
#
|
|
171
|
-
config.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
assets_regex = %r(\A/{0,2}#{config.assets.prefix})
|
|
175
|
-
if Rails.version.to_i >= 5
|
|
176
|
-
Rails::Rack::Logger.logger.filter = -> log { log.payload[:path] !~ assets_regex if log.payload }
|
|
177
|
-
else
|
|
178
|
-
# Also strips the empty log lines
|
|
179
|
-
Rails::Rack::Logger.logger.filter = -> log { log.payload.nil? ? (log.message != '') : (log.payload[:path] !~ assets_regex) }
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
# Passenger provides the :starting_worker_process event for executing
|
|
184
|
-
# code after it has forked, so we use that and reconnect immediately.
|
|
185
|
-
if defined?(PhusionPassenger)
|
|
186
|
-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
|
187
|
-
::SemanticLogger.reopen if forked
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
# Re-open appenders after Resque has forked a worker
|
|
192
|
-
if defined?(Resque)
|
|
193
|
-
Resque.after_fork { |job| ::SemanticLogger.reopen }
|
|
194
|
-
end
|
|
92
|
+
# Before any initializers run, but after the gems have been loaded
|
|
93
|
+
config.before_initialize do
|
|
94
|
+
if config.respond_to?(:assets) && defined?(Rails::Rack::Logger) && config.rails_semantic_logger.semantic
|
|
95
|
+
config.rails_semantic_logger.quiet_assets = true if config.assets.quiet
|
|
195
96
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
Spring.after_fork { |job| ::SemanticLogger.reopen }
|
|
97
|
+
# Otherwise Sprockets can't find the Rails::Rack::Logger middleware
|
|
98
|
+
config.assets.quiet = false
|
|
199
99
|
end
|
|
200
|
-
end
|
|
201
100
|
|
|
202
|
-
# Before any initializers run, but after the gems have been loaded
|
|
203
|
-
config.before_initialize do
|
|
204
101
|
# Replace the Mongo Loggers
|
|
205
102
|
Mongoid.logger = SemanticLogger[Mongoid] if defined?(Mongoid)
|
|
206
103
|
Moped.logger = SemanticLogger[Moped] if defined?(Moped)
|
|
@@ -215,11 +112,20 @@ module RailsSemanticLogger
|
|
|
215
112
|
# Replace the Sidetiq logger
|
|
216
113
|
Sidetiq.logger = SemanticLogger[Sidetiq] if defined?(Sidetiq)
|
|
217
114
|
|
|
115
|
+
# Replace the DelayedJob logger
|
|
116
|
+
if defined?(Delayed::Worker)
|
|
117
|
+
Delayed::Worker.logger = SemanticLogger[Delayed::Worker]
|
|
118
|
+
Delayed::Worker.plugins << RailsSemanticLogger::DelayedJob::Plugin
|
|
119
|
+
end
|
|
120
|
+
|
|
218
121
|
# Replace the Bugsnag logger
|
|
219
122
|
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
|
|
123
|
+
end
|
|
220
124
|
|
|
221
|
-
|
|
222
|
-
|
|
125
|
+
# After any initializers run, but after the gems have been loaded
|
|
126
|
+
config.after_initialize do
|
|
127
|
+
# Replace the Bugsnag logger
|
|
128
|
+
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
|
|
223
129
|
|
|
224
130
|
# Rails Patches
|
|
225
131
|
require('rails_semantic_logger/extensions/action_cable/tagged_logger_proxy') if defined?(ActionCable)
|
|
@@ -230,34 +136,59 @@ module RailsSemanticLogger
|
|
|
230
136
|
require('rails_semantic_logger/extensions/active_model_serializers/logging') if defined?(ActiveModelSerializers)
|
|
231
137
|
|
|
232
138
|
if config.rails_semantic_logger.semantic
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
139
|
+
# Active Record
|
|
140
|
+
if defined?(::ActiveRecord)
|
|
141
|
+
require 'active_record/log_subscriber'
|
|
142
|
+
|
|
143
|
+
RailsSemanticLogger.swap_subscriber(
|
|
144
|
+
::ActiveRecord::LogSubscriber,
|
|
145
|
+
RailsSemanticLogger::ActiveRecord::LogSubscriber,
|
|
146
|
+
:active_record
|
|
147
|
+
)
|
|
148
|
+
end
|
|
237
149
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
end
|
|
150
|
+
# Rack
|
|
151
|
+
RailsSemanticLogger::Rack::Logger.started_request_log_level = :info if config.rails_semantic_logger.started
|
|
241
152
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
153
|
+
# Silence asset logging by applying a filter to the Rails logger itself, not any of the appenders.
|
|
154
|
+
if config.rails_semantic_logger.quiet_assets && config.assets.prefix
|
|
155
|
+
assets_regex = %r(\A/{0,2}#{config.assets.prefix})
|
|
156
|
+
RailsSemanticLogger::Rack::Logger.logger.filter = ->(log) { log.payload[:path] !~ assets_regex if log.payload }
|
|
157
|
+
end
|
|
245
158
|
|
|
246
|
-
|
|
247
|
-
|
|
159
|
+
# Action View
|
|
160
|
+
RailsSemanticLogger::ActionView::LogSubscriber.rendered_log_level = :info if config.rails_semantic_logger.rendered
|
|
161
|
+
RailsSemanticLogger.swap_subscriber(
|
|
162
|
+
::ActionView::LogSubscriber,
|
|
163
|
+
RailsSemanticLogger::ActionView::LogSubscriber,
|
|
164
|
+
:action_view
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Action Controller
|
|
168
|
+
RailsSemanticLogger.swap_subscriber(
|
|
169
|
+
::ActionController::LogSubscriber,
|
|
170
|
+
RailsSemanticLogger::ActionController::LogSubscriber,
|
|
171
|
+
:action_controller
|
|
172
|
+
)
|
|
248
173
|
end
|
|
249
174
|
|
|
250
|
-
#
|
|
251
|
-
|
|
252
|
-
|
|
175
|
+
#
|
|
176
|
+
# Forking Frameworks
|
|
177
|
+
#
|
|
178
|
+
|
|
179
|
+
# Passenger provides the :starting_worker_process event for executing
|
|
180
|
+
# code after it has forked, so we use that and reconnect immediately.
|
|
181
|
+
if defined?(PhusionPassenger)
|
|
182
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
|
183
|
+
SemanticLogger.reopen if forked
|
|
184
|
+
end
|
|
253
185
|
end
|
|
254
|
-
end
|
|
255
186
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
# Replace the Bugsnag logger
|
|
259
|
-
Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag)
|
|
260
|
-
end
|
|
187
|
+
# Re-open appenders after Resque has forked a worker
|
|
188
|
+
Resque.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Resque)
|
|
261
189
|
|
|
190
|
+
# Re-open appenders after Spring has forked a process
|
|
191
|
+
Spring.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Spring)
|
|
192
|
+
end
|
|
262
193
|
end
|
|
263
194
|
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# Log actual exceptions, not a string representation
|
|
2
2
|
ActionController::Live
|
|
3
|
-
module ActionController
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
module ActionController
|
|
4
|
+
module Live
|
|
5
|
+
def log_error(exception)
|
|
6
|
+
logger.fatal(exception)
|
|
7
|
+
end
|
|
6
8
|
end
|
|
7
9
|
end
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# Log actual exceptions, not a string representation
|
|
2
2
|
ActionDispatch::DebugExceptions
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
module ActionDispatch
|
|
4
|
+
class DebugExceptions
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def log_error(_request, wrapper)
|
|
8
|
+
ActiveSupport::Deprecation.silence do
|
|
9
|
+
ActionController::Base.logger.fatal(wrapper.exception)
|
|
10
|
+
end
|
|
8
11
|
end
|
|
9
12
|
end
|
|
10
13
|
end
|
|
11
|
-
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# Log actual exceptions, not a string representation
|
|
2
2
|
ActionView::StreamingTemplateRenderer
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
class
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
module ActionView
|
|
5
|
+
class StreamingTemplateRenderer
|
|
6
|
+
class Body
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def log_error(exception)
|
|
10
|
+
ActionView::Base.logger.fatal(exception)
|
|
11
|
+
end
|
|
9
12
|
end
|
|
10
13
|
end
|
|
11
14
|
end
|
|
@@ -1,11 +1,135 @@
|
|
|
1
1
|
# Patch ActiveJob logger
|
|
2
2
|
require 'active_job/logging'
|
|
3
3
|
|
|
4
|
-
module ActiveJob
|
|
5
|
-
|
|
4
|
+
module ActiveJob
|
|
5
|
+
module Logging
|
|
6
|
+
include SemanticLogger::Loggable
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def tag_logger(*tags, &block)
|
|
11
|
+
logger.tagged(*tags, &block)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
15
|
+
def enqueue(event)
|
|
16
|
+
log_with_formatter event: event do |fmt|
|
|
17
|
+
"Enqueued #{fmt.job_info}"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def enqueue_at(event)
|
|
22
|
+
log_with_formatter event: event do |fmt|
|
|
23
|
+
"Enqueued #{fmt.job_info} at #{fmt.scheduled_at}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def perform_start(event)
|
|
28
|
+
log_with_formatter event: event do |fmt|
|
|
29
|
+
"Performing #{fmt.job_info}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def perform(event)
|
|
34
|
+
ex = event.payload[:exception_object]
|
|
35
|
+
if ex
|
|
36
|
+
logger.error ex
|
|
37
|
+
else
|
|
38
|
+
log_with_formatter event: event, log_duration: true do |fmt|
|
|
39
|
+
"Performed #{fmt.job_info} in #{event.duration.round(2)}ms"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
class EventFormatter
|
|
47
|
+
def initialize(event:, log_duration: false)
|
|
48
|
+
@event = event
|
|
49
|
+
@log_duration = log_duration
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def job_info
|
|
53
|
+
"#{job.class.name} (Job ID: #{job.job_id}) to #{queue_name}"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def payload
|
|
57
|
+
{}.tap do |h|
|
|
58
|
+
h[:event_name] = event.name
|
|
59
|
+
h[:adapter] = adapter_name
|
|
60
|
+
h[:queue] = job.queue_name
|
|
61
|
+
h[:job_class] = job.class.name
|
|
62
|
+
h[:job_id] = job.job_id
|
|
63
|
+
h[:provider_job_id] = job.try(:provider_job_id) # Not available in Rails 4.2
|
|
64
|
+
h[:duration] = event.duration.round(2) if log_duration?
|
|
65
|
+
h[:arguments] = formatted_args
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def queue_name
|
|
70
|
+
adapter_name + "(#{job.queue_name})"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def scheduled_at
|
|
74
|
+
Time.at(event.payload[:job].scheduled_at).utc
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
attr_reader :event
|
|
80
|
+
|
|
81
|
+
def job
|
|
82
|
+
event.payload[:job]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def adapter_name
|
|
86
|
+
event.payload[:adapter].class.name.demodulize.remove('Adapter')
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def formatted_args
|
|
90
|
+
JSON.pretty_generate(job.arguments.map { |arg| format(arg) })
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def format(arg)
|
|
94
|
+
case arg
|
|
95
|
+
when Hash
|
|
96
|
+
arg.transform_values { |value| format(value) }
|
|
97
|
+
when Array
|
|
98
|
+
arg.map { |value| format(value) }
|
|
99
|
+
when GlobalID::Identification
|
|
100
|
+
begin
|
|
101
|
+
arg.to_global_id
|
|
102
|
+
rescue StandardError
|
|
103
|
+
arg
|
|
104
|
+
end
|
|
105
|
+
else
|
|
106
|
+
arg
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def log_duration?
|
|
111
|
+
@log_duration
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def log_with_formatter(**kw_args)
|
|
116
|
+
fmt = EventFormatter.new(**kw_args)
|
|
117
|
+
msg = yield fmt
|
|
118
|
+
logger.info msg, fmt.payload
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def logger
|
|
122
|
+
ActiveJob::Base.logger
|
|
123
|
+
end
|
|
124
|
+
end
|
|
10
125
|
end
|
|
11
126
|
end
|
|
127
|
+
|
|
128
|
+
if defined?(ActiveSupport::Notifications)
|
|
129
|
+
ActiveSupport::Notifications.unsubscribe('perform_start.active_job')
|
|
130
|
+
ActiveSupport::Notifications.unsubscribe('perform.active_job')
|
|
131
|
+
ActiveSupport::Notifications.unsubscribe('enqueue_at.active_job')
|
|
132
|
+
ActiveSupport::Notifications.unsubscribe('enqueue.active_job')
|
|
133
|
+
|
|
134
|
+
ActiveJob::Logging::LogSubscriber.attach_to :active_job
|
|
135
|
+
end
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
# Patch ActiveModelSerializers logger
|
|
2
2
|
require 'active_model_serializers/logging'
|
|
3
3
|
|
|
4
|
-
module ActiveModelSerializers
|
|
5
|
-
|
|
4
|
+
module ActiveModelSerializers
|
|
5
|
+
module Logging
|
|
6
|
+
include SemanticLogger::Loggable
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def tag_logger(*tags, &block)
|
|
11
|
+
logger.tagged(*tags, &block)
|
|
12
|
+
end
|
|
10
13
|
end
|
|
11
|
-
end
|
|
12
14
|
|
|
13
|
-
class
|
|
14
|
-
|
|
15
|
+
class SerializableResource
|
|
16
|
+
include SemanticLogger::Loggable
|
|
17
|
+
end
|
|
15
18
|
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
module RailsSemanticLogger
|
|
2
|
+
# Options for controlling Rails Semantic Logger behavior
|
|
3
|
+
#
|
|
4
|
+
# * Convert Action Controller and Active Record text messages to semantic data
|
|
5
|
+
#
|
|
6
|
+
# Rails -- Started -- { :ip => "127.0.0.1", :method => "GET", :path => "/dashboards/inquiry_recent_activity" }
|
|
7
|
+
# UserController -- Completed #index -- { :action => "index", :db_runtime => 54.64, :format => "HTML", :method => "GET", :mongo_runtime => 0.0, :path => "/users", :status => 200, :status_message => "OK", :view_runtime => 709.88 }
|
|
8
|
+
#
|
|
9
|
+
# config.rails_semantic_logger.semantic = true
|
|
10
|
+
#
|
|
11
|
+
# * Change Rack started message to debug so that it does not appear in production
|
|
12
|
+
#
|
|
13
|
+
# config.rails_semantic_logger.started = false
|
|
14
|
+
#
|
|
15
|
+
# * Change Processing message to debug so that it does not appear in production
|
|
16
|
+
#
|
|
17
|
+
# config.rails_semantic_logger.processing = false
|
|
18
|
+
#
|
|
19
|
+
# * Change Action View render log messages to debug so that they do not appear in production
|
|
20
|
+
#
|
|
21
|
+
# ActionView::Base -- Rendered data/search/_user.html.haml (46.7ms)
|
|
22
|
+
#
|
|
23
|
+
# config.rails_semantic_logger.rendered = false
|
|
24
|
+
#
|
|
25
|
+
# * Override the Awesome Print options for logging Hash data as text:
|
|
26
|
+
#
|
|
27
|
+
# Any valid AwesomePrint option for rendering data.
|
|
28
|
+
# The defaults can changed be creating a `~/.aprc` file.
|
|
29
|
+
# See: https://github.com/michaeldv/awesome_print
|
|
30
|
+
#
|
|
31
|
+
# Note: The option :multiline is set to false if not supplied.
|
|
32
|
+
# Note: Has no effect if Awesome Print is not installed.
|
|
33
|
+
#
|
|
34
|
+
# config.rails_semantic_logger.ap_options = {multiline: false}
|
|
35
|
+
#
|
|
36
|
+
# * Whether to automatically add an environment specific log file appender.
|
|
37
|
+
# For Example: 'log/development.log'
|
|
38
|
+
#
|
|
39
|
+
# Note:
|
|
40
|
+
# When Semantic Logger fails to log to an appender it logs the error to an
|
|
41
|
+
# internal logger, which by default writes to STDERR.
|
|
42
|
+
# Example, change the default internal logger to log to stdout:
|
|
43
|
+
# SemanticLogger::Processor.logger = SemanticLogger::Appender::File.new(io: STDOUT, level: :warn)
|
|
44
|
+
#
|
|
45
|
+
# config.rails_semantic_logger.add_file_appender = true
|
|
46
|
+
#
|
|
47
|
+
# * Silence asset logging
|
|
48
|
+
#
|
|
49
|
+
# config.rails_semantic_logger.quiet_assets = false
|
|
50
|
+
#
|
|
51
|
+
# * Override the output format for the primary Rails log file.
|
|
52
|
+
#
|
|
53
|
+
# Valid options:
|
|
54
|
+
# * :default
|
|
55
|
+
# Plain text output with no color.
|
|
56
|
+
# * :color
|
|
57
|
+
# Plain text output with color.
|
|
58
|
+
# * :json
|
|
59
|
+
# JSON output format.
|
|
60
|
+
# * class
|
|
61
|
+
#
|
|
62
|
+
# * Proc
|
|
63
|
+
# A block that will be called to format the output.
|
|
64
|
+
# It is supplied with the `log` entry and should return the formatted data.
|
|
65
|
+
#
|
|
66
|
+
# Note:
|
|
67
|
+
# * `:default` is automatically changed to `:color` if `config.colorize_logging` is `true`.
|
|
68
|
+
#
|
|
69
|
+
# JSON Example, in `application.rb`:
|
|
70
|
+
# config.rails_semantic_logger.format = :json
|
|
71
|
+
#
|
|
72
|
+
# Custom Example, create `app/lib/my_formatter.rb`:
|
|
73
|
+
#
|
|
74
|
+
# # My Custom colorized formatter
|
|
75
|
+
# class MyFormatter < SemanticLogger::Formatters::Color
|
|
76
|
+
# # Return the complete log level name in uppercase
|
|
77
|
+
# def level
|
|
78
|
+
# "#{color}log.level.upcase#{color_map.clear}"
|
|
79
|
+
# end
|
|
80
|
+
# end
|
|
81
|
+
#
|
|
82
|
+
# # In application.rb:
|
|
83
|
+
# config.rails_semantic_logger.format = MyFormatter.new
|
|
84
|
+
#
|
|
85
|
+
#
|
|
86
|
+
# config.rails_semantic_logger.format = :default
|
|
87
|
+
#
|
|
88
|
+
# * Add a filter to the file logger [Regexp|Proc]
|
|
89
|
+
# RegExp: Only include log messages where the class name matches the supplied
|
|
90
|
+
# regular expression. All other messages will be ignored.
|
|
91
|
+
# Proc: Only include log messages where the supplied Proc returns true.
|
|
92
|
+
# The Proc must return true or false.
|
|
93
|
+
#
|
|
94
|
+
# config.rails_semantic_logger.filter = nil
|
|
95
|
+
#
|
|
96
|
+
# * named_tags: *DEPRECATED*
|
|
97
|
+
# Instead, supply a Hash to config.log_tags
|
|
98
|
+
# config.rails_semantic_logger.named_tags = nil
|
|
99
|
+
class Options
|
|
100
|
+
attr_accessor :semantic, :started, :processing, :rendered, :ap_options, :add_file_appender,
|
|
101
|
+
:quiet_assets, :format, :named_tags, :filter
|
|
102
|
+
|
|
103
|
+
# Setup default values
|
|
104
|
+
def initialize
|
|
105
|
+
@semantic = true
|
|
106
|
+
@started = false
|
|
107
|
+
@processing = false
|
|
108
|
+
@rendered = false
|
|
109
|
+
@ap_options = {multiline: false}
|
|
110
|
+
@add_file_appender = true
|
|
111
|
+
@quiet_assets = false
|
|
112
|
+
@format = :default
|
|
113
|
+
@named_tags = nil
|
|
114
|
+
@filter = nil
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|