lib/kontena/cli/helpers/log_helper.rb in kontena-cli-0.16.0.pre6 vs lib/kontena/cli/helpers/log_helper.rb in kontena-cli-0.16.0.pre7

- old
+ new

@@ -1,22 +1,58 @@ module Kontena::Cli::Helpers module LogHelper + def self.included(base) + if base.respond_to?(:option) + base.option ["-t", "--tail"], :flag, "Tail (follow) logs", default: false + base.option "--lines", "LINES", "Number of lines to show from the end of the logs", default: 100 do |s| + Integer(s) + end + base.option "--since", "SINCE", "Show logs since given timestamp" + end + end + + # @return [String] + def token + @token ||= require_token + end + + def show_logs(url, query_params = { }, &block) + if tail? + stream_logs(url, query_params, &block) + else + get_logs(url, query_params, &block) + end + end + + def get_logs(url, query_params) + query_params[:limit] = lines if lines + query_params[:since] = since if since + + result = client(token).get(url, query_params) + result['logs'].each do |log| + yield log + end + end + # @param [String] url # @param [Hash] query_params def stream_logs(url, query_params) + query_params[:limit] = lines if lines + query_params[:since] = since if since + query_params[:follow] = 1 + last_seen = nil streamer = lambda do |chunk, remaining_bytes, total_bytes| log = buffered_log_json(chunk) if log yield log last_seen = log['id'] end end begin - query_params[:follow] = 1 query_params[:from] = last_seen if last_seen result = client(token).get_stream(url, streamer, query_params) rescue => exc retry if exc.cause.is_a?(EOFError) # Excon wraps the EOFerror into SocketError raise @@ -40,9 +76,16 @@ log rescue => exc @buffer << orig_chunk nil end + end + + def show_log(log) + color = color_for_container(log['name']) + prefix = "#{log['created_at']} #{log['name']}:" + + puts "#{prefix.colorize(color)} #{log['data']}" end # @param [String] container_id # @return [Symbol] def color_for_container(container_id)