class Informed::Informant
Informs on method calls!
Attributes
What, if any, additional data to log when informing on a method.
If this hash has `{ result: true }`, done messages will be logged with the result of the method.
If the hash has `{ values: [:a_method_name, :a_keyword_arg_name] }` the values of the method `a_method_name` and the passed in keyword argument `a_keyword_arg_name` will be logged as well.
@example
inform_on :a_method, level: :debug, also_log: { result: true, values: [:some_method, :some_keyword_arg] } # This will log the result of `a_method` at the debug level, as well as # the value of `some_method` and the passed in keyword argumented # `some_keyword_arg` inform_on :another_method, level: :info # This will merely log that another_method was called at the info # level, with no additional context.
@return [Hash]
Which level to log informed method calls at @return [:debug,:info,:warn,:error,:fatal,:unknown]
The name of the method being informed on. This is included in all log messages. @return [Symbol]
Public Class Methods
@param method [Symbol] See {#method} @param also_log
[Hash] See {#also_log} @param level [Hash] See {#level}
# File lib/informed.rb, line 140 def initialize(method:, also_log:, level:) self.level = level self.method = method # Somehow, nils are slipping in here... self.also_log = also_log || {} end
Public Instance Methods
@param informee [Object] The object that had the method called on it. @param keyword_arguments [Hash] The keyword arguments passed into the method being informed on. These may
be logged if specified in the :values array in {#also_log}.
@return the result of the informed upon method.
# File lib/informed.rb, line 152 def inform_on(logger:, arguments:, keyword_arguments:, informee:, block: nil) method_context = { keyword_arguments: keyword_arguments, method: method, also_log: also_log, informee: informee } log(logger: logger, type: StartingMessage, method_context: method_context) result = if arguments.empty? && keyword_arguments.empty? informee.send(:"unwatched_#{method}", &block) elsif arguments.empty? && !keyword_arguments.empty? informee.send(:"unwatched_#{method}", **keyword_arguments, &block) elsif !arguments.empty? && keyword_arguments.empty? informee.send(:"unwatched_#{method}", *arguments, &block) elsif !arguments.empty? && !keyword_arguments.empty? informee.send(:"unwatched_#{method}", *arguments, **keyword_arguments, &block) end log(logger: logger, type: DoneMessage, method_context: method_context.merge(result: result)) result end
Private Instance Methods
# File lib/informed.rb, line 168 def log(logger:, type:, method_context:) logger.send(level, type.new(**method_context).to_h) end
The default data to include in a logged message. This will always include the {#method_name}, but may also include additional context from when the method was executed based upon the values of {#also_log}. @see also_log
@return [Hash]
# File lib/informed.rb, line 243 def message end