dislogger 0.1.4 → 0.1.8

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: 0b7ddd219546d4b47916d9e0cfd1303679ed1a3f4befa6675a7be486ada7de25
4
+ data.tar.gz: '001685aeefc4f82fd7a07f8a539ea9c737de5a1760eebdc0799660ea62c4087d'
5
5
  SHA512:
6
- metadata.gz: d5b3dbb2aaa1aa967abbd49aa1a4e66a85cf7d76406d54bb47f9d29f3efca8a493915153045e4203ad93f90556693bbc2073fd708c656289b6158848c82d4fc2
7
- data.tar.gz: 67affcd9886734dee1ef2913d6efe5d4b800f506c84ba3087dde657155504c8a04e3bac209379bcf152e2fa8bf416ae3d47a572e5f83f471db385ee40e7ce07a
6
+ metadata.gz: 40495b36577cce4bb00ea8ab38b74019b4792e2ca6452c1edd735f4042557403ab382bb70b08f94e4fb952b7e5240c1828057a5068f76aad4c1da5ba6be1fc41
7
+ data.tar.gz: 2d90483f3e1db1969355be2565384cde9f38f7048e5dd4f7573203a47bb814f3ba45bf11554bf9e0dd71c4751e0d7fcece7d547664afa8f2959bc98fbdba0eb9
data/README.md CHANGED
@@ -7,12 +7,13 @@ A Ruby on Rails gem for elegant error handling and Discord notifications. Automa
7
7
 
8
8
  ## Features
9
9
 
10
- - 🚨 Automatic error handling for Rails applications
10
+ - 🚨 Automatic error handling for Rails applications (both API and regular apps)
11
11
  - 🎯 Discord notifications for errors with customizable formatting
12
12
  - 📝 Detailed error information including backtrace
13
13
  - 🎨 Customizable error colors for different status codes
14
14
  - 🔧 Environment-based configuration
15
15
  - 🛡️ Built-in support for common Rails exceptions
16
+ - ⚡ Works with both `ActionController::API` and `ActionController::Base`
16
17
 
17
18
  ## Installation
18
19
 
@@ -36,6 +37,16 @@ $ gem install dislogger
36
37
 
37
38
  ## Configuration
38
39
 
40
+ ### Quick Start
41
+
42
+ After installing the gem, run the generator to create the configuration file:
43
+
44
+ ```bash
45
+ $ rails generate dislogger:install
46
+ ```
47
+
48
+ This will create an initializer at `config/initializers/dislogger.rb` with all available configuration options and their documentation.
49
+
39
50
  ### Setting up Discord Webhook
40
51
 
41
52
  Before configuring the gem, you need to set up a webhook in your Discord server:
@@ -68,27 +79,27 @@ DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/your-webhook-url
68
79
 
69
80
  Make sure to add `.env` to your `.gitignore` file to keep your webhook URL secure.
70
81
 
71
- ### Additional Configuration Options
82
+ ### Configuration Options
72
83
 
73
- Create an initializer file at `config/initializers/dislogger.rb` with all available options:
84
+ The generator will create an initializer with all available options:
74
85
 
75
86
  ```ruby
76
87
  Dislogger.configure do |config|
77
88
  # Required: Your Discord webhook URL (preferably from environment variables)
78
89
  config.discord_webhook_url = ENV['DISCORD_WEBHOOK_URL']
79
90
 
91
+ # Required: Current environment (defaults to Rails.env if available)
92
+ config.environment = Rails.env
93
+
80
94
  # Optional: Custom bot username (default: 'Error Logger')
81
95
  config.bot_username = 'My App Error Logger'
82
96
 
83
97
  # Optional: Number of backtrace lines to include (default: 5)
84
98
  config.backtrace_lines_limit = 10
85
99
 
86
- # Optional: Environments where notifications are enabled (default: ['production', 'staging'])
100
+ # Optional: Environments where notifications are enabled (default: ['production', 'staging', 'development'])
87
101
  config.enabled_environments = ['production', 'staging', 'development']
88
102
 
89
- # Optional: Current environment (default: Rails.env if available)
90
- config.environment = 'production'
91
-
92
103
  # Optional: Custom error colors (default values shown below)
93
104
  config.error_color_map = {
94
105
  500 => 15158332, # Red
@@ -107,6 +118,12 @@ end
107
118
  The gem automatically includes error handling in your Rails controllers. No additional setup is required!
108
119
 
109
120
  ```ruby
121
+ # For API applications
122
+ class ApplicationController < ActionController::API
123
+ # Error handling is automatically included
124
+ end
125
+
126
+ # For regular Rails applications
110
127
  class ApplicationController < ActionController::Base
111
128
  # Error handling is automatically included
112
129
  end
@@ -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
- @enabled_environments = %w[production staging]
16
+ @enabled_environments = %w[production staging development]
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
@@ -2,9 +2,23 @@
2
2
 
3
3
  module Dislogger
4
4
  class Railtie < Rails::Railtie
5
- initializer 'dislogger.configure_rails_initialization' do
5
+ initializer 'dislogger.configure_rails_initialization' do |app|
6
+ # Configurar el ambiente por defecto si no está configurado
7
+ ActiveSupport.on_load(:before_configuration) do
8
+ Dislogger.configure do |config|
9
+ config.environment ||= Rails.env
10
+ end
11
+ end
12
+
13
+ # Incluir el ErrorHandler en los controladores
6
14
  ActiveSupport.on_load(:action_controller) do
7
- ActionController::Base.include Dislogger::ErrorHandler
15
+ if defined?(ActionController::API)
16
+ ActionController::API.include(Dislogger::ErrorHandler)
17
+ end
18
+
19
+ if defined?(ActionController::Base)
20
+ ActionController::Base.include(Dislogger::ErrorHandler)
21
+ end
8
22
  end
9
23
  end
10
24
  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.8"
5
5
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dislogger
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def create_initializer_file
9
+ template 'dislogger.rb', 'config/initializers/dislogger.rb'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dislogger.configure do |config|
4
+ # Configura la URL del webhook de Discord (requerido)
5
+ config.discord_webhook_url = ENV['DISCORD_WEBHOOK_URL']
6
+
7
+ # Configura el ambiente actual (requerido)
8
+ config.environment = Rails.env
9
+
10
+ # Configura el nombre del bot (opcional, por defecto: 'Error Logger')
11
+ # config.bot_username = 'Mi App Error Logger'
12
+
13
+ # Configura el número de líneas del backtrace (opcional, por defecto: 5)
14
+ # config.backtrace_lines_limit = 10
15
+
16
+ # Configura los ambientes donde las notificaciones están habilitadas
17
+ # (opcional, por defecto: ['production', 'staging', 'development'])
18
+ # config.enabled_environments = ['production', 'staging', 'development']
19
+
20
+ # Configura los colores de los errores (opcional)
21
+ # config.error_color_map = {
22
+ # 500 => 15158332, # Rojo para errores del servidor
23
+ # 404 => 3447003, # Azul para no encontrado
24
+ # 422 => 16776960, # Amarillo para errores de validación
25
+ # 403 => 15105570, # Naranja para prohibido
26
+ # default: 10181046 # Gris para otros
27
+ # }
28
+ 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.8
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-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -192,6 +192,8 @@ files:
192
192
  - lib/dislogger/notifiers/discord_notifier.rb
193
193
  - lib/dislogger/railtie.rb
194
194
  - lib/dislogger/version.rb
195
+ - lib/generators/dislogger/install/install_generator.rb
196
+ - lib/generators/dislogger/install/templates/dislogger.rb
195
197
  - sig/dislogger.rbs
196
198
  homepage: https://github.com/nduartex/dislogger
197
199
  licenses: