lib/sapience/base.rb in sapience-2.11 vs lib/sapience/base.rb in sapience-2.12

- old
+ new

@@ -1,11 +1,11 @@ # frozen_string_literal: true module Sapience # rubocop:disable ClassLength class Base # Class name to be logged - attr_accessor :name, :filter + attr_accessor :name, :filter, :log_hooks include Sapience::LogMethods # Set the logging level for this logger # # Note: This level is only for this particular instance. It does not override @@ -172,19 +172,20 @@ # 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 # rubocop:disable AbcSize, PerceivedComplexity, CyclomaticComplexity, LineLength - def initialize(klass, level = nil, filter = nil) + def initialize(klass, level = nil, filter = nil, log_hooks = []) # Support filtering all messages to this logger using a Regular Expression # or Proc fail ArgumentError, ":filter must be a Regexp or Proc" unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc) @filter = filter.is_a?(Regexp) ? filter.freeze : filter @name = klass if klass.is_a?(String) @name ||= klass.name if klass.respond_to?(:name) @name ||= klass.class.name + @log_hooks = log_hooks if level.nil? # Allow the global default level to determine this loggers log level @level_index = nil @level = nil @@ -269,9 +270,12 @@ return false if duration <= min_duration log.duration = duration end log.payload = payload unless payload.empty? end + + log_hooks.each { |h| h.call(log) } + self.log(log) if include_message?(log) end # rubocop:enable AbcSize, PerceivedComplexity, CyclomaticComplexity, LineLength SELF_PATTERN = File.join("lib", "sapience")