Class: ArchUnit::Common::Logging::CheckLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/archunit/common/logging/check_logger.rb

Overview

A logger owned by one check invocation; no configuration or output state is global.

Constant Summary collapse

LEVEL_PRIORITIES =
{ debug: 0, info: 1, warn: 2, error: 3 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, clock: -> { Time.now.utc }) ⇒ CheckLogger

Returns a new instance of CheckLogger.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/archunit/common/logging/check_logger.rb', line 16

def initialize(options = nil, clock: -> { Time.now.utc })
  validate_options(options)
  raise ArgumentError, 'clock must respond to call' unless clock.respond_to?(:call)

  @options = options
  @clock = clock
  @file = nil
  @log_path = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#log_pathObject (readonly)

Returns the value of attribute log_path.



14
15
16
# File 'lib/archunit/common/logging/check_logger.rb', line 14

def log_path
  @log_path
end

Instance Method Details

#closeObject



56
57
58
59
60
61
62
# File 'lib/archunit/common/logging/check_logger.rb', line 56

def close
  @mutex.synchronize do
    @file&.close
    @file = nil
  end
  nil
end

#end_check(check_name, violation_count: nil, error: nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/archunit/common/logging/check_logger.rb', line 31

def end_check(check_name, violation_count: nil, error: nil)
  name = label(check_name, 'check_name')
  if error
    write(:error, "end check: #{name} failed with #{error.class}: #{error.message}")
  else
    write(:info, "end check: #{name} (#{Integer(violation_count)} violations)")
  end
end

#log_metric(name:, value:, subject: nil) ⇒ Object



50
51
52
53
54
# File 'lib/archunit/common/logging/check_logger.rb', line 50

def log_metric(name:, value:, subject: nil)
  metric = label(name, 'name')
  context = subject.nil? ? '' : " [#{subject}]"
  write(:debug, "log metric: #{metric}=#{value}#{context}")
end

#log_progress(message) ⇒ Object



40
41
42
# File 'lib/archunit/common/logging/check_logger.rb', line 40

def log_progress(message)
  write(:info, "log progress: #{label(message, 'message')}")
end

#log_violation(violation) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/archunit/common/logging/check_logger.rb', line 44

def log_violation(violation)
  raise ArgumentError, 'violation is required' if violation.nil?

  write(:warn, "log violation: #{violation_description(violation)}")
end

#start_check(check_name) ⇒ Object



27
28
29
# File 'lib/archunit/common/logging/check_logger.rb', line 27

def start_check(check_name)
  write(:info, "start check: #{label(check_name, 'check_name')}")
end