Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

Commit a685b77

Browse files
committed
Enhance Logtail logger configuration in application.rb with detailed logging and error handling for missing environment variables
1 parent 85f6d88 commit a685b77

3 files changed

Lines changed: 132 additions & 3 deletions

File tree

config/application.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,30 @@ class Application < Rails::Application
2727
# config.eager_load_paths << Rails.root.join("extras")
2828
config.active_support.to_time_preserves_timezone = :zone
2929

30-
if ENV["LOGTAIL_API_KEY"].present? && ENV["LOGTAIL_INGESTING_HOST"].present?
31-
config.logger = Logtail::Logger.create_default_logger(ENV["LOGTAIL_API_KEY"], ingesting_host: ENV["LOGTAIL_INGESTING_HOST"])
32-
end
30+
if ENV["LOGTAIL_API_KEY"].present? && ENV["LOGTAIL_INGESTING_HOST"].present?
31+
begin
32+
puts "🌲 Configuring Logtail..."
33+
puts " API Key: #{ENV['LOGTAIL_API_KEY'] ? 'Present' : 'Missing'}"
34+
puts " Ingesting Host: #{ENV['LOGTAIL_INGESTING_HOST']}"
35+
36+
config.logger = Logtail::Logger.create_default_logger(
37+
ENV["LOGTAIL_API_KEY"],
38+
ingesting_host: ENV["LOGTAIL_INGESTING_HOST"]
39+
)
40+
41+
puts "✅ Logtail logger configured successfully"
42+
rescue => e
43+
puts "❌ Failed to configure Logtail: #{e.message}"
44+
puts " Backtrace: #{e.backtrace.first(3).join(', ')}"
45+
puts " Using standard Rails logger instead"
46+
# Fall back to standard Rails logger
47+
config.logger = ActiveSupport::Logger.new(STDOUT)
48+
end
49+
else
50+
missing_vars = []
51+
missing_vars << "LOGTAIL_API_KEY" unless ENV["LOGTAIL_API_KEY"].present?
52+
missing_vars << "LOGTAIL_INGESTING_HOST" unless ENV["LOGTAIL_INGESTING_HOST"].present?
53+
puts "⚠️ Logtail not configured. Missing environment variables: #{missing_vars.join(', ')}"
54+
end
3355
end
3456
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Temporary debug initializer for Logtail thread monitoring
2+
# Remove this file once the issue is resolved
3+
4+
Rails.application.config.after_initialize do
5+
if defined?(Logtail) && Rails.logger.is_a?(Logtail::Logger)
6+
puts "🔍 Monitoring Logtail threads after Rails initialization..."
7+
8+
# Check for Logtail threads
9+
logtail_threads = Thread.list.select do |thread|
10+
thread.backtrace_locations&.any? { |loc| loc.path.include?("logtail") }
11+
end
12+
13+
if logtail_threads.any?
14+
puts " Found #{logtail_threads.count} Logtail threads:"
15+
logtail_threads.each_with_index do |thread, i|
16+
puts " Thread #{i + 1}: #{thread.status} - #{thread.backtrace_locations&.first}"
17+
end
18+
19+
# Schedule a thread check after 10 seconds
20+
Thread.new do
21+
sleep 10
22+
puts "🔍 Thread status after 10 seconds:"
23+
logtail_threads.each_with_index do |thread, i|
24+
if thread.alive?
25+
puts " Thread #{i + 1}: #{thread.status}"
26+
if thread.status == "sleep"
27+
puts " ⚠️ Thread is sleeping - possible connectivity issue"
28+
end
29+
else
30+
puts " Thread #{i + 1}: ❌ Dead"
31+
end
32+
end
33+
end
34+
else
35+
puts " ✅ No Logtail threads found"
36+
end
37+
else
38+
puts "⚠️ Logtail logger not configured or not detected"
39+
end
40+
end

debug_logtail.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'bundler/setup'
4+
require 'logtail-rails'
5+
require 'dotenv'
6+
7+
# Load environment variables if using .env file
8+
Dotenv.load if defined?(Dotenv)
9+
10+
puts "🔍 Debugging Logtail Configuration"
11+
puts "=================================="
12+
13+
# Check environment variables
14+
api_key = ENV["LOGTAIL_API_KEY"]
15+
ingesting_host = ENV["LOGTAIL_INGESTING_HOST"]
16+
17+
puts "Environment Variables:"
18+
puts " LOGTAIL_API_KEY: #{api_key ? "Present (#{api_key[0..8]}...)" : "❌ Missing"}"
19+
puts " LOGTAIL_INGESTING_HOST: #{ingesting_host || "❌ Missing"}"
20+
puts
21+
22+
if api_key.nil? || ingesting_host.nil?
23+
puts "❌ Missing required environment variables. Common values:"
24+
puts " LOGTAIL_INGESTING_HOST should be:"
25+
puts " - https://in.logs.betterstack.com (US)"
26+
puts " - https://in.logs.eu.betterstack.com (EU)"
27+
exit 1
28+
end
29+
30+
# Test basic connectivity
31+
puts "🌐 Testing Network Connectivity to #{ingesting_host}..."
32+
begin
33+
require 'net/http'
34+
require 'uri'
35+
36+
uri = URI(ingesting_host)
37+
response = Net::HTTP.get_response(uri)
38+
puts " Status: #{response.code} #{response.message}"
39+
rescue => e
40+
puts " ❌ Network Error: #{e.message}"
41+
puts " This could explain why Logtail threads are stuck!"
42+
end
43+
44+
puts
45+
46+
# Test Logtail logger creation
47+
puts "🧪 Testing Logtail Logger Creation..."
48+
begin
49+
logger = Logtail::Logger.create_default_logger(api_key, ingesting_host: ingesting_host)
50+
puts " ✅ Logger created successfully"
51+
52+
puts "🧪 Testing Log Message..."
53+
logger.info("Test message from debug script", { test: true, timestamp: Time.now })
54+
puts " ✅ Log message sent"
55+
56+
puts "🧪 Closing logger to flush messages..."
57+
logger.close
58+
puts " ✅ Logger closed"
59+
60+
rescue => e
61+
puts " ❌ Error: #{e.message}"
62+
puts " Backtrace:"
63+
e.backtrace.first(5).each { |line| puts " #{line}" }
64+
end
65+
66+
puts
67+
puts "✅ Debug complete. Check your Better Stack dashboard for the test message."

0 commit comments

Comments
 (0)