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:, env_debug_name: 'DEBUG') ⇒ Logger

Constructor for CLI::Kit::Logger

: (debug_log_file: String, ?env_debug_name: String) -> void

Parameters:

  • debug_log_file (String)

    path to the file where debug logs should be stored



17
18
19
20
21
# File 'lib/cli/kit/logger.rb', line 17

def initialize(debug_log_file:, env_debug_name: 'DEBUG')
  FileUtils.mkpath(File.dirname(debug_log_file))
  @debug_logger = ::Logger.new(debug_log_file, MAX_NUM_LOGS, MAX_LOG_SIZE)
  @env_debug_name = env_debug_name
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

: (String msg) -> void

Parameters:

  • msg (String)

    the message to log



72
73
74
75
# File 'lib/cli/kit/logger.rb', line 72

def debug(msg)
  $stdout.puts CLI::UI.fmt(msg) if 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

: (String msg, ?debug: bool) -> void

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

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



51
52
53
54
# File 'lib/cli/kit/logger.rb', line 51

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

: (String msg, ?debug: bool) -> void

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

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



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

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

: (String msg, ?debug: bool) -> void

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

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



29
30
31
32
# File 'lib/cli/kit/logger.rb', line 29

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

: (String msg, ?debug: bool) -> void

Parameters:

  • msg (String)

    the message to log

  • debug (Boolean) (defaults to: true)

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



40
41
42
43
# File 'lib/cli/kit/logger.rb', line 40

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