class SemanticLoggerEcsAddon::Formatters::Base
Attributes
formatted_payload[RW]
Fields are added by populating this hash.
hash[RW]
Fields are added by populating this hash.
log_labels[RW]
Fields are added by populating this hash.
time_key[RW]
Fields are added by populating this hash.
Public Class Methods
new(time_key: :time, **args)
click to toggle source
Parameters
time_format: [String|Symbol|nil] See Time#strftime for the format of this string. :iso_8601 Outputs an ISO8601 Formatted timestamp. :ms Output in miliseconds since epoch. nil: Returns Empty string for time ( no time is output ). Default: '%Y-%m-%d %H:%M:%S.%<precision>N' log_host: [Boolean] Whether or not to include hostname in logs Default: true precision: [Integer] How many fractional digits to log times with. Default: PRECISION (6, except on older JRuby, where 3)
Calls superclass method
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 24 def initialize time_key: :time, **args @time_key = time_key @log_application = true super(**args) end
Public Instance Methods
apm_agent_present_and_running?()
click to toggle source
ElasticAPM
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 203 def apm_agent_present_and_running? return false unless defined?(::ElasticAPM) ElasticAPM.running? end
application()
click to toggle source
Application name
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 36 def application log_labels[:application] = logger.application if logger && logger.application end
calculated_log_level()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 196 def calculated_log_level return log.level if log.level_index > 2 (formatted_payload.dig(:response, :status) || 0) >= 500 ? "error" : log.level end
each_exception(exception) { |e, depth| ... }
click to toggle source
Call the block for exception and any nested exception
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 99 def each_exception exception # With thanks to https://github.com/bugsnag/bugsnag-ruby/blob/6348306e44323eee347896843d16c690cd7c4362/lib/bugsnag/notification.rb#L81 depth = 0 exceptions = [] e = exception while !e.nil? && !exceptions.include?(e) && exceptions.length < 5 exceptions << e yield e, depth depth += 1 e = inner_exception e end end
environment()
click to toggle source
Environment
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 41 def environment if log_environment && logger && logger.environment log_labels[:environment] = logger.environment end end
error_log?()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 121 def error_log? !hash[:"error.type"].nil? end
exception()
click to toggle source
Exception
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 78 def exception return log.exception if log.exception unless log.payload.respond_to?(:empty?) && log.payload[:exception].class.ancestors.include?(StandardError) return end log.payload[:exception] end
exception_hash(exception)
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 113 def exception_hash exception { "error.type": exception.class.name, "error.message": exception.message, "error.stack_trace": BacktraceCleaner.clean(exception.backtrace) } end
file_name_and_line()
click to toggle source
Ruby file name and line number that logged the message.
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 188 def file_name_and_line file, line = log.file_name_and_line return unless file hash[:"log.origin.file.name"] = file hash[:"log.origin.file.line"] = line.to_i end
format_payload()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 166 def format_payload if log.payload.respond_to?(:empty?) && !log.payload.empty? self.formatted_payload = Utils::Hash[**log.payload] rack_extract else self.formatted_payload = {} end end
host()
click to toggle source
Host name
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 31 def host log_labels[:host] = logger.host if log_host && logger.host end
initialize_rack_keys()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 125 def initialize_rack_keys formatted_payload[:request] ||= {} formatted_payload[:response] ||= {} formatted_payload[:metrics] ||= {} end
inner_exception(exception)
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 88 def inner_exception exception if exception.respond_to?(:cause) && exception.cause exception.cause elsif exception.respond_to?(:continued_exception) && exception.continued_exception exception.continued_exception elsif exception.respond_to?(:original_exception) && exception.original_exception exception.original_exception end end
labels()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 57 def labels self.log_labels ||= {} host application environment named_tags hash[:labels] = log_labels unless log_labels.empty? end
message()
click to toggle source
Log message
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 67 def message hash[:message] = "#{log.name} -- #{log.cleansed_message}" if log.message hash[:message] = "#{log.metric} -- #{log.metric_amount}" if log.metric && log.metric_amount end
rack_extract()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 156 def rack_extract return unless formatted_payload.key? :controller initialize_rack_keys rack_request rack_response rack_metrics formatted_payload.delete :format end
rack_metrics()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 147 def rack_metrics metrics_keys = formatted_payload.keys.select do |k| k.to_s.end_with? "_runtime", "_count" end formatted_payload[:metrics].merge! formatted_payload.extract!(:allocations, *metrics_keys) formatted_payload[:metrics][:object_allocations] = formatted_payload[:metrics].delete :allocations end
rack_request()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 131 def rack_request formatted_payload[:request].merge! formatted_payload.extract!( :controller, :action, :params, :method, :path, :request_id ) formatted_payload[:request][:body] = formatted_payload[:request].delete :params end
rack_response()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 143 def rack_response formatted_payload[:response].merge! formatted_payload.extract!(:status, :status_message) end
request()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 176 def request hash[:"http.request.id"] = formatted_payload.dig :request, :request_id hash[:"http.request.body.content"] = formatted_payload.dig :request, :body hash[:"http.request.method"] = formatted_payload.dig :request, :method end
response()
click to toggle source
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 182 def response hash[:"http.response.body.content"] = formatted_payload.dig :response, :body hash[:"http.response.status_code"] = formatted_payload.dig :response, :status end
time()
click to toggle source
Date & time
# File lib/semantic_logger_ecs_addon/formatters/base.rb, line 53 def time hash[time_key] = format_time log.time.utc end