Class: UnifiedLogger::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/unified_logger/logger.rb

Constant Summary collapse

LOGS =
Concurrent::ThreadLocalVar.new([])
EXTRA_LOG_FIELDS =
Concurrent::ThreadLocalVar.new({})
SILENCED =
Concurrent::ThreadLocalVar.new(false)
SEVERITY_LEVELS =
{
  debug:   ::Logger::DEBUG,
  info:    ::Logger::INFO,
  note:    ::Logger::Severity::NOTE,
  warn:    ::Logger::WARN,
  error:   ::Logger::ERROR,
  fatal:   ::Logger::FATAL,
  unknown: ::Logger::UNKNOWN
}.freeze
SEVERITY_MAP =
SEVERITY_LEVELS.invert.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logging_device = $stdout) ⇒ Logger

Returns a new instance of Logger.



17
18
19
20
21
# File 'lib/unified_logger/logger.rb', line 17

def initialize(logging_device = $stdout, *)
  super
  @logging_device = logging_device
  self.formatter = proc {}
end

Class Method Details

.add(hash) ⇒ Object



82
83
84
85
86
# File 'lib/unified_logger/logger.rb', line 82

def add(hash)
  return if SILENCED.value

  EXTRA_LOG_FIELDS.value = EXTRA_LOG_FIELDS.value.merge(hash)
end

.extra_log_fieldsObject



88
89
90
# File 'lib/unified_logger/logger.rb', line 88

def extra_log_fields
  EXTRA_LOG_FIELDS.value
end

.format(log) ⇒ Object



128
129
130
131
132
# File 'lib/unified_logger/logger.rb', line 128

def format(log)
  filtered_log = filter(log)
  formatter = UnifiedLogger.format_log_callable
  formatter.present? ? formatter.call(filtered_log) : filtered_log.to_json
end

.format_exception(exception) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/unified_logger/logger.rb', line 112

def format_exception(exception)
  if exception.is_a?(String)
    { message: exception }
  elsif exception.is_a?(Exception)
    btc = ActiveSupport::BacktraceCleaner.new
    prefix = UnifiedLogger.backtrace_root + File::SEPARATOR
    btc.add_filter { |line| line.sub(/\A#{Regexp.escape(prefix)}/, "") }
    btc.add_silencer { |line| /request_logger.rb/.match?(line) }
    { class_name: exception.class.name, message: exception.message, backtrace: btc.clean(exception.backtrace || []) }
  elsif exception.respond_to?(:to_s)
    exception.to_s
  else
    exception.class.name
  end
end

.logsObject



68
69
70
# File 'lib/unified_logger/logger.rb', line 68

def logs
  LOGS.value
end

.reset_thread_logsObject



92
93
94
95
96
# File 'lib/unified_logger/logger.rb', line 92

def reset_thread_logs
  LOGS.value = []
  EXTRA_LOG_FIELDS.value = {}
  SILENCED.value = false
end

.setup_silencing(path) ⇒ Object



76
77
78
79
80
# File 'lib/unified_logger/logger.rb', line 76

def setup_silencing(path)
  SILENCED.value = UnifiedLogger.config[:silence_paths].any? do |pattern|
    pattern.is_a?(Regexp) ? pattern.match?(path) : pattern == path
  end
end

.silenced?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/unified_logger/logger.rb', line 72

def silenced?
  SILENCED.value
end

.trim(data) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unified_logger/logger.rb', line 98

def trim(data)
  data = filter(data)
  size = data.inspect.length
  max = UnifiedLogger.config[:max_log_field_size]
  return data if size < max

  begin
    json = JSON.generate(data, quirks_mode: true)
  rescue JSON::GeneratorError, Encoding::UndefinedConversionError
    json = data.respond_to?("to_s") ? data.to_s : "unparseable object (instance of #{data.class.name})"
  end
  "#{json[0..max]}... (#{size - max} extra characters omitted)"
end

.write_log(log) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/unified_logger/logger.rb', line 134

def write_log(log)
  logger = UnifiedLogger.current_logger
  max = UnifiedLogger.config[:max_log_size]
  if log.inspect.length <= max || !log.key?(:logs)
    logger.write(format(log))
  else
    entries = log.delete(:logs)
    logger.write(format(log))
    base_fields = log.except(:request, :response)
    write_overflow_logs(base_fields, entries, max, logger)
  end
end

Instance Method Details

#<<(message) ⇒ Object



58
59
60
61
# File 'lib/unified_logger/logger.rb', line 58

def <<(message)
  add(::Logger::UNKNOWN, message.to_s.chomp)
  self
end

#add(severity, message = nil, progname = nil, &block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/unified_logger/logger.rb', line 185

def add(severity, message = nil, progname = nil, &block)
  if message.nil?
    message = block ? block.call : progname
  end

  return true if message.blank?
  return true unless severity >= level

  severity_symbol = SEVERITY_MAP[severity] || :unknown
  append_log(severity_symbol, message)
end

#debug(message = nil, &block) ⇒ Object



23
24
25
26
# File 'lib/unified_logger/logger.rb', line 23

def debug(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::DEBUG, message)
end

#error(message = nil, &block) ⇒ Object



43
44
45
46
# File 'lib/unified_logger/logger.rb', line 43

def error(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::ERROR, message)
end

#fatal(message = nil, &block) ⇒ Object



48
49
50
51
# File 'lib/unified_logger/logger.rb', line 48

def fatal(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::FATAL, message)
end

#info(message = nil, &block) ⇒ Object



28
29
30
31
# File 'lib/unified_logger/logger.rb', line 28

def info(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::INFO, message)
end

#note(message = nil, &block) ⇒ Object



33
34
35
36
# File 'lib/unified_logger/logger.rb', line 33

def note(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::Severity::NOTE, message)
end

#unknown(message = nil, &block) ⇒ Object



53
54
55
56
# File 'lib/unified_logger/logger.rb', line 53

def unknown(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::UNKNOWN, message)
end

#warn(message = nil, &block) ⇒ Object



38
39
40
41
# File 'lib/unified_logger/logger.rb', line 38

def warn(message = nil, &block)
  message = block.call if message.nil? && block
  add(::Logger::WARN, message)
end

#write(message) ⇒ Object



63
64
65
# File 'lib/unified_logger/logger.rb', line 63

def write(message)
  @logging_device.write("#{message}\n") if @logging_device.respond_to?(:write)
end