Module: Foghorn

Defined in:
lib/foghorn.rb

Overview

Foghorn — A readable, terminal-friendly logger for Ruby CLIs. Provides colored console output with optional dual-write to a plain-text log file.

Constant Summary collapse

VERSION =
File.read(File.join(__dir__, '..', 'VERSION')).strip
LEVELS =

Log levels (higher = more verbose)

{
  quiet: 0,
  normal: 1,
  verbose: 2,
  debug: 3
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.level ⇒ Object

Returns the value of attribute level.



23
24
25
# File 'lib/foghorn.rb', line 23

def level
  @level
end

Class Method Details

.close_log ⇒ Object



38
39
40
41
42
43
# File 'lib/foghorn.rb', line 38

def close_log
  return unless @log_file

  @log_file.close
  @log_file = nil
end

.debug(message) ⇒ Object



91
92
93
94
95
96
# File 'lib/foghorn.rb', line 91

def debug(message)
  return unless debug?

  puts Rainbow("[DEBUG] #{message}").darkgray
  log_to_file("[DEBUG] #{message}")
end

.debug? ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/foghorn.rb', line 53

def debug?
  @level == :debug
end

.enable_file_logging(label, log_dir: File.join(Dir.pwd, 'logs')) ⇒ Object

Enable file logging. Creates a timestamped log file. All messages are written to both terminal (with color) and file (plain text).



27
28
29
30
31
32
33
34
35
36
# File 'lib/foghorn.rb', line 27

def enable_file_logging(label, log_dir: File.join(Dir.pwd, 'logs'))
  FileUtils.mkdir_p(log_dir)

  timestamp = Time.now.strftime('%Y%m%d-%H%M%S')
  log_path = File.join(log_dir, "#{label}-#{timestamp}.log")
  @log_file = File.open(log_path, 'a')
  @log_file.sync = true

  info("Logging to #{log_path}")
end

.error(message) ⇒ Object



85
86
87
88
89
# File 'lib/foghorn.rb', line 85

def error(message)
  # Always show errors, even in quiet mode
  puts Rainbow(message).red
  log_to_file("[ERROR] #{message}")
end

.info(message) ⇒ Object



64
65
66
67
68
69
# File 'lib/foghorn.rb', line 64

def info(message)
  return if quiet?

  puts message
  log_to_file(message)
end

.quiet? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/foghorn.rb', line 45

def quiet?
  @level == :quiet
end

.space ⇒ Object



57
58
59
60
61
62
# File 'lib/foghorn.rb', line 57

def space
  return if quiet?

  puts ''
  log_to_file('')
end

.success(message) ⇒ Object



71
72
73
74
75
76
# File 'lib/foghorn.rb', line 71

def success(message)
  return if quiet?

  puts Rainbow(message).green
  log_to_file(message)
end

.verbose(message) ⇒ Object



98
99
100
101
102
103
# File 'lib/foghorn.rb', line 98

def verbose(message)
  return unless verbose?

  puts Rainbow(message).darkgray
  log_to_file(message)
end

.verbose? ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/foghorn.rb', line 49

def verbose?
  @level == :verbose || @level == :debug
end

.warn(message) ⇒ Object



78
79
80
81
82
83
# File 'lib/foghorn.rb', line 78

def warn(message)
  return if quiet?

  puts Rainbow(message).yellow
  log_to_file("[WARN] #{message}")
end