class RTALogger::LogRepository

base log repository class

Attributes

enable[RW]
formatter[RW]
log_records[R]
semaphore[R]
title[RW]

def self.create type

requested_class = @@sub_classes[type]
if requested_class
  requested_class.new
else
  raise "Bad log repository type: #{type}"
end

end

def self.register repository_name

@@sub_classes[repository_name] = self

end

Public Class Methods

new() click to toggle source
# File lib/log_repository.rb, line 7
def initialize
  @semaphore = Mutex.new
  @log_records = []
  @title = self.class.to_s.split('::').last.underscore
  @enable = true
  @formatter = RTALogger::LogFactory.log_formatter_default
  @filters = {}
end

Public Instance Methods

add_log_records(items) click to toggle source
# File lib/log_repository.rb, line 35
def add_log_records(items)
  return 0 unless @enable
  @semaphore.synchronize do
    items.each do |item|
      @log_records.push(item.dup) if filters_accept(item)
    end
  end

  flush_and_clear
end
apply_run_time_config(config_json) click to toggle source
# File lib/log_repository.rb, line 86
def apply_run_time_config(config_json)
  return unless config_json
  @enable = config_json['enable'] unless config_json['enable'].nil?
  @formatter.apply_run_time_config(config_json['formatter'])
  apply_run_time_config_filters(config_json)
end
filter_by_title(title) click to toggle source
# File lib/log_repository.rb, line 74
def filter_by_title(title)
  result = nil
  @semaphore.synchronize do
    @filters.keys.each do |filter_key|
      result = @filters[filter_key.to_sym] if filter_key.to_s.casecmp(title).zero?
      break if result
    end
  end

  return result
end
load_config(config_json) click to toggle source
# File lib/log_repository.rb, line 46
def load_config(config_json)
  @enable = config_json['enable'].nil? ? true : config_json['enable']
  @title = config_json['title'].nil? ? self.class.to_s.split('::').last.underscore : config_json['title']
  formatter_config = config_json['formatter']

  @formatter = LogFactory.create_formatter(formatter_config['type'], formatter_config) if formatter_config && formatter_config['type']

  apply_config_filters(config_json)
end
reveal_config() click to toggle source
# File lib/log_repository.rb, line 70
def reveal_config
  to_builder.target!
end
to_builder() click to toggle source
# File lib/log_repository.rb, line 56
def to_builder
  jb = Jbuilder.new do |json|
    json.type self.class.to_s.split('::').last.underscore.sub('log_repository_', '')
    json.title @title
    json.enable enable
    json.formatter @formatter.to_builder.attributes!
    json.filters do
      json.array! @filters.keys.collect { |filter_key| @filters[filter_key].to_builder.attributes! }
    end
  end

  jb
end

Protected Instance Methods

apply_config_filters(config_json) click to toggle source
# File lib/log_repository.rb, line 95
def apply_config_filters(config_json)
  config_json['filters']&.each do |filter_config|
    next if filter_config['type'].nil? || filter_config['title'].nil?
    filter = LogFactory.create_filter(filter_config['type'], filter_config)
    @filters[filter_config['title'].to_sym] = filter if filter.present?
  end
end
apply_run_time_config_filters(config_json) click to toggle source
# File lib/log_repository.rb, line 103
def apply_run_time_config_filters(config_json)
  return unless config_json

  config_json['filters']&.each do |filter_config|
    next if filter_config['title'].nil?
    filter = filter_by_title(filter_config['title'])
    if filter.present?
      filter.apply_run_time_config(filter_config)
    else
      filter = LogFactory.create_filter(filter_config['type'], filter_config)
      @semaphore.synchronize { @filters[filter_config['title'].to_sym] = filter if filter.present? }
    end
  end
end
filters_accept(log_record) click to toggle source
# File lib/log_repository.rb, line 122
def filters_accept(log_record)
  result = true
  @filters&.keys.each do |filter_key|
    result = @filters[filter_key.to_sym]&.match_conditions(log_record)
    break unless result
  end

  return result
end
flush_and_clear() click to toggle source
# File lib/log_repository.rb, line 118
def flush_and_clear
  @semaphore.synchronize { @log_records.clear }
end