Module: GreenHat::Shell::Log

Defined in:
lib/greenhat/shell/log.rb

Overview

Logs

Class Method Summary collapse

Class Method Details

.auto_complete(list, word) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/greenhat/shell/log.rb', line 6

def self.auto_complete(list, word)
  # Argument Parsing
  files, flags, _args = Args.parse(list)

  # Don't try to autocomplete anything else
  return nil unless word =~ /^-/

  # Clean Up
  word.delete!('-')
  matches = FieldHelper.filter_flags(word)

  if matches.count == 1
    "--#{matches.first}"
  elsif matches.count > 1
    puts "#{'Filter Options:'.pastel(:bright_blue)} #{matches.join(' ').pastel(:bright_green)}"
    "--#{Cli.common_substr(matches)}"
  # -----------------------------------
  # TODO: Fix Icky Double Nesting
  elsif files.count.nonzero?
    matches = FieldHelper.fields_find(files, word, flags)

    return nil if matches.nil?

    if matches.count == 1
      "--#{matches.first}"
    elsif matches.count > 1
      puts "#{'Field Options:'.pastel(:bright_blue)} #{matches.join(' ').pastel(:bright_green)}"
      "--#{Cli.common_substr(matches.map(&:to_s))}"
    elsif FieldHelper.field_auto_complete?(word)
      FieldHelper.field_auto_complete(word, files, flags)
    end
    # -----------------------------------
  end
end

.default(raw_list) ⇒ Object

Filter (See Filter Help)



86
87
88
# File 'lib/greenhat/shell/log.rb', line 86

def self.default(raw_list)
  filter(raw_list)
end

.examplesObject

rubocop:disable Layout/LineLength TODO: Add a lot more examples



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/greenhat/shell/log.rb', line 135

def self.examples
  puts 'Find `done` job for sidekiq, sort by duration, only duration, and show longest first'.pastel(:bright_green)
  puts 'log filter sidekiq/current --job_status=done --sort=duration_s,db_duration_s --slice=duration_s,db_duration_s --reverse'
  puts

  puts 'Find 500s only show exceptions'.pastel(:bright_green)
  puts 'log filter --status=500 --slice=exception.message gitlab-rails/production_json.log'
  puts

  puts 'Show unique sidekiq queue namespaces. Exclude Specifics'.pastel(:bright_green)
  puts 'filter sidekiq/current --slice=queue_namespace --uniq=queue_namespace --queue_namespace!=jira_connect --queue_namespace!=hashed_storage'
  puts

  puts 'Show user,ip from API logs where `meta.user` field is present '.pastel(:bright_green)
  puts 'gitlab-rails/api_json.log --slice=meta.user,meta.remote_ip --exists=meta.user'
  puts

  puts 'Count/% occurences for both user and remote ip fields'.pastel(:bright_green)
  puts 'gitlab-rails/api_json.log --stats=meta.user,meta.remote_ip --exists=meta.user'
  puts
end

.filter(raw) ⇒ Object



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
# File 'lib/greenhat/shell/log.rb', line 90

def self.filter(raw)
  # Print Helper
  if raw == ['help']
    filter_help
    return true
  end

  # Argument Parsing
  files, flags, args = Args.parse(raw)

  # Prepare Log List
  files = ShellHelper.prepare_list(files, ShellHelper::Log.list, flags)

  results = ShellHelper.filter_start(files, flags, args)

  # Skip and Print Total if set
  if flags[:total]
    ShellHelper.total_count(results)
    return true
  end

  # Skip and Print Total if set
  if flags[:fields]
    ShellHelper.fields_print(results)
    return true
  end

  # Check Search Results
  if results.instance_of?(Hash) && results.values.flatten.empty?
    puts 'No results'.pastel(:red)
  else
    # This causes the key 'colorized' output to also be included
    ShellHelper.show(results.to_a.compact.flatten, flags)
  end

  # log filter --path='cloud/gitlab-automation' --path='/pull' --all
  # log filter --project=thingy --other_filter=asdf *
rescue StandardError => e
  LogBot.fatal('Filter', message: e.message)
  ap e.backtrace
end

.filter_helpObject



65
66
67
# File 'lib/greenhat/shell/log.rb', line 65

def self.filter_help
  ShellHelper::Filter.help
end

.helpObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/greenhat/shell/log.rb', line 41

def self.help
  puts "\u2500".pastel(:cyan) * 20
  puts "#{'Logs'.pastel(:yellow)} find stuff"
  puts "\u2500".pastel(:cyan) * 20

  puts 'Command Summary'.pastel(:blue)
  puts '  filter'.pastel(:green)
  puts "    Primary way for log searching within greenhat. See #{'filter_help'.pastel(:blue)}"
  puts '    Time, round, slice/except, and/or, stats, uniq, sort'
  puts

  puts '  show'.pastel(:green)
  puts '    Just print selected logs'
  puts

  puts '  search'.pastel(:green)
  puts "    General full text by file searching. See #{'search_help'.pastel(:blue)}"
  puts

  puts ShellHelper::List.help

  puts "See #{'examples'.pastel(:bright_blue)} for query examples"
end

.ls(args = []) ⇒ Object



69
70
71
# File 'lib/greenhat/shell/log.rb', line 69

def self.ls(args = [])
  ShellHelper::List.list(args, ShellHelper::Log.list)
end

.search(raw) ⇒ Object

rubocop:enable Layout/LineLength

Search (Full Text / String Search)



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/greenhat/shell/log.rb', line 161

def self.search(raw)
  # Extract Args
  files_list, flags, args = Args.parse(raw)

  # Prepare Log List
  files = ShellHelper.prepare_list(files_list)

  results = ShellHelper.search_start(files, flags, args)

  # Skip and Print Total if set
  if flags[:total]
    ShellHelper.total_count(results)
    return true
  end

  # Check Search Results
  if results.values.flatten.empty?
    puts 'No results'.pastel(:red)
  else
    # This causes the key 'colorized' output to also be included
    ShellHelper.show(results.to_a.compact.flatten, flags)
  end
rescue StandardError => e
  LogBot.fatal('Search', message: e.message)
  ap e.backtrace
end

.search_helpObject

rubocop:disable Metrics/MethodLength



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/greenhat/shell/log.rb', line 196

def self.search_help
  puts "\u2500".pastel(:cyan) * 20
  puts 'Log Search'.pastel(:yellow)
  puts "\u2500".pastel(:cyan) * 20

  puts 'Search will do a full line include or exclude text search'

  puts 'Options'.pastel(:blue)
  puts '--text'.pastel(:green)
  puts '  Primary parameter for searching. Include or ! to exclude'
  puts '  Ex: --text=BuildHooksWorker --text!=start sidekiq/current'
  puts

  puts '--total'.pastel(:green)
  puts '  Print only total count of matching entries'
  puts

  puts '--slice'.pastel(:green)
  puts '  Extract specific fields from entries (slice multiple with comma)'
  puts '  Ex: --slice=path or --slice=path,params'
  puts

  puts '--except'.pastel(:green)
  puts '  Exclude specific fields (except multiple with comma)'
  puts '  Ex: --except=params --except=params,path'
  puts

  puts '--archive'.pastel(:green)
  puts '  Limit to specific archive name (inclusive). Matching SOS tar.gz name'
  puts '  Ex: --archive=dev-gitlab_20210622154626, --archive=202106,202107'
  puts

  puts '--limit'.pastel(:green)
  puts '  Limit total number of results. Default to half total screen size'
  puts '  Ex: --limit; --limit=10'
  puts

  puts 'Search specific logs'.pastel(:blue)
  puts '  Any non dash parameters will be the log list to search from'
  puts "  Ex: log filter --path=api sidekiq/current (hint: use  `#{'ls'.pastel(:yellow)}` for log names)"
  puts

  puts 'Example Queries'.pastel(:blue)
  puts 'log search --text=BuildHooksWorker --text!=start sidekiq/current --total'
  puts 'log search --text=BuildHooksWorker --text!=start --slice=enqueued_at sidekiq/current'
  puts
end

.show(raw = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/greenhat/shell/log.rb', line 73

def self.show(raw = {})
  # Extract Args
  files_list, flags, _args = Args.parse(raw)

  # Collect Files
  files = ShellHelper.files(files_list, Thing.all, flags)

  ShellHelper.show files.map(&:data).flatten
end