Class: Belt::CLI::LogsCommand

Inherits:
Object
  • Object
show all
Includes:
AppDetection
Defined in:
lib/belt/cli/logs_command.rb

Constant Summary collapse

COLORS =
{
  red: "\e[0;31m",
  green: "\e[0;32m",
  yellow: "\e[0;33m",
  blue: "\e[0;34m",
  magenta: "\e[0;35m",
  cyan: "\e[0;36m",
  gray: "\e[0;90m",
  bold: "\e[1m",
  dim: "\e[2m",
  reset: "\e[0m"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AppDetection

#detect_app_name, #detect_environments, #detect_namespace, #find_contracts_file_path, #find_routes_file_path, #find_schema_file_path, #s3_safe_name

Constructor Details

#initialize(args) ⇒ LogsCommand

Returns a new instance of LogsCommand.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/belt/cli/logs_command.rb', line 63

def initialize(args)
  @lambda_name = nil
  @env = nil
  @follow = false
  @since = '5m'
  @level = 'INFO'
  @error_mode = false
  @raw = false
  @color = $stdout.tty?
  parse_args(args)
end

Class Method Details

.run(args) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/belt/cli/logs_command.rb', line 24

def self.run(args)
  if args.include?('--help') || args.include?('-h')
    puts usage
    exit 0
  end

  new(args).run
end

.usageObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/belt/cli/logs_command.rb', line 33

def self.usage
  <<~USAGE
    Usage: belt logs [lambda] [options]

    View Lambda function logs. Without arguments, tails all Lambdas for the current environment.

    Arguments:
      lambda              Lambda function short name (e.g., api, worker). Optional — shows all if omitted.

    Options:
      -e, --env ENV       Environment (default: BELT_ENV or first detected)
      -f, --follow        Follow logs in real-time
      -s, --since PERIOD  Time range (default: 5m). Examples: 5m, 30m, 1h, 2h
      -l, --level LEVEL   Minimum log level: DEBUG, INFO, WARN, ERROR (default: INFO)
      --error             Show only the most recent error and exit
      --raw               Show raw JSON logs without formatting
      --no-color          Disable colorized output
      -h, --help          Show this help

    Examples:
      belt logs                    # Last 5m of all lambdas (current env)
      belt logs api                # Last 5m of api lambda
      belt logs api -f             # Follow api lambda logs
      belt logs -e prod            # Last 5m of all lambdas in prod
      belt logs api -s 30m         # Last 30 minutes
      belt logs --error            # Show most recent error across all lambdas
      belt logs api --error        # Show most recent error for api lambda
  USAGE
end

Instance Method Details

#runObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/belt/cli/logs_command.rb', line 75

def run
  @env ||= detect_environment
  abort 'Error: Cannot determine environment. Pass -e ENV or set BELT_ENV.' unless @env

  @app_name = detect_app_name
  abort 'Error: Cannot determine app name.' unless @app_name

  if @lambda_name
    tail_single(@lambda_name)
  else
    tail_all
  end
end