Class: SemanticLogger::Base

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

Direct Known Subclasses

Appender::Base, Logger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filterObject

Class name to be logged



12
13
14
# File 'lib/semantic_logger/base.rb', line 12

def filter
  @filter
end

#nameObject

Class name to be logged



12
13
14
# File 'lib/semantic_logger/base.rb', line 12

def name
  @name
end

Class Method Details

.default_levelObject

DEPRECATED See SemanticLogger.default_level



244
245
246
247
# File 'lib/semantic_logger/base.rb', line 244

def self.default_level
  warn '[DEPRECATION] SemanticLogger::Logger.default_level is deprecated.  Please use SemanticLogger.default_level instead.'
  SemanticLogger.default_level
end

.default_level=(level) ⇒ Object

DEPRECATED See SemanticLogger.default_level=



238
239
240
241
# File 'lib/semantic_logger/base.rb', line 238

def self.default_level=(level)
  warn '[DEPRECATION] SemanticLogger::Logger.default_level= is deprecated.  Please use SemanticLogger.default_level= instead.'
  SemanticLogger.default_level = level
end

Instance Method Details

#benchmark(level, message, params = {}, &block) ⇒ Object

Dynamically supply the log level with every benchmark call



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

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

#fast_tag(tag) ⇒ Object

If the tag being supplied is definitely a string then this fast tag api can be used for short lived tags



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

def fast_tag(tag)
  (Thread.current[:semantic_logger_tags] ||= []) << tag
  yield
ensure
  Thread.current[:semantic_logger_tags].pop
end

#levelObject

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



29
30
31
# File 'lib/semantic_logger/base.rb', line 29

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



22
23
24
25
# File 'lib/semantic_logger/base.rb', line 22

def level=(level)
  @level_index = SemanticLogger.level_to_index(level)
  @level       = level
end

#payloadObject

Returns [Hash] payload to be added to every log entry in the current scope on this thread. Returns nil if no payload is currently set



183
184
185
# File 'lib/semantic_logger/base.rb', line 183

def payload
  Thread.current[:semantic_logger_payload]
end

#pop_tags(quantity = 1) ⇒ Object

Remove specified number of tags from the current tag list



153
154
155
156
# File 'lib/semantic_logger/base.rb', line 153

def pop_tags(quantity=1)
  t = Thread.current[:semantic_logger_tags]
  t.pop(quantity) unless t.nil?
end

#push_tags(*tags) ⇒ Object

Add tags to the current scope Returns the list of tags pushed after flattening them out and removing blanks



144
145
146
147
148
149
150
# File 'lib/semantic_logger/base.rb', line 144

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?)
  t                                     = Thread.current[:semantic_logger_tags]
  Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_tags)
  new_tags
end

#silence(new_level = :error) ⇒ Object

Silence noisy log levels by changing the default_level within the block

This setting is thread-safe and only applies to the current thread

Any threads spawned within the block will not be affected by this setting

#silence can be used to both raise and lower the log level within the supplied block.

Example:

# Perform trace level logging within the block when the default is higher
SemanticLogger.default_level = :info

logger.debug 'this will _not_ be logged'

logger.silence(:trace) do
  logger.debug "this will be logged"
end

Parameters

new_level
  The new log level to apply within the block
  Default: :error

Example:

# Silence all logging below :error level
logger.silence do
  logger.info "this will _not_ be logged"
  logger.warn "this neither"
  logger.error "but errors will be logged"
end

Note:

#silence does not affect any loggers which have had their log level set
explicitly. I.e. That do not rely on the global default level


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

def silence(new_level = :error)
  current_index                            = Thread.current[:semantic_logger_silence]
  Thread.current[:semantic_logger_silence] = SemanticLogger.level_to_index(new_level)
  yield
ensure
  Thread.current[:semantic_logger_silence] = current_index
end

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

Add the supplied tags to the list of tags to log for this thread whilst the supplied block is active Returns nil if no tags are currently set

To support: ActiveSupport::TaggedLogging V3 and above


124
125
126
127
128
129
# File 'lib/semantic_logger/base.rb', line 124

def tagged(*tags)
  new_tags = push_tags(*tags)
  yield self
ensure
  pop_tags(new_tags.size)
end

#tagsObject

Returns a copy of the [Array] of [String] tags currently active for this thread Returns nil if no tags are set



136
137
138
139
140
# File 'lib/semantic_logger/base.rb', line 136

def tags
  # Since tags are stored on a per thread basis this list is thread-safe
  t = Thread.current[:semantic_logger_tags]
  t.nil? ? [] : t.clone
end

#with_payload(payload) ⇒ Object

Thread specific context information to be logged with every log entry

Add a payload to all log calls on This Thread within the supplied block

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

If a log call already includes a pyload, this payload will be merged with the supplied payload, with the supplied payload taking precedence

logger.with_payload(tracking_number: 12345) do
  logger.debug('Hello World', result: 'blah')
end


172
173
174
175
176
177
178
# File 'lib/semantic_logger/base.rb', line 172

def with_payload(payload)
  current_payload                          = self.payload
  Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload
  yield
ensure
  Thread.current[:semantic_logger_payload] = current_payload
end