Module: Morpheus::Cli::LogsHelper

Included in:
Apps, Audit, ContainersCommand, HealthCommand, Hosts, Instances, LogsCommand
Defined in:
lib/morpheus/cli/mixins/logs_helper.rb

Overview

Mixin for Morpheus::Cli command classes Provides common methods for fetching log records. The including class must establish @logs_interface, @containers_interface, @servers_interface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 8

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#clusters_interfaceObject



36
37
38
39
40
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 36

def clusters_interface
  # @api_client.clusters
  raise "#{self.class} has not defined @clusters_interface" if @clusters_interface.nil?
  @clusters_interface
end

#containers_interfaceObject



24
25
26
27
28
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 24

def containers_interface
  ## @api_client.containers
  raise "#{self.class} has not defined @logs_interface" if @containers_interface.nil?
  @containers_interface
end

#format_log_level(val, return_color = cyan, label_width = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 109

def format_log_level(val, return_color=cyan, label_width=nil)
  log_level = ''
  display_value = val.to_s
  if label_width
    display_value = display_value.ljust(label_width, ' ')
  end
  case val
  when 'INFO'
    log_level = "#{blue}#{bold}#{display_value}#{reset}#{return_color}"
  when 'DEBUG'
    log_level = "#{white}#{bold}#{display_value}#{reset}#{return_color}"
  when 'WARN'
    log_level = "#{yellow}#{bold}#{display_value}#{reset}#{return_color}"
  when 'ERROR'
    log_level = "#{red}#{bold}#{display_value}#{reset}#{return_color}"
  when 'FATAL'
    log_level = "#{red}#{bold}#{display_value}#{reset}#{return_color}"
  else
    log_level = val
  end
  return log_level
end

#format_log_records(log_records, options = {}, show_object = true) ⇒ Object



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
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 42

def format_log_records(log_records, options={}, show_object=true)
  if options[:table]
    return format_log_table(log_records, options, show_object)
  end
  out = ""
  table_color = options.key?(:color) ? options[:color] : cyan
  term_width = current_terminal_width()
  message_col_width = current_terminal_width() - (show_object ? 56 : 36)
  if options[:reverse]
    log_records.reverse!
  end
  log_records.each do |log_entry|
    log_level = format_log_level(log_entry['level'], table_color, 6)
    out << table_color if table_color
    # out << "[#{log_entry['ts']}] #{log_level} - #{log_entry['message'].to_s.strip}"
    out << "#{log_level} "
    out << "[#{log_entry['ts']}] "
    if show_object
      log_src = "#{log_entry['typeCode']} #{log_entry['objectId']}"
      if options[:details] || options[:all]
        # log_src = "#{log_entry['typeCode']} #{log_entry['objectId']}"
      else
        log_src = truncate_string(log_src, 20)
      end
      out << "(#{log_src}) "
    end
    log_msg = ""
    if options[:details] || options[:all]
      log_msg = log_entry['message'].to_s.strip
    else
      log_msg = truncate_string(log_entry['message'].to_s.strip.gsub(/\r?\n/, " "), message_col_width)
    end
    out << "#{log_msg}"
    out << table_color if table_color
    out << "\n"
  end
  return out
end

#format_log_table(logs, options = {}, show_object = true) ⇒ Object



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
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 81

def format_log_table(logs, options={}, show_object=true)
  out = ""
  table_color = options.key?(:color) ? options[:color] : cyan
  term_width = current_terminal_width()
  message_col_width = current_terminal_width() - (show_object ? 56 : 36)
  log_columns = [
    {"LEVEL" => lambda {|log_entry| format_log_level(log_entry['level'], table_color) } },
    {"DATE" => lambda {|log_entry| log_entry['ts'] } },
    {"SOURCE" => lambda {|log_entry| "#{log_entry['typeCode']} #{log_entry['objectId']}" } },
    {"MESSAGE" => lambda {|log_entry| 
      if options[:details] || options[:all]
        log_entry['message'].to_s.strip
      else
        truncate_string(log_entry['message'].to_s.strip.gsub(/\r?\n/, " "), message_col_width)
      end
    } }
  ]
  if show_object != true
    log_columns = log_columns.reject {|it| it.key?("SOURCE") }
  end
  # if options[:include_fields]
  #   columns = options[:include_fields]
  # end
  out << as_pretty_table(logs, log_columns, options.merge(wrap:true))
  # out << "\n"
  return out
end

#instances_interfaceObject



18
19
20
21
22
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 18

def instances_interface
  # @api_client.instances
  raise "#{self.class} has not defined @instances_interface" if @instances_interface.nil?
  @instances_interface
end

#logs_interfaceObject



12
13
14
15
16
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 12

def logs_interface
  # @api_client.logs
  raise "#{self.class} has not defined @logs_interface" if @logs_interface.nil?
  @logs_interface
end

#servers_interfaceObject



30
31
32
33
34
# File 'lib/morpheus/cli/mixins/logs_helper.rb', line 30

def servers_interface
  # @api_client.servers
  raise "#{self.class} has not defined @servers_interface" if @servers_interface.nil?
  @servers_interface
end