lib/bmc-daemon-lib/conf.rb in bmc-daemon-lib-0.3.16 vs lib/bmc-daemon-lib/conf.rb in bmc-daemon-lib-0.3.18

- old
+ new

@@ -76,14 +76,10 @@ # Set up encodings Encoding.default_internal = "utf-8" Encoding.default_external = "utf-8" - # Init New Relic, Rollbar - # prepare_newrelic - #prepare_rollbar if self.feature?(:rollbar) - # Try to access any key to force parsing of the files self[:dummy] rescue Psych::SyntaxError => e fail ConfigParseError, e.message @@ -106,25 +102,34 @@ def self.at *path ensure_init path.reduce(Conf) { |m, key| m && m[key.to_s] } end - def self.logfile_path pipe - # Access configuration - path = Conf.at :logs, :path - specific = Conf.at :logs, pipe - default = Conf.at :logs, :default + def self.logfile pipe + # Build logfile from Conf + logfile = self.logfile_path(pipe) + return nil if logfile.nil? - # Ignore if explicitely disabled - return nil if specific.nil? + # Check that we'll be able to create logfiles + if File.exists?(logfile) + # File is there, is it writable ? + unless File.writable?(logfile) + log :conf, "logging [#{pipe}] disabled: file not writable [#{logfile}]" + return nil + end + else + # No file here, can we create it ? + logdir = File.dirname(logfile) + unless File.writable?(logdir) + log :conf, "logging [#{pipe}] disabled: directory not writable [#{logdir}]" + return nil + end + end - # Fallback on default path if not provided, - specific ||= default - specific ||= "default.log" - - # Build logfile_path - File.expand_path specific.to_s, path.to_s + # OK, return a clean file path + log :conf, "logging [#{pipe}] to [#{logfile}]" + return logfile end def self.feature? name ensure_init @@ -176,33 +181,36 @@ end def self.prepare_newrelic # Disable if no config present return unless self.feature?(:newrelic) + + # Ok, let's start section = self[:newrelic] + log :conf, "prepare NewRelic" # Enable GC profiler GC::Profiler.enable - # Set logfile, license, monitor mode - ENV["NEW_RELIC_LOG"] = logfile_path(:newrelic) - ENV["NEW_RELIC_LICENSE_KEY"] = section[:license].to_s - ENV["NEW_RELIC_MONITOR_MODE"] = "true" - # Build NewRelic app_name if not provided as-is if !section[:app_name] stack = [] stack << (section[:prefix] || @app_name) stack << section[:platform] if section[:platform] stack << @app_env text = stack.join('-') section[:app_name] = "#{text}; #{text}-#{host}" end - ENV["NEW_RELIC_APP_NAME"] = section[:app_name].to_s - # Enable module - ENV["NEWRELIC_AGENT_ENABLED"] = "true" + # Start the agent + NewRelic::Agent.manual_start({ + agent_enabled: true, + log: LoggerPool.instance.get(:newrelic), + env: @app_env, + license_key: section[:license].to_s, + app_name: section[:app_name].to_s, + }) end def self.prepare_rollbar # Disable if no config present unless self.feature?(:rollbar) @@ -210,30 +218,31 @@ config.enabled = false end return end + # Ok, let's start section = self[:rollbar] + log :conf, "prepare Rollbar" # Configure Rollbar.configure do |config| config.enabled = true config.access_token = section[:token].to_s config.code_version = @app_version config.environment = @app_env - #config.logger = Logger.new(logfile_path(:rollbar)) - config.logger = LoggerPool.instance.get :rollbar + config.logger = LoggerPool.instance.get(:rollbar) config.use_async = true end # Notify startup Rollbar.info("#{@app_name} #{@app_ver} [#{@host}]") end def self.log origin, message printf( - "%s %-10s %s \n", + "%s %-14s %s \n", Time.now.strftime("%Y-%m-%d %H:%M:%S"), origin, message ) end @@ -244,9 +253,27 @@ load files: @files, namespaces: { environment: @app_env } end def self.add_config path @files << File.expand_path(path) if path && File.readable?(path) + end + + def self.logfile_path pipe + # Access configuration + path = self.at :logs, :path + specific = self.at :logs, pipe + default = self.at :logs, :default + + # Ignore if explicitely disabled + return nil if specific.nil? + return nil if specific == false + + # Fallback on default path if not provided, + specific ||= default + specific ||= "default.log" + + # Build logfile_path + File.expand_path specific.to_s, path.to_s end private def self.ensure_init