Class: SemanticLogger::Formatters::Raw

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

Direct Known Subclasses

Ecs, Json, Logfmt, NewRelicLogs, OpenTelemetry, SyslogCee

Instance Attribute Summary collapse

Attributes inherited from Base

#filter, #instance_named_tags, #instance_tags

Instance Method Summary collapse

Methods inherited from Base

#backtrace, #fast_tag, #level=, #log, #measure, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #with_level

Constructor Details

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

By default Raw formatter does not reformat the time



9
10
11
12
# File 'lib/semantic_logger/formatters/raw.rb', line 9

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

Instance Attribute Details

#hash ⇒ Object

Fields are added by populating this hash.



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

def hash
  @hash
end

#time_key ⇒ Object

Fields are added by populating this hash.



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

def time_key
  @time_key
end

Instance Method Details

#application ⇒ Object

Application name



20
21
22
# File 'lib/semantic_logger/formatters/raw.rb', line 20

def application
  hash[:application] = logger.application if log_application && logger&.application
end

#call(log, logger) ⇒ Object

Returns log messages in Hash format



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/semantic_logger/formatters/raw.rb', line 115

def call(log, logger)
  self.hash   = {}
  self.log    = log
  self.logger = logger

  host
  application
  environment
  time
  level
  pid
  thread_name
  file_name_and_line
  duration
  tags
  named_tags
  name
  message
  payload
  exception
  metric

  hash
end

#duration ⇒ Object

Duration



70
71
72
73
74
75
# File 'lib/semantic_logger/formatters/raw.rb', line 70

def duration
  return unless log.duration

  hash[:duration_ms] = log.duration
  hash[:duration]    = log.duration_human
end

#environment ⇒ Object

Environment



25
26
27
# File 'lib/semantic_logger/formatters/raw.rb', line 25

def environment
  hash[:environment] = logger.environment if log_environment && logger&.environment
end

#exception ⇒ Object

Exception



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/semantic_logger/formatters/raw.rb', line 93

def exception
  return unless log.exception

  root = hash
  log.each_exception do |exception, i|
    name       = i.zero? ? :exception : :cause
    root[name] = {
      name:        exception.class.name,
      message:     exception.message,
      stack_trace: exception.backtrace
    }
    root = root[name]
  end
end

#file_name_and_line ⇒ Object

Ruby file name and line number that logged the message.



51
52
53
54
55
56
57
# File 'lib/semantic_logger/formatters/raw.rb', line 51

def file_name_and_line
  file, line = log.file_name_and_line
  return unless file

  hash[:file] = file
  hash[:line] = line.to_i
end

#host ⇒ Object

Host name



15
16
17
# File 'lib/semantic_logger/formatters/raw.rb', line 15

def host
  hash[:host] = logger.host if log_host && logger.host
end

#level ⇒ Object

Log level



35
36
37
38
# File 'lib/semantic_logger/formatters/raw.rb', line 35

def level
  hash[:level]       = log.level
  hash[:level_index] = log.level_index
end

#message ⇒ Object

Log message



83
84
85
# File 'lib/semantic_logger/formatters/raw.rb', line 83

def message
  hash[:message] = log.cleansed_message if log.message
end

#metric ⇒ Object

Metric



109
110
111
112
# File 'lib/semantic_logger/formatters/raw.rb', line 109

def metric
  hash[:metric]        = log.metric if log.metric
  hash[:metric_amount] = log.metric_amount if log.metric_amount
end

#name ⇒ Object

Class / app name



78
79
80
# File 'lib/semantic_logger/formatters/raw.rb', line 78

def name
  hash[:name] = log.name
end

#named_tags ⇒ Object

Named Tags



65
66
67
# File 'lib/semantic_logger/formatters/raw.rb', line 65

def named_tags
  hash[:named_tags] = log.named_tags if log.named_tags && !log.named_tags.empty?
end

#payload ⇒ Object

Payload



88
89
90
# File 'lib/semantic_logger/formatters/raw.rb', line 88

def payload
  hash[:payload] = log.payload if log.payload.respond_to?(:empty?) && !log.payload.empty?
end

#pid ⇒ Object

Process ID



41
42
43
# File 'lib/semantic_logger/formatters/raw.rb', line 41

def pid
  hash[:pid] = super
end

#tags ⇒ Object

Tags



60
61
62
# File 'lib/semantic_logger/formatters/raw.rb', line 60

def tags
  hash[:tags] = log.tags if log.tags && !log.tags.empty?
end

#thread_name ⇒ Object

Name of the thread that logged the message.



46
47
48
# File 'lib/semantic_logger/formatters/raw.rb', line 46

def thread_name
  hash[:thread] = log.thread_name
end

#time ⇒ Object

Date & time



30
31
32
# File 'lib/semantic_logger/formatters/raw.rb', line 30

def time
  hash[time_key] = format_time(log.time)
end