class Heroku::Command::Logs

display logs for an app

Constants

COLORS
COLOR_CODES

Public Instance Methods

drains() click to toggle source

logs:drains

DEPRECATED: use `heroku drains`

# File lib/heroku/command/logs.rb, line 56
def drains
  # deprecation notice added 09/30/2011
  display("~ `heroku logs:drains` has been deprecated and replaced with `heroku drains`")
  Heroku::Command::Drains.new.index
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

Example:

$ heroku logs
2012-01-01T12:00:00+00:00 heroku[api]: Config add EXAMPLE by email@example.com
2012-01-01T12:00:01+00:00 heroku[api]: Release v1 created by email@example.com
# File lib/heroku/command/logs.rb, line 22
def index
  validate_arguments!

  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

  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 73
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 92
def parse_log(log)
  return unless parsed = log.match(/^(.*\[(\w+)([\d\.]+)?\]:)(.*)?$/)
  [1, 2, 4].map { |i| parsed[i] }
end