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.
Instance Method Summary collapse
-
#backtrace(thread: Thread.current, level: :warn, message: 'Backtrace:', payload: nil, metric: nil, metric_amount: nil) ⇒ Object
Log a thread backtrace.
-
#fast_tag(tag, &block) ⇒ Object
Deprecated.
-
#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.
-
#log(_log_) ⇒ Object
Write log data to underlying data storage.
-
#measure(level, message, params = {}, &block) ⇒ Object
(also: #benchmark)
Dynamically supply the log level with every measurement call.
- #named_tags ⇒ Object
-
#payload ⇒ Object
:nodoc:.
-
#pop_tags(quantity = 1) ⇒ Object
:nodoc:.
-
#push_tags(*tags) ⇒ Object
Returns the list of tags pushed after flattening them out and removing blanks.
-
#should_log?(log) ⇒ Boolean
Whether this log entry meets the criteria to be logged by this appender.
-
#silence(new_level = :error, &block) ⇒ Object
:nodoc:.
-
#tagged(*tags, &block) ⇒ Object
(also: #with_tags)
Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active.
-
#tags ⇒ Object
:nodoc:.
-
#with_payload(payload, &block) ⇒ Object
:nodoc:.
Instance Attribute Details
#filter ⇒ Object
Class name to be logged
10 11 12 |
# File 'lib/semantic_logger/base.rb', line 10 def filter @filter end |
#name ⇒ Object
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: nil) ⇒ 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/semantic_logger/base.rb', line 127 def backtrace(thread: Thread.current, level: :warn, message: 'Backtrace:', payload: nil, metric: nil, metric_amount: nil) log = Log.new(name, level) return false unless meets_log_level?(log) backtrace = if thread == Thread.current Utils.extract_backtrace else log.thread_name = thread.name log. = (thread[:semantic_logger_tags] || []).clone log. = (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 += "\n" << backtrace.join("\n") end if log.assign(message: , backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && !filtered?(log) self.log(log) else false end end |
#fast_tag(tag, &block) ⇒ Object
Deprecated. Use ‘SemanticLogger.tagged`
237 238 239 |
# File 'lib/semantic_logger/base.rb', line 237 def fast_tag(tag, &block) SemanticLogger.fast_tag(tag, &block) end |
#level ⇒ Object
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 = Levels.index(level) @level = Levels.level(@level_index) end end |
#log(_log_) ⇒ Object
Write log data to underlying data storage
254 255 256 |
# File 'lib/semantic_logger/base.rb', line 254 def log(_log_) raise NotImplementedError, 'Logging Appender must implement #log(log)' 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, , params = {}, &block) index = Levels.index(level) if level_index <= index measure_internal(level, index, , params, &block) elsif block yield(params) end end |
#named_tags ⇒ Object
210 211 212 |
# File 'lib/semantic_logger/base.rb', line 210 def SemanticLogger. end |
#payload ⇒ Object
:nodoc:
248 249 250 251 |
# File 'lib/semantic_logger/base.rb', line 248 def payload warn '#payload is deprecated, use SemanticLogger.named_tags' SemanticLogger. end |
#pop_tags(quantity = 1) ⇒ Object
:nodoc:
227 228 229 |
# File 'lib/semantic_logger/base.rb', line 227 def (quantity = 1) SemanticLogger.(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`
220 221 222 223 224 |
# File 'lib/semantic_logger/base.rb', line 220 def (*) # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) SemanticLogger.(*) end |
#should_log?(log) ⇒ Boolean
Whether this log entry meets the criteria to be logged by this appender.
259 260 261 |
# File 'lib/semantic_logger/base.rb', line 259 def should_log?(log) meets_log_level?(log) && !filtered?(log) end |
#silence(new_level = :error, &block) ⇒ Object
:nodoc:
232 233 234 |
# File 'lib/semantic_logger/base.rb', line 232 def silence(new_level = :error, &block) SemanticLogger.silence(new_level, &block) end |
#tagged(*tags, &block) ⇒ Object Also known as:
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`.
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/semantic_logger/base.rb', line 189 def tagged(*, &block) # Allow named tags to be passed into the logger if .size == 1 tag = [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 = .flatten.collect(&:to_s).reject(&:empty?) SemanticLogger.tagged(*, &block) end |
#tags ⇒ Object
:nodoc:
206 207 208 |
# File 'lib/semantic_logger/base.rb', line 206 def SemanticLogger. end |
#with_payload(payload, &block) ⇒ Object
:nodoc:
242 243 244 245 |
# File 'lib/semantic_logger/base.rb', line 242 def with_payload(payload, &block) warn '#with_payload is deprecated, use SemanticLogger.named_tagged' SemanticLogger.named_tagged(payload, &block) end |