Class: CovLoupe::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/cov_loupe/config/logger.rb

Overview

Custom logger that gracefully handles logging failures.

Log targets:

- File path (default: ./cov_loupe.log)
- 'stderr' for stream output
- ':off' to disable logging entirely
- nil for default file logging

'stdout' is never a valid target because it would corrupt command output.

If the primary log target fails (e.g., permission denied), the logger falls back to writing to COV-LOUPE-LOG-ERROR.log and emits a one-time stderr warning in CLI mode. The safe_log method never raises, making it safe for use in rescue blocks.

Constant Summary collapse

DEFAULT_LOG_FILESPEC =
'./cov_loupe.log'
FALLBACK_LOG_FILE =
'COV-LOUPE-LOG-ERROR.log'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, mode: :library) ⇒ Logger

Returns a new instance of Logger.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cov_loupe/config/logger.rb', line 27

def initialize(target:, mode: :library)
  if target.to_s.strip.downcase == 'stdout'
    raise ConfigurationError,
      'Logging to stdout is not permitted because it corrupts command output. ' \
      "Use 'stderr', a file path, or ':off' to disable logging."
  end

  @mode = mode
  @target = target
  @init_error = nil
  @stderr_warning_emitted = false
  @disabled = logging_disabled?(target)

  return if @disabled

  begin
    @logger = build_logger(target)
  rescue => e
    @init_error = e
  end
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



25
26
27
# File 'lib/cov_loupe/config/logger.rb', line 25

def target
  @target
end

Instance Method Details

#error(msg) ⇒ Object



61
62
63
64
65
# File 'lib/cov_loupe/config/logger.rb', line 61

def error(msg)
  return if @disabled

  log_with_level(:error, msg)
end

#info(msg) ⇒ Object



49
50
51
52
53
# File 'lib/cov_loupe/config/logger.rb', line 49

def info(msg)
  return if @disabled

  log_with_level(:info, msg)
end

#safe_log(msg) ⇒ Object

Safe logging that never raises - use when logging should not interrupt execution.



68
69
70
71
72
73
74
# File 'lib/cov_loupe/config/logger.rb', line 68

def safe_log(msg)
  return if @disabled

  info(msg)
rescue
  # Silently ignore all logging failures
end

#warn(msg) ⇒ Object



55
56
57
58
59
# File 'lib/cov_loupe/config/logger.rb', line 55

def warn(msg)
  return if @disabled

  log_with_level(:warn, msg)
end