Class: Sapience::Appender::Wrapper
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- Sapience::Appender::Wrapper
- Defined in:
- lib/sapience/appender/wrapper.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Attributes inherited from Subscriber
#application, #formatter, #host
Attributes inherited from Base
Instance Method Summary collapse
-
#flush ⇒ Object
Flush all pending logs to disk.
-
#initialize(options, &block) ⇒ Wrapper
constructor
Forward all logging calls to the supplied logging instance.
-
#log(log) ⇒ Object
Pass log calls to the underlying Rails, log4j or Ruby logger trace entries are mapped to debug since :trace is not supported by the Ruby or Rails Loggers.
Methods inherited from Subscriber
#close, #default_formatter, #level, #name
Methods included from Descendants
Methods inherited from Base
#application, #default_formatter, #fast_tag, #host, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Constructor Details
#initialize(options, &block) ⇒ Wrapper
Forward all logging calls to the supplied logging instance.
Parameters
logger: [Object]
Instance of an existing logger conforming to the Ruby Logger methods.
level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: Sapience.config.default_level
formatter: [Object|Proc]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: Use the built-in formatter (See: #call)
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.
Ruby Logger
require 'logger'
require 'sapience'
ruby_logger = Logger.new(STDOUT)
Sapience.add_appender(:wrapper, logger: ruby_logger)
logger = Sapience['test']
logger.info('Hello World', some: :payload)
rubocop:disable LineLength
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sapience/appender/wrapper.rb', line 42 def initialize(, &block) # Backward compatibility = { logger: } unless .is_a?(Hash) = .dup @logger = .delete(:logger) # Check if the custom appender responds to all the log levels. For example Ruby ::Logger if (does_not_implement = LEVELS[1..-1].find { |i| !@logger.respond_to?(i) }) fail ArgumentError, "Supplied logger does not implement:#{does_not_implement}. It must implement all of #{LEVELS[1..-1].inspect}" end fail ArgumentError, "Sapience::Appender::Wrapper missing mandatory parameter :logger" unless @logger super(, &block) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/sapience/appender/wrapper.rb', line 8 def logger @logger end |
Instance Method Details
#flush ⇒ Object
Flush all pending logs to disk.
Waits for all sent documents to be writted to disk
69 70 71 |
# File 'lib/sapience/appender/wrapper.rb', line 69 def flush @logger.flush if @logger.respond_to?(:flush) end |
#log(log) ⇒ Object
Pass log calls to the underlying Rails, log4j or Ruby logger
trace entries are mapped to debug since :trace is not supported by the
Ruby or Rails Loggers
61 62 63 64 65 |
# File 'lib/sapience/appender/wrapper.rb', line 61 def log(log) return false unless should_log?(log) @logger.send(log.level == :trace ? :debug : log.level, formatter.call(log, self)) true end |