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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
"""
Structured logging implementation with trace context integration.

This module provides enhanced logging capabilities with automatic
trace context injection and structured log formatting.
"""

from src.core import json_utils
import logging
import time
from typing import Dict, Any, Optional, Union, List
from datetime import datetime, timezone
from enum import Enum
import traceback
import sys
import threading
from pathlib import Path

from pythonjsonlogger import jsonlogger

from src.core import get_logger
from .correlation import CorrelationContext


class LogLevel(str, Enum):
    """Log levels for structured logging."""
    DEBUG = "DEBUG"
    INFO = "INFO"
    WARNING = "WARNING"
    ERROR = "ERROR"
    CRITICAL = "CRITICAL"


class LogEventType(str, Enum):
    """Types of log events for categorization."""
    REQUEST = "request"
    RESPONSE = "response"
    ERROR = "error"
    BUSINESS = "business"
    SECURITY = "security"
    PERFORMANCE = "performance"
    AUDIT = "audit"
    SYSTEM = "system"
    AGENT = "agent"
    INVESTIGATION = "investigation"
    ANOMALY = "anomaly"
    DATABASE = "database"
    CACHE = "cache"
    EXTERNAL_API = "external_api"


class StructuredLogRecord:
    """
    Structured log record with standardized fields.
    """
    
    def __init__(
        self,
        message: str,
        level: LogLevel,
        event_type: LogEventType,
        timestamp: Optional[datetime] = None,
        correlation_id: Optional[str] = None,
        request_id: Optional[str] = None,
        user_id: Optional[str] = None,
        session_id: Optional[str] = None,
        span_id: Optional[str] = None,
        component: Optional[str] = None,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        error_type: Optional[str] = None,
        error_stack: Optional[str] = None,
        additional_data: Optional[Dict[str, Any]] = None
    ):
        """
        Initialize structured log record.
        
        Args:
            message: Log message
            level: Log level
            event_type: Type of event
            timestamp: Event timestamp (defaults to now)
            correlation_id: Correlation ID
            request_id: Request ID
            user_id: User ID
            session_id: Session ID
            span_id: Span ID
            component: Component/module name
            operation: Operation name
            duration_ms: Operation duration
            error_type: Error type for error events
            error_stack: Error stack trace
            additional_data: Additional structured data
        """
        self.message = message
        self.level = level
        self.event_type = event_type
        self.timestamp = timestamp or datetime.now(timezone.utc)
        self.correlation_id = correlation_id or CorrelationContext.get_correlation_id()
        self.request_id = request_id or CorrelationContext.get_request_id()
        self.user_id = user_id or CorrelationContext.get_user_id()
        self.session_id = session_id or CorrelationContext.get_session_id()
        self.span_id = span_id or CorrelationContext.get_span_id()
        self.component = component
        self.operation = operation
        self.duration_ms = duration_ms
        self.error_type = error_type
        self.error_stack = error_stack
        self.additional_data = additional_data or {}
        
        # Add thread and process information
        self.thread_id = threading.get_ident()
        self.thread_name = threading.current_thread().name
        self.process_id = os.getpid() if 'os' in sys.modules else None
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for JSON serialization."""
        record = {
            "timestamp": self.timestamp.isoformat(),
            "level": self.level.value,
            "message": self.message,
            "event_type": self.event_type.value,
            "thread_id": self.thread_id,
            "thread_name": self.thread_name
        }
        
        # Add correlation fields if present
        if self.correlation_id:
            record["correlation_id"] = self.correlation_id
        if self.request_id:
            record["request_id"] = self.request_id
        if self.user_id:
            record["user_id"] = self.user_id
        if self.session_id:
            record["session_id"] = self.session_id
        if self.span_id:
            record["span_id"] = self.span_id
        
        # Add optional fields
        if self.component:
            record["component"] = self.component
        if self.operation:
            record["operation"] = self.operation
        if self.duration_ms is not None:
            record["duration_ms"] = self.duration_ms
        if self.error_type:
            record["error_type"] = self.error_type
        if self.error_stack:
            record["error_stack"] = self.error_stack
        if self.process_id:
            record["process_id"] = self.process_id
        
        # Add additional data
        if self.additional_data:
            record["data"] = self.additional_data
        
        return record
    
    def to_json(self) -> str:
        """Convert to JSON string."""
        return json_utils.dumps(self.to_dict(), ensure_ascii=False)


class TraceContextFormatter(jsonlogger.JsonFormatter):
    """
    Custom JSON formatter that includes trace context.
    """
    
    def __init__(
        self,
        fmt: Optional[str] = None,
        datefmt: Optional[str] = None,
        style: str = '%',
        validate: bool = True,
        include_trace_context: bool = True,
        service_name: str = "cidadao-ai-backend",
        service_version: str = "1.0.0"
    ):
        """
        Initialize trace context formatter.
        
        Args:
            fmt: Format string
            datefmt: Date format string
            style: Format style
            validate: Validate format
            include_trace_context: Include trace context in logs
            service_name: Service name
            service_version: Service version
        """
        super().__init__(fmt, datefmt, style, validate)
        self.include_trace_context = include_trace_context
        self.service_name = service_name
        self.service_version = service_version
    
    def add_fields(
        self,
        log_record: Dict[str, Any],
        record: logging.LogRecord,
        message_dict: Dict[str, Any]
    ):
        """Add custom fields to log record."""
        super().add_fields(log_record, record, message_dict)
        
        # Add service information
        log_record["service"] = {
            "name": self.service_name,
            "version": self.service_version
        }
        
        # Add timestamp in ISO format
        if "timestamp" not in log_record:
            log_record["timestamp"] = datetime.now(timezone.utc).isoformat()
        
        # Add trace context if enabled
        if self.include_trace_context:
            correlation_id = CorrelationContext.get_correlation_id()
            if correlation_id:
                log_record["correlation_id"] = correlation_id
            
            request_id = CorrelationContext.get_request_id()
            if request_id:
                log_record["request_id"] = request_id
            
            user_id = CorrelationContext.get_user_id()
            if user_id:
                log_record["user_id"] = user_id
            
            session_id = CorrelationContext.get_session_id()
            if session_id:
                log_record["session_id"] = session_id
            
            span_id = CorrelationContext.get_span_id()
            if span_id:
                log_record["span_id"] = span_id
        
        # Add thread information
        log_record["thread_id"] = threading.get_ident()
        log_record["thread_name"] = threading.current_thread().name
        
        # Add file location
        log_record["location"] = {
            "file": record.filename,
            "line": record.lineno,
            "function": record.funcName,
            "module": record.module
        }
        
        # Add level name
        log_record["level"] = record.levelname
        
        # Process exception information
        if record.exc_info:
            log_record["exception"] = {
                "type": record.exc_info[0].__name__ if record.exc_info[0] else None,
                "message": str(record.exc_info[1]) if record.exc_info[1] else None,
                "stack_trace": traceback.format_exception(*record.exc_info)
            }


class StructuredLogger:
    """
    Enhanced logger with structured logging capabilities.
    """
    
    def __init__(
        self,
        name: str,
        level: LogLevel = LogLevel.INFO,
        component: Optional[str] = None
    ):
        """
        Initialize structured logger.
        
        Args:
            name: Logger name
            level: Default log level
            component: Component name for all logs
        """
        self.name = name
        self.component = component
        self.logger = get_logger(name)
        self.level = level
        
        # Note: structlog loggers don't have setLevel method
        # Level filtering is handled by structlog configuration
    
    def _configure_json_logging(self):
        """Configure JSON logging for the logger."""
        # Note: structlog handles formatting internally
        # This method is kept for compatibility but doesn't need to do anything
        # as structlog is already configured in core.logging
        pass
    
    def _log_structured(
        self,
        record: StructuredLogRecord
    ):
        """Log a structured record."""
        # Convert to standard logging extra
        extra = record.to_dict()
        message = extra.pop("message")
        level_name = extra.pop("level")
        
        # Log with appropriate level
        log_method = getattr(self.logger, level_name.lower())
        log_method(message, extra=extra)
    
    def debug(
        self,
        message: str,
        event_type: LogEventType = LogEventType.SYSTEM,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        **kwargs
    ):
        """Log debug message."""
        record = StructuredLogRecord(
            message=message,
            level=LogLevel.DEBUG,
            event_type=event_type,
            component=self.component,
            operation=operation,
            duration_ms=duration_ms,
            additional_data=kwargs
        )
        self._log_structured(record)
    
    def info(
        self,
        message: str,
        event_type: LogEventType = LogEventType.SYSTEM,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        **kwargs
    ):
        """Log info message."""
        record = StructuredLogRecord(
            message=message,
            level=LogLevel.INFO,
            event_type=event_type,
            component=self.component,
            operation=operation,
            duration_ms=duration_ms,
            additional_data=kwargs
        )
        self._log_structured(record)
    
    def warning(
        self,
        message: str,
        event_type: LogEventType = LogEventType.SYSTEM,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        **kwargs
    ):
        """Log warning message."""
        record = StructuredLogRecord(
            message=message,
            level=LogLevel.WARNING,
            event_type=event_type,
            component=self.component,
            operation=operation,
            duration_ms=duration_ms,
            additional_data=kwargs
        )
        self._log_structured(record)
    
    def error(
        self,
        message: str,
        event_type: LogEventType = LogEventType.ERROR,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        error: Optional[Exception] = None,
        **kwargs
    ):
        """Log error message."""
        error_type = None
        error_stack = None
        
        if error:
            error_type = type(error).__name__
            error_stack = traceback.format_exc()
        
        record = StructuredLogRecord(
            message=message,
            level=LogLevel.ERROR,
            event_type=event_type,
            component=self.component,
            operation=operation,
            duration_ms=duration_ms,
            error_type=error_type,
            error_stack=error_stack,
            additional_data=kwargs
        )
        self._log_structured(record)
    
    def critical(
        self,
        message: str,
        event_type: LogEventType = LogEventType.ERROR,
        operation: Optional[str] = None,
        duration_ms: Optional[float] = None,
        error: Optional[Exception] = None,
        **kwargs
    ):
        """Log critical message."""
        error_type = None
        error_stack = None
        
        if error:
            error_type = type(error).__name__
            error_stack = traceback.format_exc()
        
        record = StructuredLogRecord(
            message=message,
            level=LogLevel.CRITICAL,
            event_type=event_type,
            component=self.component,
            operation=operation,
            duration_ms=duration_ms,
            error_type=error_type,
            error_stack=error_stack,
            additional_data=kwargs
        )
        self._log_structured(record)
    
    # Business-specific logging methods
    def log_request(
        self,
        method: str,
        path: str,
        status_code: int,
        duration_ms: float,
        user_agent: Optional[str] = None,
        client_ip: Optional[str] = None
    ):
        """Log HTTP request."""
        self.info(
            f"{method} {path} - {status_code}",
            event_type=LogEventType.REQUEST,
            operation="http_request",
            duration_ms=duration_ms,
            method=method,
            path=path,
            status_code=status_code,
            user_agent=user_agent,
            client_ip=client_ip
        )
    
    def log_investigation(
        self,
        investigation_id: str,
        action: str,
        query: Optional[str] = None,
        confidence_score: Optional[float] = None,
        duration_ms: Optional[float] = None
    ):
        """Log investigation event."""
        self.info(
            f"Investigation {action}: {investigation_id}",
            event_type=LogEventType.INVESTIGATION,
            operation=f"investigation_{action}",
            duration_ms=duration_ms,
            investigation_id=investigation_id,
            query=query,
            confidence_score=confidence_score
        )
    
    def log_agent_task(
        self,
        agent_name: str,
        task_type: str,
        action: str,
        duration_ms: Optional[float] = None,
        success: bool = True,
        error_message: Optional[str] = None
    ):
        """Log agent task execution."""
        level = LogLevel.INFO if success else LogLevel.ERROR
        message = f"Agent {agent_name} {action} {task_type}"
        
        if success:
            self.info(
                message,
                event_type=LogEventType.AGENT,
                operation=f"agent_{action}",
                duration_ms=duration_ms,
                agent_name=agent_name,
                task_type=task_type,
                success=success
            )
        else:
            self.error(
                message,
                event_type=LogEventType.AGENT,
                operation=f"agent_{action}",
                duration_ms=duration_ms,
                agent_name=agent_name,
                task_type=task_type,
                success=success,
                error_message=error_message
            )
    
    def log_anomaly(
        self,
        anomaly_type: str,
        severity: str,
        confidence_score: float,
        data_source: str,
        description: str,
        investigation_id: Optional[str] = None
    ):
        """Log anomaly detection."""
        self.warning(
            f"Anomaly detected: {anomaly_type}",
            event_type=LogEventType.ANOMALY,
            operation="anomaly_detection",
            anomaly_type=anomaly_type,
            severity=severity,
            confidence_score=confidence_score,
            data_source=data_source,
            description=description,
            investigation_id=investigation_id
        )
    
    def log_database_operation(
        self,
        operation: str,
        table: str,
        duration_ms: float,
        rows_affected: Optional[int] = None,
        success: bool = True
    ):
        """Log database operation."""
        level = LogLevel.DEBUG if success else LogLevel.ERROR
        message = f"Database {operation} on {table}"
        
        if success:
            self.debug(
                message,
                event_type=LogEventType.DATABASE,
                operation=f"db_{operation}",
                duration_ms=duration_ms,
                table=table,
                rows_affected=rows_affected,
                success=success
            )
        else:
            self.error(
                message,
                event_type=LogEventType.DATABASE,
                operation=f"db_{operation}",
                duration_ms=duration_ms,
                table=table,
                rows_affected=rows_affected,
                success=success
            )
    
    def log_cache_operation(
        self,
        operation: str,
        cache_key: str,
        hit: Optional[bool] = None,
        duration_ms: Optional[float] = None,
        cache_type: str = "redis"
    ):
        """Log cache operation."""
        self.debug(
            f"Cache {operation}: {cache_key}",
            event_type=LogEventType.CACHE,
            operation=f"cache_{operation}",
            duration_ms=duration_ms,
            cache_key=cache_key,
            cache_hit=hit,
            cache_type=cache_type
        )
    
    def log_external_api(
        self,
        service_name: str,
        endpoint: str,
        method: str,
        status_code: int,
        duration_ms: float,
        success: bool = True
    ):
        """Log external API call."""
        level = LogLevel.INFO if success else LogLevel.ERROR
        message = f"External API {method} {service_name}{endpoint} - {status_code}"
        
        if success:
            self.info(
                message,
                event_type=LogEventType.EXTERNAL_API,
                operation="external_api_call",
                duration_ms=duration_ms,
                service_name=service_name,
                endpoint=endpoint,
                method=method,
                status_code=status_code,
                success=success
            )
        else:
            self.error(
                message,
                event_type=LogEventType.EXTERNAL_API,
                operation="external_api_call",
                duration_ms=duration_ms,
                service_name=service_name,
                endpoint=endpoint,
                method=method,
                status_code=status_code,
                success=success
            )
    
    def log_performance(
        self,
        operation: str,
        duration_ms: float,
        threshold_ms: float = 1000.0,
        **metrics
    ):
        """Log performance metrics."""
        is_slow = duration_ms > threshold_ms
        level = LogLevel.WARNING if is_slow else LogLevel.DEBUG
        message = f"Performance: {operation} took {duration_ms:.2f}ms"
        
        if is_slow:
            self.warning(
                message,
                event_type=LogEventType.PERFORMANCE,
                operation=operation,
                duration_ms=duration_ms,
                threshold_ms=threshold_ms,
                slow_operation=True,
                **metrics
            )
        else:
            self.debug(
                message,
                event_type=LogEventType.PERFORMANCE,
                operation=operation,
                duration_ms=duration_ms,
                threshold_ms=threshold_ms,
                slow_operation=False,
                **metrics
            )


def get_structured_logger(
    name: str,
    component: Optional[str] = None
) -> StructuredLogger:
    """
    Get a structured logger instance.
    
    Args:
        name: Logger name
        component: Component name
        
    Returns:
        StructuredLogger instance
    """
    return StructuredLogger(name, component=component)


import os  # Add missing import