Class: Collins::CLI::Log
- Inherits:
-
Object
- Object
- Collins::CLI::Log
- Includes:
- Mixins
- Defined in:
- lib/collins/cli/log.rb
Constant Summary collapse
- LOG_LEVELS =
Collins::Api::Logging::Severity.constants.map(&:to_s)
- OPTIONS_DEFAULTS =
{ :tags => [], :show_all => false, :poll_wait => 2, :follow => false, :severities => [], :timeout => 20, :sev_colors => { 'EMERGENCY' => {:color => :red, :background => :light_blue}, 'ALERT' => {:color => :red}, 'CRITICAL' => {:color => :black, :background => :red}, 'ERROR' => {:color => :red}, 'WARNING' => {:color => :yellow}, 'NOTICE' => {}, 'INFORMATIONAL' => {:color => :green}, 'DEBUG' => {:color => :blue}, 'NOTE' => {:color => :light_cyan}, }, :config => nil }
- SEARCH_DEFAULTS =
{ :size => 20, :filter => nil, }
- PROG_NAME =
'collins log'
Constants included from Mixins
Mixins::COLORS, Mixins::ERROR, Mixins::SUCCESS
Instance Method Summary collapse
-
#initialize ⇒ Log
constructor
A new instance of Log.
- #parse!(argv = ARGV) ⇒ Object
- #run! ⇒ Object
- #validate! ⇒ Object
Methods included from Mixins
#api_call, #as_query?, #collins, #convert_to_query
Constructor Details
#initialize ⇒ Log
42 43 44 45 46 47 48 49 50 |
# File 'lib/collins/cli/log.rb', line 42 def initialize @parsed = false @validated = false @running = false @collins = nil @logs_seen = [] @options = OPTIONS_DEFAULTS.clone @search_opts = SEARCH_DEFAULTS.clone end |
Instance Method Details
#parse!(argv = ARGV) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/collins/cli/log.rb', line 52 def parse!(argv = ARGV) raise "No flags given! See --help for #{PROG_NAME} usage" if argv.empty? OptionParser.new do |opts| opts. = "Usage: #{PROG_NAME} [options]" opts.on('-a','--all',"Show logs from ALL assets") {|v| @options[:show_all] = true} opts.on('-n','--number LINES',Integer,"Show the last LINES log entries. (Default: #{@search_opts[:size]})") {|v| @search_opts[:size] = v} opts.on('-t','--tags TAGS',Array,"Tags to work on, comma separated") {|v| @options[:tags] = v} opts.on('-f','--follow',"Poll for logs every #{@options[:poll_wait]} seconds") {|v| @options[:follow] = true} opts.on('-s','--severity SEVERITY[,...]',Array,"Log severities to return (Defaults to all). Use !SEVERITY to exclude one.") {|v| @options[:severities] = v.map(&:upcase) } #opts.on('-i','--interleave',"Interleave all log entries (Default: groups by asset)") {|v| options[:interleave] = true} opts.on('-C','--config CONFIG',String,'Use specific Collins config yaml for Collins::Client') {|v| @options[:config] = v} opts.on('-h','--help',"Help") {puts opts ; exit 0} opts.separator "" opts.separator <<_EOE_ Severities: #{Collins::Api::Logging::Severity.to_a.map{|s| s.colorize(@options[:sev_colors][s])}.join(", ")} Examples: Show last 20 logs for an asset #{PROG_NAME} -t 001234 Show last 100 logs for an asset #{PROG_NAME} -t 001234 -n100 Show last 10 logs for 2 assets that are ERROR severity #{PROG_NAME} -t 001234,001235 -n10 -sERROR Show last 10 logs all assets that are not note or informational severity #{PROG_NAME} -a -n10 -s'!informational,!note' Show last 10 logs for all web nodes that are provisioned having verification in the message collins find -S provisioned -n webnode\$ | #{PROG_NAME} -n10 -s debug | grep -i verification Tail logs for all assets that are provisioning collins find -Sprovisioning,provisioned | #{PROG_NAME} -f _EOE_ end.parse!(argv) @parsed = true self end |
#run! ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/collins/cli/log.rb', line 108 def run! raise "Options not yet validated with #validate!" unless @validated raise "Already running" if @running begin @running = true # collins sends us messages that are HTML escaped all_logs = grab_logs all_logs.map! {|l| l.MESSAGE = CGI.unescapeHTML(l.MESSAGE) ; l } @logs_seen = all_logs.map(&:ID).to_set output_logs(all_logs) while @options[:follow] sleep @options[:poll_wait] logs = grab_logs new_logs = logs.reject {|l| @logs_seen.include?(l.ID)} output_logs(new_logs) @logs_seen = @logs_seen | new_logs.map(&:ID) end return true rescue Interrupt return true rescue return false ensure @running = false end end |
#validate! ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/collins/cli/log.rb', line 88 def validate! raise "Options not yet parsed with #parse!" unless @parsed unless @options[:severities].all? {|l| Collins::Api::Logging::Severity.valid?(l.tr('!','')) } raise "Log severities #{@options[:severities].join(',')} are invalid! Use one of #{LOG_LEVELS.join(', ')}" end @search_opts[:filter] = @options[:severities].join(';') if @options[:tags].empty? and not @options[:show_all] # read tags from stdin. first field on the line is the tag begin input = ARGF.readlines rescue Interrupt raise "Interrupt reading tags from ARGF" end @options[:tags] = input.map{|l| l.split(/\s+/)[0] rescue nil}.compact.uniq end raise "You need to give me some assets to display logs; see --help" if @options[:tags].empty? and not @options[:show_all] @validated = true self end |