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
-
.level ⇒ Object
Returns the value of attribute level.
Class Method Summary collapse
- .close_log ⇒ Object
- .debug(message) ⇒ Object
- .debug? ⇒ Boolean
-
.enable_file_logging(label, log_dir: File.join(Dir.pwd, 'logs')) ⇒ Object
Enable file logging.
- .error(message) ⇒ Object
- .info(message) ⇒ Object
- .quiet? ⇒ Boolean
- .space ⇒ Object
- .success(message) ⇒ Object
- .verbose(message) ⇒ Object
- .verbose? ⇒ Boolean
- .warn(message) ⇒ Object
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() return unless debug? puts Rainbow("[DEBUG] #{}").darkgray log_to_file("[DEBUG] #{}") end |
.debug? ⇒ 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) = Time.now.strftime('%Y%m%d-%H%M%S') log_path = File.join(log_dir, "#{label}-#{}.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() # Always show errors, even in quiet mode puts Rainbow().red log_to_file("[ERROR] #{}") end |
.info(message) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/foghorn.rb', line 64 def info() return if quiet? puts log_to_file() end |
.quiet? ⇒ 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() return if quiet? puts Rainbow().green log_to_file() end |
.verbose(message) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/foghorn.rb', line 98 def verbose() return unless verbose? puts Rainbow().darkgray log_to_file() end |
.verbose? ⇒ 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() return if quiet? puts Rainbow().yellow log_to_file("[WARN] #{}") end |