Module: Axn::Core::AutomaticLogging

Defined in:
lib/axn/core/automatic_logging.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OUTCOMES =

The outcomes result.outcome can report, each independently configurable.

%i[success failure exception].freeze

Class Method Summary collapse

Class Method Details

.base_level(args, overrides) ⇒ Object

The base level for unspecified outcomes (see resolve_levels).



44
45
46
47
48
# File 'lib/axn/core/automatic_logging.rb', line 44

def self.base_level(args, overrides)
  return normalize_level(args.first) unless args.empty?

  overrides.empty? ? Axn.config.log_level : nil
end

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/axn/core/automatic_logging.rb', line 9

def self.included(base)
  base.class_eval do
    extend ClassMethods

    # Per-outcome log levels; a nil value means that outcome is not logged.
    # Defaults to every outcome at the configured level (logging on by default).
    default_level = Axn.config.log_level
    class_attribute :_auto_log_levels,
                    default: OUTCOMES.each_with_object({}) { |outcome, h| h[outcome] = default_level }
  end
end

.normalize_level(value) ⇒ Object

Coerce a single level value: true → configured default, false/nil → off, a valid level → itself.



51
52
53
54
55
56
57
58
59
# File 'lib/axn/core/automatic_logging.rb', line 51

def self.normalize_level(value)
  case value
  when true then Axn.config.log_level
  when false, nil then nil
  when *Core::Logging::LEVELS then value
  else
    raise ArgumentError, "Invalid log level: #{value.inspect} (expected one of #{Core::Logging::LEVELS.join('/')})"
  end
end

.resolve_levels(args, overrides) ⇒ Object

Resolve auto_log arguments into a frozen failure:, exception: level hash.

  • A positional level is the default for any outcome not named by a keyword.
  • success:/failure:/exception: keywords override per outcome.
  • With no positional and at least one keyword, unnamed outcomes are off (so keyword-only forms log only the named outcomes).
  • A bare auto_log (no positional, no keywords) logs every outcome at the configured level.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/axn/core/automatic_logging.rb', line 28

def self.resolve_levels(args, overrides)
  raise ArgumentError, "auto_log accepts at most one positional level argument" if args.size > 1

  # Indifferent access: a string-keyed Hash (e.g. forwarded by Axn::Factory.build from
  # YAML/params) resolves the same as symbol keys.
  overrides = overrides.transform_keys(&:to_sym)
  unknown = overrides.keys - OUTCOMES
  raise ArgumentError, "auto_log got unknown outcome(s): #{unknown.join(', ')} (expected #{OUTCOMES.join('/')})" if unknown.any?

  base = base_level(args, overrides)
  OUTCOMES.each_with_object({}) do |outcome, hash|
    hash[outcome] = overrides.key?(outcome) ? normalize_level(overrides[outcome]) : base
  end.freeze
end