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
Returns a new instance of Log.
41 42 43 44 45 46 47 48 49 |
# File 'lib/collins/cli/log.rb', line 41 def initialize @parsed = false @validated = false @running = false @collins = nil @logs_seen = [] = OPTIONS_DEFAULTS.clone @search_opts = SEARCH_DEFAULTS.clone end |
Instance Method Details
#parse!(argv = ARGV) ⇒ Object
51 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 |
# File 'lib/collins/cli/log.rb', line 51 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| [: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| [:tags] = v} opts.on('-f','--follow',"Poll for logs every #{@options[:poll_wait]} seconds") {|v| [:follow] = true} opts.on('-s','--severity SEVERITY[,...]',Array,"Log severities to return (Defaults to all). Use !SEVERITY to exclude one.") {|v| [: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| [:config] = v} opts.on('-h','--help',"Help") {puts opts ; exit 0} opts.separator "" opts.separator "Severities:\n \#{Collins::Api::Logging::Severity.to_a.map{|s| s.colorize(@options[:sev_colors][s])}.join(\", \")}\n\nExamples:\n Show last 20 logs for an asset\n\#{PROG_NAME} -t 001234\n Show last 100 logs for an asset\n\#{PROG_NAME} -t 001234 -n100\n Show last 10 logs for 2 assets that are ERROR severity\n\#{PROG_NAME} -t 001234,001235 -n10 -sERROR\n Show last 10 logs all assets that are not note or informational severity\n\#{PROG_NAME} -a -n10 -s'!informational,!note'\n Show last 10 logs for all web nodes that are provisioned having verification in the message\ncollins find -S provisioned -n webnode\\$ | \#{PROG_NAME} -n10 -s debug | grep -i verification\n Tail logs for all assets that are provisioning\ncollins find -Sprovisioning,provisioned | \#{PROG_NAME} -f\n" end.parse!(argv) @parsed = true self end |
#run! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/collins/cli/log.rb', line 107 def run! raise "Options not yet validated with #validate!" unless @validated raise "Already running" if @running begin @running = true all_logs = grab_logs @logs_seen = all_logs.map(&:ID).to_set output_logs(all_logs) while [:follow] sleep [: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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/collins/cli/log.rb', line 87 def validate! raise "Options not yet parsed with #parse!" unless @parsed unless [: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] = [:severities].join(';') if [:tags].empty? and not [: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 [: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 [:tags].empty? and not [:show_all] @validated = true self end |