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



248
249
250
251
# File 'lib/semantic_logger/base.rb', line 248

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=



242
243
244
245
# File 'lib/semantic_logger/base.rb', line 242

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

#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



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

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       = SemanticLogger.send(:index_to_level, @level_index)
end

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

Dynamically supply the log level with every measurement call



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

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

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



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

def payload
  Thread.current[:semantic_logger_payload]
end

#pop_tags(quantity = 1) ⇒ Object

Remove specified number of tags from the current tag list



163
164
165
166
# File 'lib/semantic_logger/base.rb', line 163

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



154
155
156
157
158
159
160
# File 'lib/semantic_logger/base.rb', line 154

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


233
234
235
236
237
238
239
# File 'lib/semantic_logger/base.rb', line 233

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


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

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



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

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


182
183
184
185
186
187
188
# File 'lib/semantic_logger/base.rb', line 182

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