dislogger 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68999016d4bdfa0436b75a70149e7a6f4b9643a4f2277fcf6a720a324dcfebbf
4
- data.tar.gz: 66ea5ef0d7e5bbfa17016aefe4da05e701219b5ea8bdc0731e145e693ab0c310
3
+ metadata.gz: 36f02d278c361c1316f47a29bedad900e03183f2f40e750656cf200d01d80f66
4
+ data.tar.gz: 2d78928ccb2740d8eca3a15fc81e5cc0168c2730511417b2ece3167a9f973f18
5
5
  SHA512:
6
- metadata.gz: d5b3dbb2aaa1aa967abbd49aa1a4e66a85cf7d76406d54bb47f9d29f3efca8a493915153045e4203ad93f90556693bbc2073fd708c656289b6158848c82d4fc2
7
- data.tar.gz: 67affcd9886734dee1ef2913d6efe5d4b800f506c84ba3087dde657155504c8a04e3bac209379bcf152e2fa8bf416ae3d47a572e5f83f471db385ee40e7ce07a
6
+ metadata.gz: 519c66523ab2f57d73f85cd06cbda35ceef6e7ae359164d45df6c2b1b30aadfe5e6828c5aa344c7862b6a89cea8807b46905b9c01bc1e135fc1da7d53a677519
7
+ data.tar.gz: 429ebbb1a4ee2faf591092e3cd532a519fc65d47f7a26a51a397298a31e2651dd766b4e0fdbd7cb06df689ba59a5d188f087d4b59a8c1502bd72508a40acf1ca
@@ -11,10 +11,10 @@ module Dislogger
11
11
 
12
12
  def initialize
13
13
  @discord_webhook_url = nil
14
- @environment = nil
15
14
  @bot_username = 'Error Logger'
16
15
  @backtrace_lines_limit = 5
17
16
  @enabled_environments = %w[production staging]
17
+ @environment = nil
18
18
  @error_color_map = {
19
19
  500 => 15158332, # Red for server errors
20
20
  404 => 3447003, # Blue for not found
@@ -24,6 +24,14 @@ module Dislogger
24
24
  }
25
25
  end
26
26
 
27
+ def validate!
28
+ raise Errors::ConfigurationError, 'Discord webhook URL is required' unless discord_webhook_url.present?
29
+ raise Errors::ConfigurationError, 'Bot username is required' unless bot_username.present?
30
+ raise Errors::ConfigurationError, 'Enabled environments must be an array' unless enabled_environments.is_a?(Array)
31
+ raise Errors::ConfigurationError, 'Environment must be present' unless environment.present?
32
+ true
33
+ end
34
+
27
35
  def enabled?
28
36
  return false if environment.nil? || enabled_environments.empty?
29
37
  enabled_environments.include?(environment)
@@ -8,11 +8,11 @@ module Dislogger
8
8
  username: @config.bot_username,
9
9
  embeds: [
10
10
  {
11
- title: "#{@config.environment.capitalize} - Error Notification (#{@status})",
12
- description: @message,
13
- color: @config.error_color_map[@status] || @config.error_color_map[:default],
11
+ title: format_title,
12
+ description: format_description,
13
+ color: get_error_color,
14
14
  fields: build_fields,
15
- timestamp: Time.current.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
15
+ timestamp: Time.current.utc.iso8601
16
16
  }
17
17
  ]
18
18
  }
@@ -20,22 +20,46 @@ module Dislogger
20
20
 
21
21
  private
22
22
 
23
+ def format_title
24
+ "#{@config.environment.capitalize} - Error Notification (#{@status})"
25
+ end
26
+
27
+ def format_description
28
+ return @message if @message.is_a?(String)
29
+ return @message.message if @message.respond_to?(:message)
30
+ @message.to_s
31
+ end
32
+
33
+ def get_error_color
34
+ @config.error_color_map[@status] || @config.error_color_map[:default]
35
+ end
36
+
23
37
  def build_fields
24
38
  fields = [
25
39
  { name: 'Status Code', value: @status.to_s, inline: true },
26
40
  { name: 'Environment', value: @config.environment, inline: true }
27
41
  ]
28
42
 
29
- if @backtrace
43
+ if @backtrace && !@backtrace.empty?
30
44
  fields << {
31
45
  name: 'Backtrace',
32
- value: @backtrace.first(@config.backtrace_lines_limit).join("\n"),
46
+ value: format_backtrace(@backtrace),
33
47
  inline: false
34
48
  }
35
49
  end
36
50
 
37
51
  fields
38
52
  end
53
+
54
+ def format_backtrace(backtrace)
55
+ return 'No backtrace available' if backtrace.nil? || backtrace.empty?
56
+
57
+ trace = backtrace.first(@config.backtrace_lines_limit)
58
+ if trace.length >= @config.backtrace_lines_limit
59
+ trace << "... (truncated)"
60
+ end
61
+ trace.join("\n")
62
+ end
39
63
  end
40
64
  end
41
65
  end
@@ -8,6 +8,13 @@ module Dislogger
8
8
 
9
9
  formatted_message = format_message(message, status, backtrace)
10
10
  send_notification(formatted_message)
11
+ rescue StandardError => e
12
+ if defined?(Rails) && Rails.logger
13
+ Rails.logger.error("Discord notification failed: #{e.message}")
14
+ else
15
+ warn("Discord notification failed: #{e.message}")
16
+ end
17
+ false
11
18
  end
12
19
 
13
20
  protected
@@ -22,17 +29,27 @@ module Dislogger
22
29
  end
23
30
 
24
31
  def send_notification(payload)
25
- HTTParty.post(
32
+ response = HTTParty.post(
26
33
  @config.discord_webhook_url,
27
34
  body: payload.to_json,
28
35
  headers: { 'Content-Type' => 'application/json' }
29
36
  )
30
- rescue StandardError => e
31
- if defined?(Rails) && Rails.logger
32
- Rails.logger.error("Discord notification failed: #{e.message}")
33
- else
34
- warn("Discord notification failed: #{e.message}")
37
+
38
+ unless response.success?
39
+ error_message = "Discord API Error: #{response.code} - #{response.body}"
40
+ if defined?(Rails) && Rails.logger
41
+ Rails.logger.error(error_message)
42
+ else
43
+ warn(error_message)
44
+ end
45
+ return false
35
46
  end
47
+
48
+ true
49
+ end
50
+
51
+ def enabled?
52
+ @config.enabled?
36
53
  end
37
54
  end
38
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dislogger
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dislogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nelson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-17 00:00:00.000000000 Z
11
+ date: 2025-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails