Class: Loggr

Inherits:
Object
  • Object
show all
Defined in:
lib/loggr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Loggr

Returns a new instance of Loggr.



10
11
12
13
14
15
16
# File 'lib/loggr.rb', line 10

def initialize(config={})
  @config = config
  init_config
  if @bash_command == ""
    start
  end
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



8
9
10
# File 'lib/loggr.rb', line 8

def alias
  @alias
end

#bash_commandObject

Returns the value of attribute bash_command.



8
9
10
# File 'lib/loggr.rb', line 8

def bash_command
  @bash_command
end

#clear_eachObject

Returns the value of attribute clear_each.



8
9
10
# File 'lib/loggr.rb', line 8

def clear_each
  @clear_each
end

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/loggr.rb', line 8

def color
  @color
end

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/loggr.rb', line 8

def command
  @command
end

#conditionObject

Returns the value of attribute condition.



8
9
10
# File 'lib/loggr.rb', line 8

def condition
  @condition
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/loggr.rb', line 8

def config
  @config
end

#fetch_eachObject

Returns the value of attribute fetch_each.



8
9
10
# File 'lib/loggr.rb', line 8

def fetch_each
  @fetch_each
end

#formatObject

Returns the value of attribute format.



8
9
10
# File 'lib/loggr.rb', line 8

def format
  @format
end

#ignoreObject

Returns the value of attribute ignore.



8
9
10
# File 'lib/loggr.rb', line 8

def ignore
  @ignore
end

#languageObject

Returns the value of attribute language.



8
9
10
# File 'lib/loggr.rb', line 8

def language
  @language
end

#last_idObject

Returns the value of attribute last_id.



8
9
10
# File 'lib/loggr.rb', line 8

def last_id
  @last_id
end

#linesObject

Returns the value of attribute lines.



8
9
10
# File 'lib/loggr.rb', line 8

def lines
  @lines
end

#messagesObject

Returns the value of attribute messages.



8
9
10
# File 'lib/loggr.rb', line 8

def messages
  @messages
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/loggr.rb', line 8

def name
  @name
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/loggr.rb', line 8

def path
  @path
end

#report_eachObject

Returns the value of attribute report_each.



8
9
10
# File 'lib/loggr.rb', line 8

def report_each
  @report_each
end

#timerObject

Returns the value of attribute timer.



8
9
10
# File 'lib/loggr.rb', line 8

def timer
  @timer
end

#trello_labelsObject

Returns the value of attribute trello_labels.



8
9
10
# File 'lib/loggr.rb', line 8

def trello_labels
  @trello_labels
end

#trello_list_idObject

Returns the value of attribute trello_list_id.



8
9
10
# File 'lib/loggr.rb', line 8

def trello_list_id
  @trello_list_id
end

#trello_members_idsObject

Returns the value of attribute trello_members_ids.



8
9
10
# File 'lib/loggr.rb', line 8

def trello_members_ids
  @trello_members_ids
end

Instance Method Details

#clearObject



135
136
137
# File 'lib/loggr.rb', line 135

def clear
  init_config
end

#create_trello_card(message) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/loggr.rb', line 139

def create_trello_card(message)
  client = Trello::Client.new(
    developer_public_key: Emissary.trello_public_key,
    member_token: Emissary.trello_member_token
  )

  card = client.create(:card,
    {
      'name' => message[:lines][1],
      'desc' => "Ocurrences: #{message[:count]}\n\n```#{@language}\n#{lines_to_code(message)}```",
      'idList' => @trello_list_id,
      'labels' => @trello_labels,
      'idMembers' => @trello_members_ids,
    }
  )

  card.url
end

#fetchObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/loggr.rb', line 65

def fetch
  file = File.open(@path)
  file_lines = file.readlines.map(&:chomp)
  @ignore.each{|i| file_lines.delete(i) }

  file_lines.each_with_index do |line, i|
    if line.include? @condition
      if @messages["#{file_lines[i+1]}"].nil?
        @last_id += 1
        @messages["#{file_lines[i+1]}"] = {
          id: "#{@alias}-#{@last_id}",
          trigger: "Completed 500 Internal Server Error",
          lines: file_lines[i..(i+@lines)],
          count: 1,
          reported: false,
          index: i
        }
      else
        @messages["#{file_lines[i+1]}"][:count] += 1
      end
    end
  end
end

#formatted(text) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/loggr.rb', line 158

def formatted(text)
  if @format != ""
    text.gsub(@format["from"], @format["to"])
  else
    text
  end
end

#init_configObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/loggr.rb', line 18

def init_config
  @timer = 0
  @messages = {}
  @name = config["name"]
  @path = config["path"]
  @condition = config["condition"]
  @lines = config["lines"]
  @language = config["language"]
  @ignore = config["ignore"]
  @command = config["command"]
  @bash_command = config["bash_command"]
  @fetch_each = config["fetch_each"]
  @clear_each = config["clear_each"]
  @report_each = config["report_each"]
  @trello_list_id = config["trello_list_id"]
  @trello_labels = config["trello_labels"].downcase.split(',').map(&:strip).map(&:to_sym) if config["trello_labels"]
  @trello_members_ids = config["trello_members_ids"].split(',').map(&:strip) if config["trello_members_ids"]
  @color = config["color"]
  @alias = config["alias"]
  @last_id = 0
  @format = config["format"]
end

#last_messageObject



95
96
97
# File 'lib/loggr.rb', line 95

def last_message
  message_by_id("#{@alias}-#{@last_id}")
end

#lines_to_code(message) ⇒ Object



99
100
101
# File 'lib/loggr.rb', line 99

def lines_to_code(message)
  "#{message[:lines].join("\n")}"
end

#message_by_id(id) ⇒ Object



89
90
91
92
93
# File 'lib/loggr.rb', line 89

def message_by_id(id)
  @messages.each do |k, v|
    return v if v[:id] == id
  end
end

#message_to_chat(message) ⇒ Object



131
132
133
# File 'lib/loggr.rb', line 131

def message_to_chat(message)
  formatted("#{Emissary.header}\n**ID: #{message[:id]}**\nOcurrences: #{message[:count]}\n\n```#{@language}\n#{lines_to_code(message)}```")
end

#reportObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/loggr.rb', line 107

def report

client = Discordrb::Webhooks::Client.new(url: Emissary.webhooks_url)
client.execute do |builder|
  builder.content = "__**#{Emissary.server_name}**__\n**Report Alert** from **#{@name}** at #{Time.now}"
  builder.add_embed do |embed|
    embed.title = "#{@condition}"
    embed.description = "**Ocurrences: **\n#{@messages.count{|k,v| v[:reported] == false}}"
    embed.color = @color
    embed.timestamp = Time.now
    embed.fields = []
    @messages.each do |k, v|
      unless v[:reported]
        embed.fields.push({
          "name": "**ID: #{v[:id]}**\n\nOcurrences: #{v[:count]}",
          "value": formatted("```#{@language}\n#{v[:lines][0]}\n#{v[:lines][1]}```")
          })
          v[:reported] = true
        end
      end
    end
  end
end

#startObject



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/loggr.rb', line 41

def start

  one_at_a_time = Mutex.new

  Thread.new do
    loop do
      one_at_a_time.synchronize do
        if @timer % @fetch_each == 0
          fetch
        end
        if @timer % @clear_each == 0
          clear
        end
        if @timer % @report_each == 0
          report
        end
        @timer += 1
        sleep 1*60
      end
    end
  end

end

#syscallObject



166
167
168
169
170
171
172
173
# File 'lib/loggr.rb', line 166

def syscall
  begin
    stdout, stderr, status = Open3.capture3(@bash_command)
    status.success? && formatted(stdout.slice!(0..-(1 + $/.size)).gsub(/^(.{1500,}?).*/m,'\1...'))# strip trailing eol
  rescue
    "The command #{@bash_command} could not be executed"
  end
end

#truncate_lines(lines, length) ⇒ Object



103
104
105
# File 'lib/loggr.rb', line 103

def truncate_lines(lines, length)
  lines.gsub(/^(.{50,}?).*/m,'\1...')
end