Module: Sapience
- Defined in:
- lib/sapience/logger.rb,
lib/sapience/log.rb,
lib/sapience/base.rb,
lib/sapience/grape.rb,
lib/sapience/rails.rb,
lib/sapience/version.rb,
lib/sapience/loggable.rb,
lib/sapience/sapience.rb,
lib/sapience/subscriber.rb,
lib/sapience/ansi_colors.rb,
lib/sapience/appender/file.rb,
lib/sapience/config_loader.rb,
lib/sapience/configuration.rb,
lib/sapience/formatters/raw.rb,
lib/sapience/appender/sentry.rb,
lib/sapience/formatters/base.rb,
lib/sapience/formatters/json.rb,
lib/sapience/appender/datadog.rb,
lib/sapience/appender/wrapper.rb,
lib/sapience/formatters/color.rb,
lib/sapience/formatters/default.rb,
lib/sapience/configuration/grape.rb,
lib/sapience/concerns/compatibility.rb,
lib/sapience/extensions/grape/timings.rb,
lib/sapience/extensions/grape/middleware/logging.rb
Overview
:nodoc:
Defined Under Namespace
Modules: AnsiColors, Appender, Concerns, ConfigLoader, Extensions, Formatters, Loggable Classes: Base, Configuration, Grape, Log, Logger, Rails, Subscriber
Constant Summary collapse
- VERSION =
"0.1.3"
- LEVELS =
Logging levels in order of most detailed to most severe
[:trace, :debug, :info, :warn, :error, :fatal]
Class Method Summary collapse
-
.[](klass) ⇒ Object
Return a logger for the supplied class or class_name.
-
.add_appender(appender, options = {}, _deprecated_level = nil, &_block) ⇒ Object
Add a new logging appender as a new destination for all log messages emitted from Sapience.
-
.appenders ⇒ Object
Returns [Sapience::Subscriber] a copy of the list of active appenders for debugging etc.
-
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails.
-
.close ⇒ Object
Close and flush all appenders.
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .constantize(class_name) ⇒ Object
- .constantize_symbol(symbol, namespace = "Sapience::Appender") ⇒ Object
- .environment ⇒ Object
-
.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.
-
.flush ⇒ Object
Wait until all queued log messages have been written and flush all active appenders.
- .logger ⇒ Object
- .logger=(logger) ⇒ Object
- .metrix ⇒ Object
- .metrix=(metrix) ⇒ Object
-
.named_tags(tag) ⇒ Object
Add the supplied named tags to the list of tags to log for this thread whilst the supplied block is active.
-
.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.
-
.remove_appender(appender) ⇒ Object
Remove an existing appender Currently only supports appender instances TODO: Make it possible to remove appenders by type Maybe create a concurrent collection that allows this by inheriting from concurrent array.
-
.remove_appenders(appenders = @@appenders) ⇒ Object
Remove specific appenders or all existing.
-
.reopen ⇒ Object
After forking an active process call Sapience.reopen to re-open any open file handles etc to resources.
- .reset! ⇒ Object
- .reset_appenders! ⇒ Object
-
.silence(new_level = :error) ⇒ Object
Silence noisy log levels by changing the default_level within the block.
-
.tagged(*tags) ⇒ Object
Add the supplied tags to the list of tags to log for this thread whilst the supplied block is active.
-
.tags ⇒ Object
Returns a copy of the [Array] of [String] tags currently active for this thread Returns nil if no tags are set.
Class Method Details
.[](klass) ⇒ Object
Return a logger for the supplied class or class_name
73 74 75 |
# File 'lib/sapience/sapience.rb', line 73 def self.[](klass) Sapience::Logger.new(klass) end |
.add_appender(appender, options = {}, _deprecated_level = nil, &_block) ⇒ Object
Add a new logging appender as a new destination for all log messages emitted from Sapience
Appenders will be written to in the order that they are added
If a block is supplied then it will be used to customize the format of the messages sent to that appender. See Sapience::Logger.new for more information on custom formatters
Parameters
file_name: [String]
File name to write log messages to.
Or,
io: [IO]
An IO Stream to log to.
For example STDOUT, STDERR, etc.
Or,
appender: [Symbol|Sapience::Subscriber]
A symbol identifying the appender to create.
For example:
:bugsnag, :elasticsearch, :graylog, :http, :mongodb, :new_relic, :splunk_http, :syslog, :wrapper
Or,
An instance of an appender derived from Sapience::Subscriber
For example:
Sapience::Appender::Http.new(url: 'http://localhost:8088/path')
Or,
logger: [Logger|Log4r]
An instance of a Logger or a Log4r logger.
level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: Sapience.config.default_level
formatter: [Symbol|Object|Proc]
Any of the following symbol values: :default, :color, :json
Or,
An instance of a class that implements #call
Or,
A Proc to be used to format the output from this appender
Default: :default
filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
The Proc must return true or false.
Examples:
# Send all logging output to Standard Out (Screen)
Sapience.add_appender(:file, io: STDOUT)
# Send all logging output to a file
Sapience.add_appender(:file, file_name: 'logfile.log')
# Send all logging output to a file and only :info and above to standard output
Sapience.add_appender(:file, file_name: 'logfile.log')
Sapience.add_appender(:file, io: STDOUT, level: :info)
Log to log4r, Logger, etc.:
# Send logging output to an existing logger
require 'logger'
require 'sapience'
# Built-in Ruby logger
log = Logger.new(STDOUT)
log.level = Logger::DEBUG
Sapience.config.default_level = :debug
Sapience.add_appender(:wrapper, logger: log)
logger = Sapience['Example']
logger.info "Hello World"
logger.debug("Login time", user: 'Joe', duration: 100, ip_address: '127.0.0.1')
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sapience/sapience.rb', line 155 def self.add_appender(appender, = {}, _deprecated_level = nil, &_block) fail ArgumentError, "options should be a hash" unless .is_a?(Hash) .deep_symbolize_keys! appender_class = constantize_symbol(appender) appender = appender_class.new() @@appenders << appender # Start appender thread if it is not already running Sapience::Logger.start_appender_thread Sapience.logger = appender if appender.is_a?(Sapience::Appender::File) Sapience.metrix = appender if appender.is_a?(Sapience::Appender::Datadog) appender end |
.appenders ⇒ Object
Returns [Sapience::Subscriber] a copy of the list of active appenders for debugging etc. Use Sapience.add_appender and Sapience.remove_appender to manipulate the active appenders list
189 190 191 |
# File 'lib/sapience/sapience.rb', line 189 def self.appenders @@appenders.clone end |
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails
349 350 351 352 353 354 355 356 357 |
# File 'lib/sapience/sapience.rb', line 349 def self.camelize(term) # rubocop:disable AbcSize string = term.to_s string = string.sub(/^[a-z\d]*/) { |match| match.capitalize } string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do "#{Regexp.last_match[1]}#{inflections.acronyms[Regexp.last_match[2]] || Regexp.last_match[2].capitalize}" end string.gsub!("/".freeze, "::".freeze) string end |
.close ⇒ Object
Close and flush all appenders
216 217 218 |
# File 'lib/sapience/sapience.rb', line 216 def self.close Sapience::Logger.close end |
.config ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sapience/sapience.rb', line 29 def self.config @@config ||= begin config = ConfigLoader.load_from_file env = config[environment] unless env puts "Missing configuration for #{environment} environment. Sapience is going to use default configuration" env = config.fetch("default") end Configuration.new(env) end end |
.configure {|config| ... } ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sapience/sapience.rb', line 60 def self.configure yield config if block_given? config.appenders.each do |appender| appender.each do |name, | add_appender(name, ) end end config end |
.constantize(class_name) ⇒ Object
339 340 341 342 343 344 345 346 |
# File 'lib/sapience/sapience.rb', line 339 def self.constantize(class_name) return class_name unless class_name.is_a?(String) if RUBY_VERSION.to_i >= 2 Object.const_get(class_name) else class_name.split("::").inject(Object) { |o, name| o.const_get(name) } # rubocop:disable SingleLineBlockParams end end |
.constantize_symbol(symbol, namespace = "Sapience::Appender") ⇒ Object
332 333 334 335 336 337 |
# File 'lib/sapience/sapience.rb', line 332 def self.constantize_symbol(symbol, namespace = "Sapience::Appender") klass = "#{namespace}::#{camelize(symbol.to_s)}" constantize(klass) rescue NameError raise(ArgumentError, "Could not convert symbol: #{symbol} to a class in: #{namespace}. Looking for: #{klass}") end |
.environment ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/sapience/sapience.rb', line 52 def self.environment ENV.fetch("RAILS_ENV") do ENV.fetch("RACK_ENV") do ::Rails.env if defined?(::Rails) && ::Rails.respond_to?(:env) end 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
232 233 234 235 236 237 |
# File 'lib/sapience/sapience.rb', line 232 def self.fast_tag(tag) (Thread.current[:sapience_tags] ||= []) << tag yield ensure Thread.current[:sapience_tags].pop end |
.flush ⇒ Object
Wait until all queued log messages have been written and flush all active appenders
211 212 213 |
# File 'lib/sapience/sapience.rb', line 211 def self.flush Sapience::Logger.flush end |
.logger ⇒ Object
205 206 207 |
# File 'lib/sapience/sapience.rb', line 205 def self.logger @@logger ||= Sapience::Logger.logger end |
.logger=(logger) ⇒ Object
201 202 203 |
# File 'lib/sapience/sapience.rb', line 201 def self.logger=(logger) @@logger = Sapience::Logger.logger = logger end |
.metrix ⇒ Object
197 198 199 |
# File 'lib/sapience/sapience.rb', line 197 def self.metrix @@metrix ||= Sapience.add_appender(:datadog) end |
.metrix=(metrix) ⇒ Object
193 194 195 |
# File 'lib/sapience/sapience.rb', line 193 def self.metrix=(metrix) @@metrix = metrix end |
.named_tags(tag) ⇒ Object
Add the supplied named tags to the list of tags to log for this thread whilst the supplied block is active.
Returns result of block
Example:
245 246 247 248 249 250 |
# File 'lib/sapience/sapience.rb', line 245 def self.(tag) (Thread.current[:sapience_tags] ||= []) << tag yield ensure Thread.current[:sapience_tags].pop end |
.pop_tags(quantity = 1) ⇒ Object
Remove specified number of tags from the current tag list
281 282 283 284 |
# File 'lib/sapience/sapience.rb', line 281 def self.(quantity = 1) t = Thread.current[:sapience_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
272 273 274 275 276 277 278 |
# File 'lib/sapience/sapience.rb', line 272 def self.(*) # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) t = Thread.current[:sapience_tags] Thread.current[:sapience_tags] = t.nil? ? : t.concat() end |
.remove_appender(appender) ⇒ Object
Remove an existing appender Currently only supports appender instances TODO: Make it possible to remove appenders by type Maybe create a concurrent collection that allows this by inheriting from concurrent array.
174 175 176 |
# File 'lib/sapience/sapience.rb', line 174 def self.remove_appender(appender) @@appenders.delete(appender) end |
.remove_appenders(appenders = @@appenders) ⇒ Object
Remove specific appenders or all existing
179 180 181 182 183 |
# File 'lib/sapience/sapience.rb', line 179 def self.remove_appenders(appenders = @@appenders) appenders.each do |appender| remove_appender(appender) end end |
.reopen ⇒ Object
After forking an active process call Sapience.reopen to re-open any open file handles etc to resources
Note: Only appenders that implement the reopen method will be called
224 225 226 227 228 |
# File 'lib/sapience/sapience.rb', line 224 def self.reopen @@appenders.each { |appender| appender.reopen if appender.respond_to?(:reopen) } # After a fork the appender thread is not running, start it if it is not running Sapience::Logger.start_appender_thread end |
.reset! ⇒ Object
41 42 43 44 45 46 |
# File 'lib/sapience/sapience.rb', line 41 def self.reset! @@config = nil @@logger = nil @@metrix = nil reset_appenders! end |
.reset_appenders! ⇒ Object
48 49 50 |
# File 'lib/sapience/sapience.rb', line 48 def self.reset_appenders! @@appenders = Concurrent::Array.new 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
Sapience.config.default_level = :info
logger.debug 'this will _not_ be logged'
Sapience.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 for this thread below :error level
Sapience.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
322 323 324 325 326 327 328 |
# File 'lib/sapience/sapience.rb', line 322 def self.silence(new_level = :error) current_index = Thread.current[:sapience_silence] Thread.current[:sapience_silence] = Sapience.config.level_to_index(new_level) yield ensure Thread.current[:sapience_silence] = current_index end |
.tagged(*tags) ⇒ Object
Add the supplied tags to the list of tags to log for this thread whilst the supplied block is active. Returns result of block
255 256 257 258 259 260 |
# File 'lib/sapience/sapience.rb', line 255 def self.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
264 265 266 267 268 |
# File 'lib/sapience/sapience.rb', line 264 def self. # Since tags are stored on a per thread basis this list is thread-safe t = Thread.current[:sapience_tags] t.nil? ? [] : t.clone end |