Class: SemanticLogger::Base

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

Direct Known Subclasses

Appender::Base, Logger

Defined Under Namespace

Classes: Log

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



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

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=



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

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

#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



174
175
176
# File 'lib/semantic_logger/base.rb', line 174

def payload
  Thread.current[:semantic_logger_payload]
end

#pop_tags(quantity = 1) ⇒ Object

Remove specified number of tags from the current tag list



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

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



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

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

#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


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

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



127
128
129
130
131
# File 'lib/semantic_logger/base.rb', line 127

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


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

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