Module: Clack::Log

Defined in:
lib/clack/log.rb

Overview

Styled console logging with consistent formatting.

Each method prints a message prefixed with a colored symbol. Multi-line messages are automatically aligned with a continuation bar on subsequent lines.

With guides off (+with_guide: false+ or Clack.update_settings(with_guide: false)) the level symbol stays on the first line and only the continuation rail goes away, so log.error still looks different from log.info (upstream drops the symbol as well).

Accessed via Clack.log:

Examples:

Clack.log.info("Starting build...")
Clack.log.success("Build completed!")
Clack.log.warn("Cache is stale")
Clack.log.error("Build failed")

Class Method Summary collapse

Class Method Details

.error(msg, with_guide: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Print an error message (red symbol).

Parameters:

  • msg (String)

    the message to display

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



102
103
104
# File 'lib/clack/log.rb', line 102

def error(msg, with_guide: nil, output: $stdout)
  message(msg, symbol: Colors.red(Symbols::S_ERROR), with_guide:, output:)
end

.info(msg, with_guide: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Print an informational message (blue symbol).

Parameters:

  • msg (String)

    the message to display

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



61
62
63
# File 'lib/clack/log.rb', line 61

def info(msg, with_guide: nil, output: $stdout)
  message(msg, symbol: Colors.blue(Symbols::S_INFO), with_guide:, output:)
end

.message(msg = "", symbol: nil, with_guide: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Print a message with a custom or default symbol prefix.

This is the base method used by all other log methods. Pass symbol: to customize the leading character (useful for extending with your own log levels).

Examples:

Custom symbol

Clack.log.message("Deploying...", symbol: "")

Parameters:

  • msg (String) (defaults to: "")

    the message to display

  • symbol (String, nil) (defaults to: nil)

    custom prefix symbol (default: gray bar, or nothing with guides off)

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/clack/log.rb', line 38

def message(msg = "", symbol: nil, with_guide: nil, output: $stdout)
  guide = Core::Settings.with_guide?(with_guide)
  rail = Colors.gray(Symbols::S_BAR)
  first = symbol || (guide ? rail : nil)
  first_prefix = first ? "#{first}  " : ""
  next_prefix = guide ? "#{rail}  " : ""
  lines = msg.to_s.lines

  if lines.empty?
    output.puts first.to_s
  else
    lines.each_with_index do |line, idx|
      output.puts "#{idx.zero? ? first_prefix : next_prefix}#{line.chomp}"
    end
  end
end

.step(msg, with_guide: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Print a step completion message (green submit symbol).

Parameters:

  • msg (String)

    the message to display

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



81
82
83
# File 'lib/clack/log.rb', line 81

def step(msg, with_guide: nil, output: $stdout)
  message(msg, symbol: Colors.green(Symbols::S_STEP_SUBMIT), with_guide:, output:)
end

.success(msg, with_guide: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Print a success message (green symbol).

Parameters:

  • msg (String)

    the message to display

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



71
72
73
# File 'lib/clack/log.rb', line 71

def success(msg, with_guide: nil, output: $stdout)
  message(msg, symbol: Colors.green(Symbols::S_SUCCESS), with_guide:, output:)
end

.warn(msg, with_guide: nil, output: $stdout) ⇒ void Also known as: warning

This method returns an undefined value.

Print a warning message (yellow symbol).

Parameters:

  • msg (String)

    the message to display

  • with_guide (Boolean, nil) (defaults to: nil)

    show the guide rail (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



91
92
93
# File 'lib/clack/log.rb', line 91

def warn(msg, with_guide: nil, output: $stdout)
  message(msg, symbol: Colors.yellow(Symbols::S_WARN), with_guide:, output:)
end