module GreenHat::Shell::Log

Logs

Public Class Methods

default(raw_list) click to toggle source

Filter (See Filter Help)

# File lib/greenhat/shell/log.rb, line 51
def self.default(raw_list)
  filter(raw_list)
end
examples() click to toggle source

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

# File lib/greenhat/shell/log.rb, line 94
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) click to toggle source
# File lib/greenhat/shell/log.rb, line 55
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

  # 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_help() click to toggle source
# File lib/greenhat/shell/log.rb, line 30
def self.filter_help
  ShellHelper::Filter.help
end
help() click to toggle source
# File lib/greenhat/shell/log.rb, line 6
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 = []) click to toggle source
# File lib/greenhat/shell/log.rb, line 34
def self.ls(args = [])
  ShellHelper::List.list(args, ShellHelper::Log.list)
end
search_help() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/greenhat/shell/log.rb, line 155
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 = {}) click to toggle source
# File lib/greenhat/shell/log.rb, line 38
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