Class: CLI::Kit::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/kit/logger.rb

Constant Summary collapse

MAX_LOG_SIZE =

5MB

5 * 1024 * 1000
MAX_NUM_LOGS =
10

Instance Method Summary collapse

Constructor Details

#initialize(debug_log_file:) ⇒ Logger

Constructor for CLI::Kit::Logger

Parameters:

  • debug_log_file (String)

    path to the file where debug logs should be stored



13
14
15
16
# File 'lib/cli/kit/logger.rb', line 13

def initialize(debug_log_file:)
  FileUtils.mkpath(File.dirname(debug_log_file))
  @debug_logger = ::Logger.new(debug_log_file, MAX_NUM_LOGS, MAX_LOG_SIZE)
end

Instance Method Details

#debug(msg) ⇒ Object

Similar to Logger#debug, however will not output to STDOUT unless DEBUG env var is set Logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id

Parameters:

  • msg (String)

    the message to log



62
63
64
65
# File 'lib/cli/kit/logger.rb', line 62

def debug(msg)
  $stdout.puts CLI::UI.fmt(msg) if ENV['DEBUG']
  @debug_logger.debug(format_debug(msg))
end

#error(msg, debug: true) ⇒ Object

Functionally equivalent to Logger#error Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

    determines if the debug logger will receive the log (default true)



43
44
45
46
# File 'lib/cli/kit/logger.rb', line 43

def error(msg, debug: true)
  $stderr.puts CLI::UI.fmt("{{red:#{msg}}}")
  @debug_logger.error(format_debug(msg)) if debug
end

#fatal(msg, debug: true) ⇒ Object

Functionally equivalent to Logger#fatal Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

    determines if the debug logger will receive the log (default true)



53
54
55
56
# File 'lib/cli/kit/logger.rb', line 53

def fatal(msg, debug: true)
  $stderr.puts CLI::UI.fmt("{{red:{{bold:Fatal:}} #{msg}}}")
  @debug_logger.fatal(format_debug(msg)) if debug
end

#info(msg, debug: true) ⇒ Object

Functionally equivalent to Logger#info Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

    determines if the debug logger will receive the log (default true)



23
24
25
26
# File 'lib/cli/kit/logger.rb', line 23

def info(msg, debug: true)
  $stdout.puts CLI::UI.fmt(msg)
  @debug_logger.info(format_debug(msg)) if debug
end

#warn(msg, debug: true) ⇒ Object

Functionally equivalent to Logger#warn Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

    determines if the debug logger will receive the log (default true)



33
34
35
36
# File 'lib/cli/kit/logger.rb', line 33

def warn(msg, debug: true)
  $stdout.puts CLI::UI.fmt("{{yellow:#{msg}}}")
  @debug_logger.warn(format_debug(msg)) if debug
end