Class: SemanticLogger::Formatters::Raw

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

Direct Known Subclasses

Json, SyslogCee

Instance Attribute Summary collapse

Attributes inherited from Base

#filter

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(time_format: :none, log_host: true, log_application: true, time_key: :time, precision: PRECISION) ⇒ 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, log_host: true, log_application: true, time_key: :time, precision: PRECISION)
  @time_key = time_key
  super(time_format: time_format, log_host: log_host, log_application: log_application, precision: precision)
end

Instance Attribute Details

#hashObject

Fields are added by populating this hash.



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

def hash
  @hash
end

#logObject

Fields are added by populating this hash.



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

def log
  @log
end

#loggerObject

Fields are added by populating this hash.



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

def logger
  @logger
end

#time_keyObject

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

#applicationObject

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



102
103
104
105
106
107
108
109
# File 'lib/semantic_logger/formatters/raw.rb', line 102

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

  host; application; time; level; process_info; duration; tags; named_tags; name; message; payload; exception; metric
  hash
end

#durationObject

Duration



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

def duration
  return unless log.duration

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

#exceptionObject

Exception



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/semantic_logger/formatters/raw.rb', line 81

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

#hostObject

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

#levelObject

Log level



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

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

#messageObject

Log message



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

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

#metricObject

Metric



96
97
98
99
# File 'lib/semantic_logger/formatters/raw.rb', line 96

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

#nameObject

Class / app name



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

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

#named_tagsObject

Named Tags



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

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

#payloadObject

Payload



76
77
78
# File 'lib/semantic_logger/formatters/raw.rb', line 76

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

#process_infoObject

Process info



36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_logger/formatters/raw.rb', line 36

def process_info
  hash[:pid]    = $$
  hash[:thread] = log.thread_name

  file, line = log.file_name_and_line
  return unless file

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

#tagsObject

Tags



48
49
50
# File 'lib/semantic_logger/formatters/raw.rb', line 48

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

#timeObject

Date & time



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

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