Module: LogicalModel::SafeLog::ClassMethods
- Defined in:
- lib/logical_model/safe_log.rb
Instance Attribute Summary collapse
-
#log_path ⇒ Object
Returns the value of attribute log_path.
Instance Method Summary collapse
- #log_failed(response) ⇒ Object
- #log_ok(response) ⇒ Object
- #logger ⇒ Object
-
#mask_api_key(str) ⇒ String
Filters api_key.
- #mask_sensitive_attributes(parsed_response) ⇒ Object
- #safe_body(body) ⇒ Object
-
#sensitive_attribute(name) ⇒ Object
declares an attribute that is sensitive and should be masked in logs si no se llamó antes a attribute, lo declara.
- #sensitive_attributes ⇒ Object
Instance Attribute Details
#log_path ⇒ Object
Returns the value of attribute log_path.
27 28 29 |
# File 'lib/logical_model/safe_log.rb', line 27 def log_path @log_path end |
Instance Method Details
#log_failed(response) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/logical_model/safe_log.rb', line 38 def log_failed(response) begin = ActiveSupport::JSON.decode(response.body)["message"] rescue => e = "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) ⇒ Object
33 34 35 36 |
# 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 ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# 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) ⇒ String
Filters api_key
106 107 108 109 110 111 |
# 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) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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) ⇒ Object
80 81 82 83 84 85 |
# 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) ⇒ Object
declares an attribute that is sensitive and should be masked in logs si no se llamó antes a attribute, lo declara
68 69 70 71 72 73 74 |
# 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 ⇒ Object
76 77 78 |
# File 'lib/logical_model/safe_log.rb', line 76 def sensitive_attributes @sensitive_attributes || [] end |