[PATCH/foreman 1/1] Fixes #593 - Separate log file for facts and reports

Signed-off-by: Paul Kelly <paul.ian.kelly@googlemail.com>

··· --- app/models/fact_value.rb | 4 ++++ app/models/report.rb | 4 ++++ config/initializers/foreman.rb | 9 +++++++++ lib/foreman.rb | 1 + lib/foreman/threadsession.rb | 1 - lib/log_switcher.rb | 21 +++++++++++++++++++++ 6 files changed, 39 insertions(+), 1 deletions(-) create mode 100644 lib/log_switcher.rb

diff --git a/app/models/fact_value.rb b/app/models/fact_value.rb
index 94aaa1a…f61c4d5 100644
— a/app/models/fact_value.rb
+++ b/app/models/fact_value.rb
@@ -17,6 +17,10 @@ class FactValue < Puppet::Rails::FactValue
scope :required_fields, { :include => :host }
default_scope :order => ‘LOWER(fact_values.value)’

  • def logger
  • Foreman.fact_logger
  • end
  • Todo: find a way to filter which values are logged,

    this generates too much useless data

diff --git a/app/models/report.rb b/app/models/report.rb
index 2308714…6435ec9 100644
— a/app/models/report.rb
+++ b/app/models/report.rb
@@ -46,6 +46,10 @@ class Report < ActiveRecord::Base
write_attribute(:metrics,m.to_yaml) unless m.nil?
end

  • def logger
  • Foreman.report_logger
  • end
  • def to_label
    "#{host.name} / #{reported_at.to_s}"
    end
    diff --git a/config/initializers/foreman.rb b/config/initializers/foreman.rb
    index 1a55ee4…8497341 100644
    — a/config/initializers/foreman.rb
    +++ b/config/initializers/foreman.rb
    @@ -31,3 +31,12 @@ Foreman::DefaultSettings::Loader.load
    Foreman::DefaultData::Loader.load(false)

WillPaginate.per_page = Setting.entries_per_page rescue 20
+
+# We create the report and fact logs
+Foreman::report_logger = ActiveSupport::BufferedLogger.new(Rails.root + “log/#{Rails.env}-report.log”, Rails.logger.level)
+Foreman::fact_logger = ActiveSupport::BufferedLogger.new(Rails.root + “log/#{Rails.env}-fact.log”, Rails.logger.level)
+Foreman::default_logger = Rails.logger
+# and now activate the custom logger that uses them.
+# Unfortunately the first line of the log is written by the metal so we need to modify the rack stack
+Rails.configuration.middleware.swap Rails::Rack::Logger, Foreman::LogSwitcher
+
diff --git a/lib/foreman.rb b/lib/foreman.rb
index 2920ff9…34a3574 100644
— a/lib/foreman.rb
+++ b/lib/foreman.rb
@@ -6,4 +6,5 @@ require 'foreman/controller’
require ‘net’

module Foreman

  • mattr_accessor :report_logger, :fact_logger, :default_logger
    end
    diff --git a/lib/foreman/threadsession.rb b/lib/foreman/threadsession.rb
    index 443f20f…e8df88f 100644
    — a/lib/foreman/threadsession.rb
    +++ b/lib/foreman/threadsession.rb
    @@ -27,7 +27,6 @@ module Foreman
    unless (o.nil? || o.is_a?(self))
    raise(ArgumentError, “Unable to set current User, expected class ‘#{self}’, got #{o.inspect}”)
    end
  •        Rails.logger.debug "Setting current user thread-local variable to " + (o.is_a?(User) ? o.login : 'nil')
           Thread.current[:user] = o
         end
    

diff --git a/lib/log_switcher.rb b/lib/log_switcher.rb
new file mode 100644
index 0000000…2cd58bc
— /dev/null
+++ b/lib/log_switcher.rb
@@ -0,0 +1,21 @@
+class LogSwitcher < Rails::Rack::Logger

  • def initialize(app, opts = {})
  • @app = app
  • @opts = opts
  • @opts[:silenced] ||= []
  • @report_regex = %r{^/reports}
  • @fact_regex = %r{^/fact} # This will match /fact_values and /facts but /host/:id/facts will end up in the main log.
  • end
  • def call(env)
  • if env[‘PATH_INFO’] =~ @report_regex
  •  ActiveRecord::Base.logger = ActionController::Base.logger = @logger = Foreman::report_logger
    
  • elsif env[‘PATH_INFO’] =~ @fact_regex
  •  ActiveRecord::Base.logger = ActionController::Base.logger = @logger = Foreman::fact_logger
    
  • else
  •  ActiveRecord::Base.logger = ActionController::Base.logger = @logger = Foreman::default_logger
    
  • end
  • super(env)
  • end
    +end
    \ No newline at end of file

    1.7.5.4