dislogger 0.1.6 → 0.1.9

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: 36f02d278c361c1316f47a29bedad900e03183f2f40e750656cf200d01d80f66
4
- data.tar.gz: 2d78928ccb2740d8eca3a15fc81e5cc0168c2730511417b2ece3167a9f973f18
3
+ metadata.gz: 9a274a53633279d495fd56b23a36c52730b7ca8e8c848f918e88be497b3d1dcf
4
+ data.tar.gz: 7fb1ce90ab63bbff72470682564c11772d5985e32195c46681daa8daf95c83ac
5
5
  SHA512:
6
- metadata.gz: 519c66523ab2f57d73f85cd06cbda35ceef6e7ae359164d45df6c2b1b30aadfe5e6828c5aa344c7862b6a89cea8807b46905b9c01bc1e135fc1da7d53a677519
7
- data.tar.gz: 429ebbb1a4ee2faf591092e3cd532a519fc65d47f7a26a51a397298a31e2651dd766b4e0fdbd7cb06df689ba59a5d188f087d4b59a8c1502bd72508a40acf1ca
6
+ metadata.gz: 64dc81ec91e4c71e6e168aadd71ab5a7d6abd4a632a7f192a7373cf47a816a4365f232bcf337b834c29c12a1abba3c71fd660305e2e8a66bde392b4d24342f9a
7
+ data.tar.gz: 2f55c155f9158f0252599b34353546d65bb14b9ea447efb35ce9cc59ea9b0a58a05777775cdfc32a352065eb42d9026c169f3cf4445d01a4ca767f3be3b690da
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
@@ -13,7 +13,7 @@ module Dislogger
13
13
  @discord_webhook_url = nil
14
14
  @bot_username = 'Error Logger'
15
15
  @backtrace_lines_limit = 5
16
- @enabled_environments = %w[production staging]
16
+ @enabled_environments = %w[production staging development]
17
17
  @environment = nil
18
18
  @error_color_map = {
19
19
  500 => 15158332, # Red for server errors
@@ -7,16 +7,30 @@ module Dislogger
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  included do
10
- rescue_from StandardError, with: :handle_internal_server_error
11
- rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
12
- rescue_from ActiveRecord::RecordInvalid, with: :handle_unprocessable_entity
13
- rescue_from ActionController::ParameterMissing, with: :handle_unprocessable_entity
14
- rescue_from Pundit::NotAuthorizedError, with: :handle_forbidden if defined?(Pundit)
15
- rescue_from CanCan::AccessDenied, with: :handle_forbidden if defined?(CanCan)
10
+ if ancestors.include?(ActionController::API) || ancestors.include?(ActionController::Base)
11
+ rescue_from Exception, with: :handle_exception
12
+ rescue_from StandardError, with: :handle_internal_server_error
13
+ rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
14
+ rescue_from ActiveRecord::RecordInvalid, with: :handle_unprocessable_entity
15
+ rescue_from ActionController::ParameterMissing, with: :handle_unprocessable_entity
16
+ rescue_from Pundit::NotAuthorizedError, with: :handle_forbidden if defined?(Pundit)
17
+ rescue_from CanCan::AccessDenied, with: :handle_forbidden if defined?(CanCan)
18
+ end
16
19
  end
17
20
 
18
21
  private
19
22
 
23
+ def handle_exception(exception)
24
+ Rails.logger.error("Dislogger caught exception: #{exception.class.name} - #{exception.message}")
25
+ Rails.logger.error(exception.backtrace.join("\n")) if exception.backtrace
26
+
27
+ notify_and_render_error(
28
+ message: "#{exception.class.name}: #{exception.message}",
29
+ status: :internal_server_error,
30
+ backtrace: exception.backtrace
31
+ )
32
+ end
33
+
20
34
  def handle_not_found(exception)
21
35
  notify_and_render_error(
22
36
  message: exception.message.presence || 'Resource not found',
@@ -42,7 +56,7 @@ module Dislogger
42
56
 
43
57
  def handle_internal_server_error(exception)
44
58
  notify_and_render_error(
45
- message: 'Internal Server Error',
59
+ message: exception.message || 'Internal Server Error',
46
60
  status: :internal_server_error,
47
61
  backtrace: exception.backtrace
48
62
  )
@@ -51,12 +65,15 @@ module Dislogger
51
65
  def notify_and_render_error(message:, status:, details: nil, backtrace: nil)
52
66
  status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status]
53
67
 
54
- Notifiers::DiscordNotifier.new.notify(
68
+ notifier = Notifiers::DiscordNotifier.new
69
+ notification_result = notifier.notify(
55
70
  message: message,
56
71
  status: status_code,
57
72
  backtrace: backtrace
58
73
  )
59
74
 
75
+ Rails.logger.info("Dislogger notification result: #{notification_result}") if defined?(Rails)
76
+
60
77
  render_error(message, status, details)
61
78
  end
62
79
 
@@ -4,22 +4,21 @@ module Dislogger
4
4
  module Notifiers
5
5
  class DiscordNotifier < BaseNotifier
6
6
  def notify(message:, status:, backtrace: nil)
7
- return unless enabled? && @config.discord_webhook_url.present?
7
+ return false unless enabled? && @config.discord_webhook_url.present?
8
8
 
9
+ log_info("Attempting to send Discord notification")
9
10
  formatted_message = format_message(message, status, backtrace)
10
11
  send_notification(formatted_message)
11
12
  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
13
+ log_error("Discord notification failed: #{e.message}")
14
+ log_error(e.backtrace.join("\n")) if e.backtrace
17
15
  false
18
16
  end
19
17
 
20
18
  protected
21
19
 
22
20
  def format_message(message, status, backtrace)
21
+ log_info("Formatting Discord message")
23
22
  Formatters::DiscordFormatter.new(
24
23
  message: message,
25
24
  status: status,
@@ -29,6 +28,7 @@ module Dislogger
29
28
  end
30
29
 
31
30
  def send_notification(payload)
31
+ log_info("Sending notification to Discord")
32
32
  response = HTTParty.post(
33
33
  @config.discord_webhook_url,
34
34
  body: payload.to_json,
@@ -37,19 +37,40 @@ module Dislogger
37
37
 
38
38
  unless response.success?
39
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
40
+ log_error(error_message)
45
41
  return false
46
42
  end
47
43
 
44
+ log_info("Discord notification sent successfully")
48
45
  true
46
+ rescue StandardError => e
47
+ log_error("Failed to send Discord notification: #{e.message}")
48
+ log_error(e.backtrace.join("\n")) if e.backtrace
49
+ false
49
50
  end
50
51
 
51
52
  def enabled?
52
- @config.enabled?
53
+ result = @config.enabled?
54
+ log_info("Dislogger enabled? #{result} (environment: #{@config.environment}, enabled_environments: #{@config.enabled_environments})")
55
+ result
56
+ end
57
+
58
+ private
59
+
60
+ def log_info(message)
61
+ if defined?(Rails) && Rails.logger
62
+ Rails.logger.info("[Dislogger] #{message}")
63
+ else
64
+ puts "[Dislogger] #{message}"
65
+ end
66
+ end
67
+
68
+ def log_error(message)
69
+ if defined?(Rails) && Rails.logger
70
+ Rails.logger.error("[Dislogger] #{message}")
71
+ else
72
+ warn "[Dislogger] #{message}"
73
+ end
53
74
  end
54
75
  end
55
76
  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.6"
4
+ VERSION = "0.1.9"
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.6
4
+ version: 0.1.9
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-20 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: