Class: Rack::AI::Features::Logging

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ai/features/logging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, config) ⇒ Logging

Returns a new instance of Logging.



9
10
11
12
13
# File 'lib/rack/ai/features/logging.rb', line 9

def initialize(provider, config)
  @name = :logging
  @provider = provider
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/rack/ai/features/logging.rb', line 7

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rack/ai/features/logging.rb', line 7

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



7
8
9
# File 'lib/rack/ai/features/logging.rb', line 7

def provider
  @provider
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rack/ai/features/logging.rb', line 15

def enabled?
  @config.feature_enabled?(:logging)
end

#generate_traffic_summary(time_window = 3600) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rack/ai/features/logging.rb', line 60

def generate_traffic_summary(time_window = 3600)
  # This would be called periodically to generate AI-powered traffic summaries
  return { processed: false, reason: "disabled" } unless enabled?

  # In a real implementation, this would analyze stored log data
  sample_data = build_sample_traffic_data(time_window)
  
  begin
    summary = @provider.analyze_patterns(sample_data)
    
    {
      time_window: time_window,
      summary: summary,
      generated_at: Time.now.iso8601
    }
  rescue => e
    Utils::Logger.error("Failed to generate traffic summary", error: e.message)
    { error: e.message, generated_at: Time.now.iso8601 }
  end
end

#process_request(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/ai/features/logging.rb', line 23

def process_request(env)
  return { processed: false, reason: "disabled" } unless enabled?

  # Collect request metadata for AI analysis
  request_summary = build_request_summary(env)
  
  # Generate AI insights about the request
  insights = generate_request_insights(request_summary, env)

  {
    request_summary: request_summary,
    ai_insights: insights,
    feature: @name,
    timestamp: Time.now.iso8601
  }
end

#process_response(env, status, headers, body) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack/ai/features/logging.rb', line 40

def process_response(env, status, headers, body)
  return { processed: false, reason: "disabled" } unless enabled?

  # Collect response metadata
  response_summary = build_response_summary(status, headers, body)
  
  # Generate AI insights about the complete request-response cycle
  cycle_insights = generate_cycle_insights(env, response_summary)

  # Log structured data for AI analysis
  log_structured_data(env, response_summary, cycle_insights)

  {
    response_summary: response_summary,
    cycle_insights: cycle_insights,
    feature: "#{@name}_response",
    timestamp: Time.now.iso8601
  }
end

#process_response?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rack/ai/features/logging.rb', line 19

def process_response?
  true
end