Class: SemanticLoggerEcsAddon::Formatters::Raw

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger_ecs_addon/formatters/raw.rb

Direct Known Subclasses

Json

Instance Attribute Summary

Attributes inherited from Base

#formatted_payload, #hash, #log_labels, #time_key

Instance Method Summary collapse

Methods inherited from Base

#apm_agent_present_and_running?, #application, #calculated_log_level, #each_exception, #environment, #error_log?, #exception, #exception_hash, #file_name_and_line, #format_payload, #host, #initialize_rack_keys, #inner_exception, #labels, #message, #named_tags, #rack_extract, #rack_metrics, #rack_request, #rack_response, #request, #response, #tags, #time

Constructor Details

#initialize(time_format: :none, time_key: :@timestamp, **args) ⇒ Raw

Returns a new instance of Raw.



6
7
8
9
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 6

def initialize time_format: :none, time_key: :@timestamp, **args
  @time_key = time_key
  super(time_format: time_format, time_key: @time_key, **args)
end

Instance Method Details

#baseObject



11
12
13
14
15
16
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 11

def base
  time
  labels
  message
  tags
end

#call(log, logger) ⇒ Object

Returns log messages in Hash format



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 101

def call log, logger
  self.hash   = {}
  self.log    = log
  self.logger = logger
  format_payload

  base
  ecs
  error
  event
  http
  ecs_log
  ecs_process
  ecs_service
  ecs_source
  ecs_tracing
  ecs_url
  ecs_user
  extras

  hash.compact
end

#ecsObject



18
19
20
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 18

def ecs
  hash[:"ecs.version"] = "1.10"
end

#ecs_logObject



47
48
49
50
51
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 47

def ecs_log
  hash[:"log.level"] = calculated_log_level
  hash[:"log.logger"] = log.name
  file_name_and_line
end

#ecs_processObject



53
54
55
56
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 53

def ecs_process
  hash[:"process.thread.name"] = log.thread_name
  hash[:"process.pid"] = pid
end

#ecs_serviceObject



58
59
60
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 58

def ecs_service
  hash[:"service.name"] = logger.application
end

#ecs_sourceObject



62
63
64
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 62

def ecs_source
  hash[:"source.ip"] = formatted_payload.delete :remote_ip
end

#ecs_tracingObject



66
67
68
69
70
71
72
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 66

def ecs_tracing
  return unless apm_agent_present_and_running?

  hash[:"transaction.id"] = ElasticAPM.current_transaction&.id
  hash[:"trace.id"] = ElasticAPM.current_transaction&.trace_id
  hash[:"span.id"] = ElasticAPM.current_span&.id
end

#ecs_urlObject



74
75
76
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 74

def ecs_url
  hash[:"url.path"] = formatted_payload.dig :request, :path
end

#ecs_userObject



78
79
80
81
82
83
84
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 78

def ecs_user
  hash[:"user.email"] = formatted_payload.dig :user, :email
  hash[:"user.full_name"] = formatted_payload.dig :user, :full_name
  hash[:"user.id"] = formatted_payload.dig :user, :id
  hash[:"user.name"] = formatted_payload.dig :user, :name
  hash[:"user.domain"] = formatted_payload.dig :user, :type
end

#errorObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 22

def error
  root = hash
  each_exception exception do |e, i|
    if i.zero?
      root.merge! exception_hash e
    else
      root[:"error.cause"] = exception_hash e
      root = root[:"error.cause"]
    end
  end
rescue SystemStackError => _error
  root.merge! exception_hash exception
end

#eventObject



36
37
38
39
40
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 36

def event
  hash[:"event.dataset"] = "#{logger.application}.log"
  hash[:"event.duration"] = (log.duration || 0) * 1000000
  hash[:"event.outcome"] = error_log? ? "failure" : "success"
end

#extrasObject



94
95
96
97
98
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 94

def extras
  return unless formatted_payload.respond_to?(:empty?) && !formatted_payload.empty? && formatted_payload.respond_to?(:has_key?)

  hash.merge!(safe_jsonify(formatted_payload.except(:request, :response, :user)))
end

#httpObject



42
43
44
45
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 42

def http
  request
  response
end

#safe_jsonify(hash) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 86

def safe_jsonify hash
  hash.each do |k, v|
    hash[k] = v.respond_to?(:to_json) ? v.to_json : v.inspect
  rescue SystemStackError => _error
    hash[k] = v.inspect
  end
end