class Heroku::Command::Logs

display logs for an app

Constants

COLORS
COLOR_CODES

Public Instance Methods

cron() click to toggle source

logs:cron

DEPRECATED: display cron logs from legacy logging

# File lib/heroku/command/logs.rb, line 51
def cron
  display heroku.cron_logs(app)
end
drains() click to toggle source

logs:drains

DEPRECATED: use `heroku drains`

# File lib/heroku/command/logs.rb, line 59
def drains
  output_with_bang "The logs:drain command has been deprecated. Please use drains"
  usage = Heroku::Command::Help.usage_for_command("drains")
  puts usage
end
index() click to toggle source

logs

display recent log output

-n, --num NUM # the number of lines to display -p, --ps PS # only display logs from the given process -s, --source SOURCE # only display logs from the given source -t, --tail # continually stream logs

# File lib/heroku/command/logs.rb, line 18
def index
  opts = []
  opts << "tail=1"                                 if options[:tail]
  opts << "num=#{options[:num]}"                   if options[:num]
  opts << "ps=#{URI.encode(options[:ps])}"         if options[:ps]
  opts << "source=#{URI.encode(options[:source])}" if options[:source]

  @assigned_colors = {}
  @line_start = true
  @token = nil

  $stdout.sync = true
  heroku.read_logs(app, opts) do |chunk|
    unless chunk.empty?
      if STDOUT.isatty && ENV.has_key?("TERM")
        display(colorize(chunk))
      else
        display(chunk)
      end
    end
  end
rescue Errno::EPIPE
rescue Interrupt => interrupt
  if STDOUT.isatty && ENV.has_key?("TERM")
    display("\e[0m")
  end
  raise(interrupt)
end

Protected Instance Methods

colorize(chunk) click to toggle source
# File lib/heroku/command/logs.rb, line 76
def colorize(chunk)
  lines = []
  chunk.split("\n").map do |line|
    if parsed_line = parse_log(line)
      header, identifier, body = parsed_line
      @assigned_colors[identifier] ||= COLORS[@assigned_colors.size % COLORS.size]
      lines << [
        "\e[#{COLOR_CODES[@assigned_colors[identifier]]}m",
        header,
        "\e[0m",
        body,
      ].join("")
    elsif not line.empty?
      lines << line
    end
  end
  lines.join("\n")
end
parse_log(log) click to toggle source
# File lib/heroku/command/logs.rb, line 95
def parse_log(log)
  return unless parsed = log.match(%r^(.*\[(\w+)([\d\.]+)?\]:)(.*)?$/)
  [1, 2, 4].map { |i| parsed[i] }
end