Class: SemanticLogger::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/base.rb

Direct Known Subclasses

Formatters::Raw, Logger, Subscriber

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filterObject

Class name to be logged



10
11
12
# File 'lib/semantic_logger/base.rb', line 10

def filter
  @filter
end

#nameObject

Class name to be logged



10
11
12
# File 'lib/semantic_logger/base.rb', line 10

def name
  @name
end

Instance Method Details

#backtrace(thread: Thread.current, level: :warn, message: 'Backtrace:', payload: nil, metric: nil, metric_amount: 1) ⇒ Object

Log a thread backtrace



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/semantic_logger/base.rb', line 127

def backtrace(thread: Thread.current, level: :warn, message: 'Backtrace:', payload: nil, metric: nil, metric_amount: 1)
  log       = Log.new(name, level)
  backtrace =
    if thread == Thread.current
      Log.cleanse_backtrace
    else
      log.thread_name = thread.name
      log.tags        = (thread[:semantic_logger_tags] || []).clone
      log.named_tags  = (thread[:semantic_logger_named_tags] || {}).clone
      thread.backtrace
    end
  # TODO: Keep backtrace instead of transforming into a text message at this point
  # Maybe log_backtrace: true
  if backtrace
    message += "\n"
    message << backtrace.join("\n")
  end

  if log.assign(message: message, backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && should_log?(log)
    self.log(log)
  end
end

#fast_tag(tag, &block) ⇒ Object

Deprecated. Use SemanticLogger.tagged



219
220
221
# File 'lib/semantic_logger/base.rb', line 219

def fast_tag(tag, &block)
  SemanticLogger.fast_tag(tag, &block)
end

#levelObject

Returns the current log level if set, otherwise it returns the global default log level



33
34
35
# File 'lib/semantic_logger/base.rb', line 33

def level
  @level || SemanticLogger.default_level
end

#level=(level) ⇒ Object

Set the logging level for this logger

Note: This level is only for this particular instance. It does not override

the log level in any logging instance or the default log level
SemanticLogger.default_level

Must be one of the values in SemanticLogger::LEVELS, or nil if this logger instance should use the global default level



20
21
22
23
24
25
26
27
28
29
# File 'lib/semantic_logger/base.rb', line 20

def level=(level)
  if level.nil?
    # Use the global default level for this logger
    @level_index = nil
    @level       = nil
  else
    @level_index = SemanticLogger.level_to_index(level)
    @level       = SemanticLogger.send(:index_to_level, @level_index)
  end
end

#measure(level, message, params = {}, &block) ⇒ Object Also known as: benchmark

Dynamically supply the log level with every measurement call



114
115
116
117
118
119
120
121
# File 'lib/semantic_logger/base.rb', line 114

def measure(level, message, params = {}, &block)
  index = SemanticLogger.level_to_index(level)
  if level_index <= index
    measure_internal(level, index, message, params, &block)
  else
    block.call(params) if block
  end
end

#payloadObject

:nodoc:



230
231
232
233
# File 'lib/semantic_logger/base.rb', line 230

def payload
  warn '#payload is deprecated, use SemanticLogger.named_tags'
  SemanticLogger.named_tags
end

#pop_tags(quantity = 1) ⇒ Object

:nodoc:



209
210
211
# File 'lib/semantic_logger/base.rb', line 209

def pop_tags(quantity = 1)
  SemanticLogger.pop_tags(quantity)
end

#push_tags(*tags) ⇒ Object

Returns the list of tags pushed after flattening them out and removing blanks

Note:

  • This method is slow since it needs to flatten the tags and remove empty elements to support Rails 4.

  • For better performance with clean tags, use SemanticLogger.push_tags



202
203
204
205
206
# File 'lib/semantic_logger/base.rb', line 202

def push_tags(*tags)
  # Need to flatten and reject empties to support calls from Rails 4
  new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
  SemanticLogger.push_tags(*new_tags)
end

#silence(new_level = :error, &block) ⇒ Object

:nodoc:



214
215
216
# File 'lib/semantic_logger/base.rb', line 214

def silence(new_level = :error, &block)
  SemanticLogger.silence(new_level, &block)
end

#tagged(*tags, &block) ⇒ Object Also known as: with_tags

Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active.

Returns result of block.

Tagged example:

SemanticLogger.tagged(12345, 'jack') do
  logger.debug('Hello World')
end

Named Tags (Hash) example:

SemanticLogger.tagged(tracking_number: 12345) do
  logger.debug('Hello World')
end

Notes:

  • Named tags are the recommended approach since the tag consists of a name value pair this is more useful than just a string value in the logs, or centralized logging system.

  • This method is slow when using multiple text tags since it needs to flatten the tags and remove empty elements to support Rails 4.

  • It is recommended to keep tags as a list without any empty values, or contain any child arrays. However, this api will convert:

    `logger.tagged([['first', nil], nil, ['more'], 'other'])`
    

    to:

    `logger.tagged('first', 'more', 'other')`
    
  • For better performance with clean tags, see SemanticLogger.tagged.



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/semantic_logger/base.rb', line 175

def tagged(*tags, &block)
  # Allow named tags to be passed into the logger
  if tags.size == 1
    tag = tags[0]
    return yield if tag.nil? || tag == ''
    return tag.is_a?(Hash) ? SemanticLogger.named_tagged(tag, &block) : SemanticLogger.fast_tag(tag.to_s, &block)
  end

  # Need to flatten and reject empties to support calls from Rails 4
  new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
  SemanticLogger.tagged(*new_tags, &block)
end

#tagsObject

:nodoc:



192
193
194
# File 'lib/semantic_logger/base.rb', line 192

def tags
  SemanticLogger.tags
end

#with_payload(payload, &block) ⇒ Object

:nodoc:



224
225
226
227
# File 'lib/semantic_logger/base.rb', line 224

def with_payload(payload, &block)
  warn '#with_payload is deprecated, use SemanticLogger.named_tagged'
  SemanticLogger.named_tagged(payload, &block)
end