logrithm 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86cba65ebff7f5967d1adbfb48940c461ba76511
4
- data.tar.gz: 7a10a6397937fa1500de32f65123776e27003ed5
3
+ metadata.gz: 19a6cbd393a80271537a01e19d935283dbb6a81b
4
+ data.tar.gz: 3705919f41dffebd5129156fcb4c3591c49067af
5
5
  SHA512:
6
- metadata.gz: b5a0d7e43974711935fe2e8f5faefc7c5e9dd663fe3ff683f9c4b810cb0528c8478880841c8bf7126fcaa58a10008e0386b27ec36a46f475d0943c3040eb5054
7
- data.tar.gz: 445167639261857670d8e2f72cbc55ae801d4e3abe5116410d0f935f0ef4805465780edbfd40d23bfc268544275490c5e5e0eae41d6b43f576832ddaca6ce1b3
6
+ metadata.gz: a5f25429828c665325913a0834e3a5f924a242a2dd719321099c43ebc51d708db8b2fb1f4c8cc6f9ec249d7e35195bfea6ae96f017c28eceecaeefa985f5efe3
7
+ data.tar.gz: af291bd299bb3e7bc281a06175635302baf02711a436f0d0c18b76ccf486a727d18bc3a9f78b86d2ad97b0847862c07415881347c19185b72d1c67e477999ad9
@@ -38,6 +38,7 @@ end
38
38
 
39
39
  require 'logrithm/log'
40
40
  require 'logrithm/utils/output'
41
+ require 'logrithm/utils/airslack'
41
42
  require 'logrithm/spitters'
42
43
 
43
44
  # This require is to be put into Rails initializer
@@ -0,0 +1,107 @@
1
+ module Logrithm
2
+ module Utils
3
+ class Airslack
4
+ require 'slack-notifier'
5
+
6
+ SLACK_CHANNEL = Logrithm::Log.option :general, :slack, :channel
7
+
8
+ SETTINGS = {
9
+ dbg: { color: '#999999', preface: "Debugged error" },
10
+ wrn: { color: '#DD00DD', preface: "Expected error" },
11
+ err: { color: '#DD0000', preface: "Internal server error" },
12
+ ftl: { color: '#FF9900', preface: "Unexpected error" }
13
+ }.freeze
14
+
15
+ class << self
16
+ # rubocop:disable Metrics/MethodLength
17
+ # rubocop:disable Metrics/AbcSize
18
+ def notify(**params)
19
+ # {
20
+ # :cause => #<ZeroDivisionError: divided by 0>,
21
+ # :controller => Api::V1::CompaniesController < Api::V1::BaseController,
22
+ # :current_api_user => "#<ApiUser id: 2, login: \"api_user_1@kantox.com\", email: ...
23
+ # :current_company => "#<Profile id: 1, group_id: 1, company_name: \"usd\", elegible_for_matching: nil, ...
24
+ # :current_power_class => "Power",
25
+ # :hide_locale_switcher => "false",
26
+ # :message => "Error #200 :: “divided by 0”",
27
+ # :owner => Api::GetCompany < Mutations::Command,
28
+ # :sender => "Kantox::Herro::ReportedError",
29
+ # :wrap => #<Kantox::Herro::Reporter:0x000000057eed20 @cause=#<Kantox::Herro::ReportedError: Error #200 :: “divided by 0”>>
30
+ # }
31
+
32
+ # {
33
+ # "text": "New Help Ticket Received:",
34
+ # "attachments": [
35
+ # {
36
+ # "title": "App hangs on reboot",
37
+ # "title_link": "http://domain.com/ticket/123456",
38
+ # "text": "If I restart my computer without quitting your app, it stops the reboot sequence.\nhttp://domain.com/ticket/123456",
39
+ # }
40
+ # ]
41
+ # }
42
+ fields = params.reject do |k, _|
43
+ %i(type cause message owner sender wrap backtrace).include? k
44
+ end.map do |k, v|
45
+ value = case v
46
+ when String then [v.to_s[0...64]]
47
+ when Array then v.map(&:to_s)
48
+ when Hash
49
+ [
50
+ v[:id] ? "id=“#{v[:id]}”" : nil,
51
+ v[:reference] ? "ref=“#{v[:reference]}”" : nil,
52
+ v[:name] ? "name=“#{v[:name]}”" : nil
53
+ ]
54
+ else
55
+ [
56
+ v.respond_to?(:id) ? "id=“#{v.id}”" : nil,
57
+ v.respond_to?(:reference) ? "ref=“#{v.reference}”" : nil,
58
+ v.respond_to?(:name) ? "name=“#{v.name}”" : nil
59
+ ]
60
+ end.compact.join(', ')
61
+
62
+ value = v.to_s[0...64] if value.empty?
63
+ { title: k, value: value, short: value.length <= 64 }
64
+ end
65
+
66
+ preface = SETTINGS[params[:type] || :ftl][:preface]
67
+ msg = "_#{preface}:_ #{params[:cause].try(:class)} | *#{params[:cause].try(:message)}*"
68
+ atts = [
69
+ {
70
+ fallback: "#{preface} in #{params[:owner] || '[see backtrace]'}",
71
+ color: SETTINGS[params[:type] || :ftl][:color],
72
+ title: "#{preface} in #{params[:owner] || '[see backtrace]'}",
73
+ fields: fields,
74
+ mrkdwn_in: [:text],
75
+ text: "```\n" << params[:cause].to_s << "\n```",
76
+ author_name: params[:owner].to_s
77
+ },
78
+ {
79
+ fallback: "Backtrace is available for desktop clients only",
80
+ color: '#DDDDDD',
81
+ title: "Backtrace (Rails.root related only)",
82
+ mrkdwn_in: [:text, :title],
83
+ text: "```\n" << params[:backtrace].map.with_index { |bt, i| bt =~ /#{Rails.root}/ ? "[#{i.to_s.rjust(3, '0')}] #{bt}" : nil }.compact.join($RS) << "\n```"
84
+ }
85
+ ]
86
+ SLACK.ping msg, attachments: atts
87
+ rescue => e
88
+ Kantox::Rescuer.log_error(e).airbrake
89
+ end
90
+ # rubocop:enable Metrics/AbcSize
91
+ # rubocop:enable Metrics/MethodLength
92
+
93
+ def my_first_private_ipv4
94
+ ip_priv = Socket.ip_address_list.detect(&:ipv4_private?)
95
+ ip_priv && ip_priv.ip_address || 'Unknown server'
96
+ end
97
+ end
98
+
99
+ SLACK = ::Slack::Notifier.new(
100
+ SLACK_CHANNEL[:endpoint],
101
+ channel: SLACK_CHANNEL[:channel],
102
+ icon_emoji: ':vertical_traffic_light:',
103
+ username: Airslack.my_first_private_ipv4
104
+ ) if SLACK_CHANNEL.is_a?(Hash)
105
+ end
106
+ end
107
+ end
@@ -1,3 +1,3 @@
1
1
  module Logrithm
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'awesome_print'
32
32
 
33
33
  spec.add_dependency 'kungfuig', '~> 0.7', '>= 0.7.5'
34
+ spec.add_dependency 'slack-notifier', '~> 1.2'
34
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logrithm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: 0.7.5
103
+ - !ruby/object:Gem::Dependency
104
+ name: slack-notifier
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.2'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.2'
103
117
  description: Some improvements to standard logger, such as detailed colored logger
104
118
  messages etc.
105
119
  email:
@@ -128,6 +142,7 @@ files:
128
142
  - lib/logrithm/spitters.rb
129
143
  - lib/logrithm/spitters/exception.rb
130
144
  - lib/logrithm/spitters/string.rb
145
+ - lib/logrithm/utils/airslack.rb
131
146
  - lib/logrithm/utils/color.rb
132
147
  - lib/logrithm/utils/helpers.rb
133
148
  - lib/logrithm/utils/output.rb