/
railtie.rb
89 lines (74 loc) · 3.6 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
require 'rails/railtie'
require 'action_view/log_subscriber'
require 'action_controller/log_subscriber'
require 'socket'
module LogStasher
def default_source
case RUBY_PLATFORM
when /darwin/
# NOTE: MacOS Sierra and later are setting `.local`
# hostnames that even as real hostnames without the `.local` part,
# are still unresolvable. One reliable way to get an IP is to
# get all available IP address lists and use the first one.
# This will always be `127.0.0.1`.
address_info = Socket.ip_address_list.first
address_info&.ip_address
else
IPSocket.getaddress(Socket.gethostname)
end
end
class Railtie < Rails::Railtie
config.logstasher = ::ActiveSupport::OrderedOptions.new
config.logstasher.enabled = false
# Set up the default logging options
config.logstasher.controller_enabled = true
config.logstasher.mailer_enabled = true
config.logstasher.record_enabled = false
config.logstasher.view_enabled = true
config.logstasher.job_enabled = true
config.logstasher.source = LogStasher.default_source
# Try loading the config/logstasher.yml if present
env = Rails.env.to_sym || :development
config_file = File.expand_path './config/logstasher.yml'
# Load and ERB templating of YAML files
LOGSTASHER = File.exist?(config_file) ? YAML.safe_load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
initializer :logstasher, before: :load_config_initializers do |app|
if LOGSTASHER.present?
# process common configs
LogStasher.process_config(app.config.logstasher, LOGSTASHER)
# process environment specific configs
LogStasher.process_config(app.config.logstasher, LOGSTASHER[env].symbolize_keys) if LOGSTASHER.key? env
end
app.config.action_dispatch.rack_cache[:verbose] = false if rack_cache_hashlike?(app)
LogStasher.setup_before(app.config.logstasher) if app.config.logstasher.enabled
end
initializer :logstasher do
config.after_initialize do
LogStasher.setup(config.logstasher) if config.logstasher.enabled
end
end
def rack_cache_hashlike?(app)
app.config.action_dispatch.rack_cache&.respond_to?(:[]=)
end
end
def process_config(config, yml_config)
# Enable the logstasher logs for the current environment
config.enabled = yml_config[:enabled] if yml_config.key? :enabled
config.controller_enabled = yml_config[:controller_enabled] if yml_config.key? :controller_enabled
config.mailer_enabled = yml_config[:mailer_enabled] if yml_config.key? :mailer_enabled
config.record_enabled = yml_config[:record_enabled] if yml_config.key? :record_enabled
config.view_enabled = yml_config[:view_enabled] if yml_config.key? :view_enabled
config.job_enabled = yml_config[:job_enabled] if yml_config.key? :job_enabled
# This line is optional if you do not want to suppress app logs in your <environment>.log
config.suppress_app_log = yml_config[:suppress_app_log] if yml_config.key? :suppress_app_log
# This line is optional, it allows you to set a custom value for the @source field of the log event
config.source = yml_config.key?(:source) ? yml_config[:source] : default_source
config.backtrace = yml_config[:backtrace] if yml_config.key? :backtrace
config.logger_path = yml_config[:logger_path] if yml_config.key? :logger_path
config.log_level = yml_config[:log_level] if yml_config.key? :log_level
if yml_config.key? :log_controller_parameters
config.log_controller_parameters = yml_config[:log_controller_parameters]
end
end
end