Class: Papertrail::Cli
- Inherits:
-
Object
- Object
- Papertrail::Cli
- Includes:
- CliHelpers
- Defined in:
- lib/papertrail/cli.rb
Constant Summary collapse
- COLORS =
[:cyan, :yellow, :green, :magenta, :red]
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#query_options ⇒ Object
readonly
Returns the value of attribute query_options.
Instance Method Summary collapse
- #colorize(event) ⇒ Object
- #display_colors? ⇒ Boolean
- #display_result(event) ⇒ Object
- #display_results(results) ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #output_raw_json(hash) ⇒ Object
- #query_time_range ⇒ Object
- #run ⇒ Object
- #usage ⇒ Object
Methods included from CliHelpers
#find_configfile, #load_configfile, #output_http_error, #parse_time, #set_min_max_time!, #symbolize_keys
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/papertrail/cli.rb', line 16 def initialize @options = { :configfile => nil, :delay => 2, :follow => false, :token => ENV['PAPERTRAIL_API_TOKEN'], :color => :program, :force_color => false, } @query_options = {} @query = nil end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
14 15 16 |
# File 'lib/papertrail/cli.rb', line 14 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/papertrail/cli.rb', line 14 def @options end |
#query_options ⇒ Object (readonly)
Returns the value of attribute query_options.
14 15 16 |
# File 'lib/papertrail/cli.rb', line 14 def @query_options end |
Instance Method Details
#colorize(event) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/papertrail/cli.rb', line 161 def colorize(event) attribs = "" if [:color] == :system || [:color] == :all attribs += event.data["hostname"] end if [:color] == :program || [:color] == :all attribs += event.data["program"] end idx = attribs.hash % 5 color = COLORS[idx] pre = "#{event.received_at.strftime('%b %d %X')} #{event.data['hostname']} #{event.data['program']}:" post = " #{event.data['message']}" pre.ansi(color) + post end |
#display_colors? ⇒ Boolean
177 178 179 180 |
# File 'lib/papertrail/cli.rb', line 177 def display_colors? [:color] != :off && ([:force_color] || (STDOUT.isatty && ENV.has_key?("TERM"))) end |
#display_result(event) ⇒ Object
182 183 184 185 |
# File 'lib/papertrail/cli.rb', line 182 def display_result(event) event_str = display_colors? ? colorize(event) : event.to_s $stdout.puts event_str end |
#display_results(results) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/papertrail/cli.rb', line 187 def display_results(results) if [:json] output_raw_json(results.data) else results.events.each do |event| display_result(event) end end $stdout.flush end |
#output_raw_json(hash) ⇒ Object
203 204 205 |
# File 'lib/papertrail/cli.rb', line 203 def output_raw_json(hash) $stdout.puts Papertrail::OkJson.encode(hash) end |
#query_time_range ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/papertrail/cli.rb', line 143 def query_time_range min_time = parse_time([:min_time]) if [:max_time] max_time = parse_time([:max_time]) end connection.each_event(@query, .merge(:min_time => min_time, :max_time => max_time)) do |event| if [:json] output_raw_json(event.data) else display_result(event) end end end |
#run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/papertrail/cli.rb', line 30 def run if configfile = find_configfile = load_configfile(configfile) .merge!() end OptionParser.new do |opts| opts. = "papertrail - command-line tail and search for Papertrail log management service" opts.version = Papertrail::VERSION opts.on("-h", "--help", "Show usage") do |v| puts opts exit end opts.on("-f", "--follow", "Continue running and printing new events (off)") do |v| [:follow] = true end opts.on("--min-time MIN", "Earliest time to search from") do |v| [:min_time] = v end opts.on("--max-time MAX", "Latest time to search from") do |v| [:max_time] = v end opts.on("-d", "--delay SECONDS", "Delay between refresh (2)") do |v| [:delay] = v.to_i end opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v| [:configfile] = File.(v) end opts.on("-g", "--group GROUP", "Group to search") do |v| [:group] = v end opts.on("-S", "--search SEARCH", "Saved search to search") do |v| [:search] = v end opts.on("-s", "--system SYSTEM", "System to search") do |v| [:system] = v end opts.on("-j", "--json", "Output raw JSON data (off)") do |v| [:json] = true end opts.on("--color [program|system|all|off]", [:program, :system, :all, :off], "Attribute(s) to colorize based on (program)") do |v| [:color] = v end opts.on("--force-color", "Force use of ANSI color characters even on non-tty outputs (off)") do |v| [:force_color] = true end opts.on("-V", "--version", "Display the version and exit") do |v| puts "papertrail version #{Papertrail::VERSION}" exit end opts.separator usage end.parse! if [:configfile] = load_configfile([:configfile]) .merge!() end unless [:token] abort 'Authentication token not found. Set config file "token" attribute or PAPERTRAIL_API_TOKEN.' end @connection = Papertrail::Connection.new() # Use HTTP Keep-Alive unless delay is too long connection.start if [:delay] < 10 if [:system] [:system_id] = connection.find_id_for_source([:system]) unless [:system_id] abort "System \"#{[:system]}\" not found" end end if [:group] [:group_id] = connection.find_id_for_group([:group]) unless [:group_id] abort "Group \"#{[:group]}\" not found" end end if [:search] search = connection.find_search([:search], [:group_id]) unless search abort "Search \"#{[:search]}\" not found" end [:group_id] ||= search['group_id'] @query = search['query'] end @query ||= ARGV.join ' ' if [:follow] search_query = Papertrail::SearchQuery.new(connection, @query, ) loop do display_results(search_query.next_results_page) sleep [:delay] end elsif [:min_time] query_time_range else set_min_max_time!(, ) search_query = Papertrail::SearchQuery.new(connection, @query, ) display_results(search_query.next_results_page) end end |
#usage ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/papertrail/cli.rb', line 214 def usage <<-EOF Usage: papertrail [-f] [--min-time time] [--max-time time] [-g group] [-S search] [-s system] [-d seconds] [-c papertrail.yml] [-j] [--color attributes] [--force-color] [--version] [--] [query] Examples: papertrail -f papertrail something papertrail 1.2.3 Failure papertrail -s ns1 "connection refused" papertrail -f "(www OR db) (nginx OR pgsql) -accepted" papertrail -f -g Production --color all "(nginx OR pgsql) -accepted" papertrail --min-time 'yesterday at noon' --max-time 'today at 4am' -g Production papertrail -- -redis More: https://github.com/papertrail/papertrail-cli https://papertrailapp.com/ EOF end |