Class: SemanticLoggerEcsAddon::Formatters::Raw
- Inherits:
-
Base
- Object
- SemanticLogger::Formatters::Base
- Base
- SemanticLoggerEcsAddon::Formatters::Raw
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.
8
9
10
11
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 8
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
#base ⇒ Object
13
14
15
16
17
18
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 13
def base
time
labels
message
tags
end
|
#call(log, logger) ⇒ Object
Returns log messages in Hash format
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 105
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
hash.compact
end
|
#ecs ⇒ Object
20
21
22
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 20
def ecs
hash[:"ecs.version"] = "1.10"
end
|
#ecs_log ⇒ Object
49
50
51
52
53
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 49
def ecs_log
hash[:"log.level"] = calculated_log_level
hash[:"log.logger"] = log.name
file_name_and_line
end
|
#ecs_process ⇒ Object
55
56
57
58
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 55
def ecs_process
hash[:"process.thread.name"] = log.thread_name
hash[:"process.pid"] = pid
end
|
#ecs_service ⇒ Object
60
61
62
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 60
def ecs_service
hash[:"service.name"] = logger.application
end
|
#ecs_source ⇒ Object
64
65
66
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 64
def ecs_source
hash[:"source.ip"] = formatted_payload.delete :remote_ip
end
|
#ecs_tracing ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 68
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_url ⇒ Object
76
77
78
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 76
def ecs_url
hash[:"url.path"] = formatted_payload.dig :request, :path
end
|
#ecs_user ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 80
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
|
#error ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 24
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
|
#event ⇒ Object
38
39
40
41
42
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 38
def event
hash[:"event.dataset"] = "#{logger.application}.log"
hash[:"event.duration"] = (log.duration || 0) * 1000000
hash[:"event.outcome"] = error_log? ? "failure" : "success"
end
|
96
97
98
99
100
101
102
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 96
def
unless formatted_payload.respond_to?(:empty?) && !formatted_payload.empty? && formatted_payload.respond_to?(:has_key?)
return
end
hash.merge!(safe_jsonify(formatted_payload.except(:request, :response, :user)))
end
|
#http ⇒ Object
44
45
46
47
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 44
def http
request
response
end
|
#safe_jsonify(hash) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/semantic_logger_ecs_addon/formatters/raw.rb', line 88
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
|