From 0dfc01613bce034c2f9fcffd5059706a618d73bb Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 12 Jun 2017 11:07:57 -0500 Subject: [PATCH 1/3] Setup namespace in loggers before child requires When doing the following: Dir.glob(File.join(File.dirname(__FILE__), "loggers", "*")).each { |f| require f } We are including a bunch of classes do the following: module Vmdb::Loggers class MyLogger < VmdbLogger end end This works fine when the module Vmdb::Loggers has been defined, but when requiring `lib/vmdb/loggers.rb` directly, this is usually not the case. By pushing the requires of the child loggers to the bottom of the file, we ensure that the `Vmdb` module has been defined, and it's child, `Loggers`, has also been defined. This allows the following: require "vmdb/loggers" require "vmdb/logging" To work with minimal prerequisites and doesn't require autoloading from rails. --- lib/vmdb/loggers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vmdb/loggers.rb b/lib/vmdb/loggers.rb index 99e9c9e619a..fc0537c447a 100644 --- a/lib/vmdb/loggers.rb +++ b/lib/vmdb/loggers.rb @@ -1,7 +1,5 @@ require 'util/vmdb-logger' -Dir.glob(File.join(File.dirname(__FILE__), "loggers", "*")).each { |f| require f } - module Vmdb def self.logger $log @@ -85,3 +83,5 @@ def self.apply_config_value_logged(config, logger, level_method, key) end end end + +Dir.glob(File.join(File.dirname(__FILE__), "loggers", "*")).each { |f| require f } From 1b3390afa369a6a61d274cd311c761bae8e3d969 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 12 Jun 2017 14:22:21 -0500 Subject: [PATCH 2/3] Use non-rails way for delegation in vmdb/logging Allows us to load vmdb/logging without needed to require `active_support/dependencies` to included the `delegation` class method onto modules/objects. This doesn't save us much space, and reduces the dependency on rails incase we need to load the logging earlier and possibly without rails/active_support. Included some comments to allow greping for the method definitions via a `git grep "def log\?"` or something similar. --- lib/vmdb/logging.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/vmdb/logging.rb b/lib/vmdb/logging.rb index 8a8326be594..483f4074333 100644 --- a/lib/vmdb/logging.rb +++ b/lib/vmdb/logging.rb @@ -4,8 +4,15 @@ module Vmdb class LogProxy < Struct.new(:klass, :separator) LEVELS = [:debug, :info, :warn, :error] - delegate *LEVELS.map { |level| :"#{level}?" }, - :log_backtrace, :level, :to => :logger + # def debug? + # def info? + # def warn? + # def error? + # def log_backtrace + # def level + (LEVELS.map { |l| :"#{l}?" } + [:log_backtrace, :level]).each do |method| + define_method(method) { |*args| logger.send(method, *args) } + end LEVELS.each do |level| define_method(level) do |msg = nil, &blk| From 5d9c4ec6a3e981d1690504a605786c138da716e4 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 12 Jun 2017 15:12:29 -0500 Subject: [PATCH 3/3] Use manageiq lib for project root and env checks --- lib/vmdb/loggers.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/vmdb/loggers.rb b/lib/vmdb/loggers.rb index fc0537c447a..b1a540b8d40 100644 --- a/lib/vmdb/loggers.rb +++ b/lib/vmdb/loggers.rb @@ -1,3 +1,4 @@ +require 'manageiq' require 'util/vmdb-logger' module Vmdb @@ -36,10 +37,10 @@ def self.apply_config(config) private def self.create_loggers - path_dir = Rails.root.join("log") + path_dir = ManageIQ.root.join("log") $log = VMDBLogger.new(path_dir.join("evm.log")) - $rails_log = VMDBLogger.new(path_dir.join("#{Rails.env}.log")) + $rails_log = VMDBLogger.new(path_dir.join("#{ManageIQ.env}.log")) $audit_log = AuditLogger.new(path_dir.join("audit.log")) $fog_log = FogLogger.new(path_dir.join("fog.log")) $policy_log = MirroredLogger.new(path_dir.join("policy.log"), " ")