Skip to content

Commit

Permalink
Merge pull request #20950 from Fryguy/pluggable_loggers
Browse files Browse the repository at this point in the history
Move logger initialization and configuration into plugins
  • Loading branch information
agrare authored Jan 15, 2021
2 parents 0a8c6f8 + 297aa6f commit fb0610a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
11 changes: 7 additions & 4 deletions .rubocop_local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@ AllCops:
Style/GlobalVars:
AllowedVariables:
# Loggers
- $ansible_tower_log
- $api_log
- $audit_log
- $aws_log
- $azure_log
- $azure_stack_log
- $cn_monitoring_log
- $container_log
- $datawarehouse_log
- $fog_log
- $gce_log
- $ibm_cloud_log
- $ibm_terraform_log
- $journald_log
- $kube_log
- $lenovo_log
- $log
- $miq_ae_logger
- $nsxt_log
- $nuage_log
- $policy_log
- $rails_log
- $redfish_log
- $remote_console_log
- $rhevm_log
- $scvmm_log
- $vcloud_log
- $vim_log
- $remote_console_log
- $nuage_log
- $nsxt_log
- $redfish_log
Rails/Exit:
Exclude:
- lib/workers/bin/*
Expand Down
2 changes: 2 additions & 0 deletions app/models/file_depot_s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def connect(options = {})
# Note: The hard-coded aws_region will be removed after manageiq-ui-class implements region selection
aws_region = options[:region] || "us-east-1"

$aws_log ||= Vmdb::Loggers.create_logger("aws.log")

Aws::S3::Resource.new(
:access_key_id => username,
:secret_access_key => ManageIQ::Password.try_decrypt(password),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ def self.vmdb_plugin?
def self.plugin_name
_('<%= plugin_human_name %>')
end

def self.init_loggers
$<%= plugin_name %>_log ||= Vmdb::Loggers.create_logger("<%= plugin_name %>.log")
end

def self.apply_logger_config(config)
Vmdb::Loggers.apply_config_value(config, $<%= plugin_name %>_log, :level_<%= plugin_name %>)
end
end
<% end %>
89 changes: 47 additions & 42 deletions lib/vmdb/loggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ def self.apply_config(config)
apply_config_value(config, $log, :level)
apply_config_value(config, $journald_log, :level) if $journald_log
apply_config_value(config, $rails_log, :level_rails)
apply_config_value(config, $ansible_tower_log, :level_ansible_tower)
apply_config_value(config, $policy_log, :level_policy)
apply_config_value(config, $remote_console_log, :level_remote_console)

# TODO: Move this into the manageiq-api plugin
apply_config_value(config, $api_log, :level_api)
# TODO: Move this into the manageiq-automation_engine plugin
apply_config_value(config, $miq_ae_logger, :level_automation)
apply_config_value(config, $aws_log, :level_aws)
# TODO: Move these to their respective provider plugins
apply_config_value(config, $ansible_tower_log, :level_ansible_tower)
apply_config_value(config, $azure_log, :level_azure)
apply_config_value(config, $azure_stack_log, :level_azure_stack)
apply_config_value(config, $cn_monitoring_log, :level_cn_monitoring)
Expand All @@ -39,57 +44,59 @@ def self.apply_config(config)
apply_config_value(config, $lenovo_log, :level_lenovo)
apply_config_value(config, $nsxt_log, :level_nsxt)
apply_config_value(config, $nuage_log, :level_nuage)
apply_config_value(config, $policy_log, :level_policy)
apply_config_value(config, $redfish_log, :level_redfish)
apply_config_value(config, $rhevm_log, :level_rhevm)
apply_config_value(config, $scvmm_log, :level_scvmm)
apply_config_value(config, $vcloud_log, :level_vcloud)
apply_config_value(config, $vim_log, :level_vim)
apply_config_value(config, $remote_console_log, :level_remote_console)

Vmdb::Plugins.each { |p| p.try(:apply_logger_config, config) }
end

private_class_method def self.create_loggers
path_dir = ManageIQ.root.join("log")
def self.create_logger(log_file_name, logger_class = VMDBLogger)
log_file = ManageIQ.root.join("log", log_file_name)
logger_class.new(log_file).tap do |logger|
logger.extend(ActiveSupport::Logger.broadcast($container_log)) if $container_log
logger.extend(ActiveSupport::Logger.broadcast($journald_log)) if $journald_log
end
end

private_class_method def self.create_loggers
$container_log = create_container_logger
$journald_log = create_journald_logger
$log = create_multicast_logger(path_dir.join("evm.log"))
$rails_log = create_multicast_logger(path_dir.join("#{Rails.env}.log"))
$audit_log = create_multicast_logger(path_dir.join("audit.log"), AuditLogger)
$api_log = create_multicast_logger(path_dir.join("api.log"))
$ansible_tower_log = create_multicast_logger(path_dir.join("ansible_tower.log"))
$miq_ae_logger = create_multicast_logger(path_dir.join("automation.log"))
$aws_log = create_multicast_logger(path_dir.join("aws.log"))
$azure_log = create_multicast_logger(path_dir.join("azure.log"), ProviderSdkLogger)
$azure_stack_log = create_multicast_logger(path_dir.join("azure_stack.log"))
$cn_monitoring_log = create_multicast_logger(path_dir.join("container_monitoring.log"))
$datawarehouse_log = create_multicast_logger(path_dir.join("datawarehouse.log"))
$fog_log = create_multicast_logger(path_dir.join("fog.log"), FogLogger)
$gce_log = create_multicast_logger(path_dir.join("gce.log"))
$ibm_cloud_log = create_multicast_logger(path_dir.join("ibm_cloud.log"), ProviderSdkLogger)
$ibm_terraform_log = create_multicast_logger(path_dir.join("ibm_terraform.log"), ProviderSdkLogger)
$kube_log = create_multicast_logger(path_dir.join("kubernetes.log"))
$lenovo_log = create_multicast_logger(path_dir.join("lenovo.log"))
$nsxt_log = create_multicast_logger(path_dir.join("nsxt.log"))
$nuage_log = create_multicast_logger(path_dir.join("nuage.log"))
$policy_log = create_multicast_logger(path_dir.join("policy.log"))
$redfish_log = create_multicast_logger(path_dir.join("redfish.log"))
$rhevm_log = create_multicast_logger(path_dir.join("rhevm.log"))
$scvmm_log = create_multicast_logger(path_dir.join("scvmm.log"))
$vcloud_log = create_multicast_logger(path_dir.join("vcloud.log"))
$vim_log = create_multicast_logger(path_dir.join("vim.log"))
$remote_console_log = create_multicast_logger(path_dir.join("remote_console.log"))
$log = create_logger("evm.log")
$rails_log = create_logger("#{Rails.env}.log")
$audit_log = create_logger("audit.log", AuditLogger)
$policy_log = create_logger("policy.log")
$remote_console_log = create_logger("remote_console.log")

# TODO: Move this into the manageiq-api plugin
$api_log = create_logger("api.log")
# TODO: Move this into the manageiq-automation_engine plugin
$miq_ae_logger = create_logger("automation.log")
# TODO: Move these to their respective provider plugins
$ansible_tower_log = create_logger("ansible_tower.log")
$azure_log = create_logger("azure.log", ProviderSdkLogger)
$azure_stack_log = create_logger("azure_stack.log")
$cn_monitoring_log = create_logger("container_monitoring.log")
$datawarehouse_log = create_logger("datawarehouse.log")
$fog_log = create_logger("fog.log", FogLogger)
$gce_log = create_logger("gce.log")
$ibm_cloud_log = create_logger("ibm_cloud.log", ProviderSdkLogger)
$ibm_terraform_log = create_logger("ibm_terraform.log", ProviderSdkLogger)
$kube_log = create_logger("kubernetes.log")
$lenovo_log = create_logger("lenovo.log")
$nsxt_log = create_logger("nsxt.log")
$nuage_log = create_logger("nuage.log")
$redfish_log = create_logger("redfish.log")
$rhevm_log = create_logger("rhevm.log")
$scvmm_log = create_logger("scvmm.log")
$vcloud_log = create_logger("vcloud.log")
$vim_log = create_logger("vim.log")

configure_external_loggers
end

private_class_method def self.create_multicast_logger(log_file_path, logger_class = VMDBLogger)
logger_class.new(log_file_path).tap do |logger|
logger.extend(ActiveSupport::Logger.broadcast($container_log)) if $container_log
logger.extend(ActiveSupport::Logger.broadcast($journald_log)) if $journald_log
end
end

private_class_method def self.create_container_logger
return unless ENV["CONTAINER"]

Expand All @@ -106,7 +113,7 @@ def self.apply_config(config)
nil
end

def self.configure_external_loggers
private_class_method def self.configure_external_loggers
require 'awesome_spawn'
AwesomeSpawn.logger = $log

Expand All @@ -116,7 +123,6 @@ def self.configure_external_loggers
require 'inventory_refresh'
InventoryRefresh.logger = $log
end
private_class_method :configure_external_loggers

def self.apply_config_value(config, logger, key)
old_level = logger.level
Expand All @@ -127,7 +133,6 @@ def self.apply_config_value(config, logger, key)
logger.level = new_level
end
end
private_class_method :apply_config_value
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/vmdb/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def each(&block)

def init
load_inflections
init_loggers
register_models
end

Expand Down Expand Up @@ -89,6 +90,12 @@ def load_inflections
end
end

def init_loggers
each do |engine|
engine.try(:init_loggers)
end
end

def register_models
each do |engine|
# make sure STI models are recognized
Expand Down
14 changes: 7 additions & 7 deletions spec/lib/vmdb/loggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def in_container_env(example)
$container_log = nil
end

describe "#create_multicast_logger (private)" do
describe ".create_logger" do
shared_examples "has basic logging functionality" do
subject { described_class.send(:create_multicast_logger, log_file) }
subject { described_class.create_logger(log_file) }

before do
# Hide the container logger output to STDOUT
Expand Down Expand Up @@ -121,14 +121,14 @@ def in_container_env(example)
end
end

describe "#apply_config_value (private)" do
describe ".apply_config_value" do
before do
allow($log).to receive(:info)
end

it "will update the main lower level logger instance" do
log = described_class.send(:create_multicast_logger, log_file)
described_class.send(:apply_config_value, {:foo => :info}, log, :foo)
log = described_class.create_logger(log_file)
described_class.apply_config_value({:foo => :info}, log, :foo)

expect(log.level).to eq(Logger::INFO)
end
Expand All @@ -137,8 +137,8 @@ def in_container_env(example)
around { |example| in_container_env(example) }

it "will always keep $container_log as DEBUG" do
log = described_class.send(:create_multicast_logger, log_file)
described_class.send(:apply_config_value, {:foo => :info}, log, :foo)
log = described_class.create_logger(log_file)
described_class.apply_config_value({:foo => :info}, log, :foo)

expect(log.level).to eq(Logger::INFO)
expect($container_log.level).to eq(Logger::DEBUG)
Expand Down

0 comments on commit fb0610a

Please sign in to comment.