module LogicalModel::SafeLog::ClassMethods

Attributes

log_path[RW]

Public Instance Methods

log_failed(response) click to toggle source
# File lib/logical_model/safe_log.rb, line 38
def log_failed(response)
  begin
    error_message = ActiveSupport::JSON.decode(response.body)["message"]
  rescue => e
    error_message = "error"
  end
  msg = "LogicalModel Log: #{response.code} #{mask_api_key(response.effective_url)} in #{response.time}s FAILED: #{error_message}"
  self.logger.warn { msg }
  self.logger.debug { "LogicalModel Log RESPONSE: #{safe_body(response.body)}" }
end
log_ok(response) click to toggle source
# File lib/logical_model/safe_log.rb, line 33
def log_ok(response)
  self.logger.info { "LogicalModel Log: #{response.code} #{mask_api_key(response.effective_url)} in #{response.time}s" }
  self.logger.debug { "LogicalModel Log RESPONSE: #{safe_body(response.body)}" }
end
logger() click to toggle source
# File lib/logical_model/safe_log.rb, line 49
def logger
  unless @logger
    @logger = Logger.new(self.log_path || "log/logical_model.log")
    if defined?(Rails)
      @logger.level = Rails.logger.level
    else
      @logger.level = Logger::DEBUG
    end
  end
  @logger
end
mask_api_key(str) click to toggle source

Filters api_key @return [String]

# File lib/logical_model/safe_log.rb, line 106
def mask_api_key(str)
  if use_api_key && str
    str = str.gsub(api_key,SECRET_PLACEHOLDER)
  end
  str
end
mask_sensitive_attributes(parsed_response) click to toggle source
# File lib/logical_model/safe_log.rb, line 87
def mask_sensitive_attributes(parsed_response)
  if parsed_response.is_a?(Hash)
    parsed_response.each do |k,v|
      if sensitive_attributes.include?(k.to_sym)
        parsed_response[k] = SECRET_PLACEHOLDER
      else
        parsed_response[k] = mask_sensitive_attributes(v)
      end
    end
  elsif parsed_response.is_a?(Array)
    parsed_response.map! do |v|
      mask_sensitive_attributes(v)
    end
  end
  parsed_response
end
safe_body(body) click to toggle source
# File lib/logical_model/safe_log.rb, line 80
def safe_body(body)
  parsed_response = ActiveSupport::JSON.decode(body)
  mask_sensitive_attributes(parsed_response).to_json
rescue => e
  body
end
sensitive_attribute(name) click to toggle source

declares an attribute that is sensitive and should be masked in logs si no se llamó antes a attribute, lo declara @param name [Symbol] @example

class Client < LogicalModel
  sensitive_attribute :att_name
end
# File lib/logical_model/safe_log.rb, line 68
def sensitive_attribute(name)
  if attribute_keys.blank? || !attribute_keys.include?(name)
    attribute(name)
  end
  @sensitive_attributes ||= []
  @sensitive_attributes << name
end
sensitive_attributes() click to toggle source
# File lib/logical_model/safe_log.rb, line 76
def sensitive_attributes
  @sensitive_attributes || []
end