Class: Loggr
- Inherits:
-
Object
- Object
- Loggr
- Defined in:
- lib/loggr.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
Returns the value of attribute alias.
-
#bash_command ⇒ Object
Returns the value of attribute bash_command.
-
#clear_each ⇒ Object
Returns the value of attribute clear_each.
-
#color ⇒ Object
Returns the value of attribute color.
-
#command ⇒ Object
Returns the value of attribute command.
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#config ⇒ Object
Returns the value of attribute config.
-
#fetch_each ⇒ Object
Returns the value of attribute fetch_each.
-
#format ⇒ Object
Returns the value of attribute format.
-
#ignore ⇒ Object
Returns the value of attribute ignore.
-
#language ⇒ Object
Returns the value of attribute language.
-
#last_id ⇒ Object
Returns the value of attribute last_id.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#report_each ⇒ Object
Returns the value of attribute report_each.
-
#timer ⇒ Object
Returns the value of attribute timer.
-
#trello_labels ⇒ Object
Returns the value of attribute trello_labels.
-
#trello_list_id ⇒ Object
Returns the value of attribute trello_list_id.
-
#trello_members_ids ⇒ Object
Returns the value of attribute trello_members_ids.
Instance Method Summary collapse
- #clear ⇒ Object
- #create_trello_card(message) ⇒ Object
- #fetch ⇒ Object
- #formatted(text) ⇒ Object
- #init_config ⇒ Object
-
#initialize(config = {}) ⇒ Loggr
constructor
A new instance of Loggr.
- #last_message ⇒ Object
- #lines_to_code(message) ⇒ Object
- #message_by_id(id) ⇒ Object
- #message_to_chat(message) ⇒ Object
- #report ⇒ Object
- #start ⇒ Object
- #syscall ⇒ Object
- #truncate_lines(lines, length) ⇒ Object
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
#alias ⇒ Object
Returns the value of attribute alias.
8 9 10 |
# File 'lib/loggr.rb', line 8 def alias @alias end |
#bash_command ⇒ Object
Returns the value of attribute bash_command.
8 9 10 |
# File 'lib/loggr.rb', line 8 def bash_command @bash_command end |
#clear_each ⇒ Object
Returns the value of attribute clear_each.
8 9 10 |
# File 'lib/loggr.rb', line 8 def clear_each @clear_each end |
#color ⇒ Object
Returns the value of attribute color.
8 9 10 |
# File 'lib/loggr.rb', line 8 def color @color end |
#command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/loggr.rb', line 8 def command @command end |
#condition ⇒ Object
Returns the value of attribute condition.
8 9 10 |
# File 'lib/loggr.rb', line 8 def condition @condition end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/loggr.rb', line 8 def config @config end |
#fetch_each ⇒ Object
Returns the value of attribute fetch_each.
8 9 10 |
# File 'lib/loggr.rb', line 8 def fetch_each @fetch_each end |
#format ⇒ Object
Returns the value of attribute format.
8 9 10 |
# File 'lib/loggr.rb', line 8 def format @format end |
#ignore ⇒ Object
Returns the value of attribute ignore.
8 9 10 |
# File 'lib/loggr.rb', line 8 def ignore @ignore end |
#language ⇒ Object
Returns the value of attribute language.
8 9 10 |
# File 'lib/loggr.rb', line 8 def language @language end |
#last_id ⇒ Object
Returns the value of attribute last_id.
8 9 10 |
# File 'lib/loggr.rb', line 8 def last_id @last_id end |
#lines ⇒ Object
Returns the value of attribute lines.
8 9 10 |
# File 'lib/loggr.rb', line 8 def lines @lines end |
#messages ⇒ Object
Returns the value of attribute messages.
8 9 10 |
# File 'lib/loggr.rb', line 8 def end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/loggr.rb', line 8 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/loggr.rb', line 8 def path @path end |
#report_each ⇒ Object
Returns the value of attribute report_each.
8 9 10 |
# File 'lib/loggr.rb', line 8 def report_each @report_each end |
#timer ⇒ Object
Returns the value of attribute timer.
8 9 10 |
# File 'lib/loggr.rb', line 8 def timer @timer end |
#trello_labels ⇒ Object
Returns the value of attribute trello_labels.
8 9 10 |
# File 'lib/loggr.rb', line 8 def trello_labels @trello_labels end |
#trello_list_id ⇒ Object
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_ids ⇒ Object
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
#clear ⇒ Object
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() client = Trello::Client.new( developer_public_key: Emissary.trello_public_key, member_token: Emissary.trello_member_token ) card = client.create(:card, { 'name' => [: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 |
#fetch ⇒ Object
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 ["#{file_lines[i+1]}"].nil? @last_id += 1 ["#{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 ["#{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_config ⇒ Object
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 = {} @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_message ⇒ Object
95 96 97 |
# File 'lib/loggr.rb', line 95 def ("#{@alias}-#{@last_id}") end |
#lines_to_code(message) ⇒ Object
99 100 101 |
# File 'lib/loggr.rb', line 99 def lines_to_code() "#{message[:lines].join("\n")}" end |
#message_by_id(id) ⇒ Object
89 90 91 92 93 |
# File 'lib/loggr.rb', line 89 def (id) .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 () formatted("#{Emissary.header}\n**ID: #{message[:id]}**\nOcurrences: #{message[:count]}\n\n```#{@language}\n#{lines_to_code(message)}```") end |
#report ⇒ Object
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. do || .title = "#{@condition}" .description = "**Ocurrences: **\n#{@messages.count{|k,v| v[:reported] == false}}" .color = @color . = Time.now .fields = [] .each do |k, v| unless v[:reported] .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 |
#start ⇒ Object
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 |
#syscall ⇒ Object
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 |