Class: SemanticLogger::Base
- Inherits:
-
Object
- Object
- SemanticLogger::Base
- Defined in:
- lib/semantic_logger/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filter ⇒ Object
Class name to be logged.
-
#name ⇒ Object
Class name to be logged.
Class Method Summary collapse
-
.default_level ⇒ Object
DEPRECATED See SemanticLogger.default_level.
-
.default_level=(level) ⇒ Object
DEPRECATED See SemanticLogger.default_level=.
Instance Method Summary collapse
-
#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.
-
#level ⇒ Object
Returns the current log level if set, otherwise it returns the global default log level.
-
#level=(level) ⇒ Object
Set the logging level for this logger.
-
#measure(level, message, params = {}, &block) ⇒ Object
(also: #benchmark)
Dynamically supply the log level with every measurement call.
-
#payload ⇒ Object
Returns [Hash] payload to be added to every log entry in the current scope on this thread.
-
#pop_tags(quantity = 1) ⇒ Object
Remove specified number of tags from the current tag list.
-
#push_tags(*tags) ⇒ Object
Add tags to the current scope Returns the list of tags pushed after flattening them out and removing blanks.
-
#silence(new_level = :error) ⇒ Object
Silence noisy log levels by changing the default_level within the block.
-
#tagged(*tags) ⇒ Object
(also: #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.
-
#tags ⇒ Object
Returns a copy of the [Array] of [String] tags currently active for this thread Returns nil if no tags are set.
-
#with_payload(payload) ⇒ Object
Thread specific context information to be logged with every log entry.
Instance Attribute Details
#filter ⇒ Object
Class name to be logged
12 13 14 |
# File 'lib/semantic_logger/base.rb', line 12 def filter @filter end |
#name ⇒ Object
Class name to be logged
12 13 14 |
# File 'lib/semantic_logger/base.rb', line 12 def name @name end |
Class Method Details
.default_level ⇒ Object
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 |
#level ⇒ Object
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, , params = {}, &block) index = SemanticLogger.level_to_index(level) if level_index <= index measure_internal(level, index, , params, &block) else block.call(params) if block end end |
#payload ⇒ Object
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 (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 * # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) t = Thread.current[:semantic_logger_tags] Thread.current[:semantic_logger_tags] = t.nil? ? : t.concat() 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:
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(*) = (*) yield self ensure (.size) end |
#tags ⇒ Object
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 # 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 |