|
1 | 1 | """ |
2 | | -This module provides functions for Google Cloud Logging handlers, |
3 | | -properly isolates Cloud-specific dependencies to production/staging |
| 2 | +Uses Google's setup_logging() which automatically: |
| 3 | +- Detects Django framework |
| 4 | +- Extracts X-Cloud-Trace-Context headers from requests |
| 5 | +- Adds trace/spanId to all log entries |
| 6 | +- Groups logs by request in Cloud Logging |
| 7 | +
|
| 8 | +References: |
| 9 | +- https://cloud.google.com/python/docs/reference/logging/latest/auto-trace-span-extraction |
| 10 | +- https://cloud.google.com/trace/docs/trace-log-integration |
| 11 | +- https://docs.cloud.google.com/python/docs/reference/logging/latest/client |
| 12 | +- https://github.com/googleapis/python-logging/blob/main/google/cloud/logging_v2/handlers/handlers.py |
4 | 13 | """ |
5 | 14 |
|
6 | 15 | import os |
7 | 16 | import logging |
8 | 17 |
|
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
9 | 20 |
|
10 | | -def get_cloud_logging_handler(): |
| 21 | +def setup_cloud_logging(): |
11 | 22 | """ |
12 | | - 1. Checks if Cloud Logging is enabled (USE_GCLOUD_LOGGING env var) |
13 | | - 2. Imports google-cloud-logging packages ONLY if enabled |
14 | | - 3. Returns configured handler with trace correlation filter |
15 | | - |
| 23 | + Initialize Google Cloud Logging with automatic trace correlation. |
| 24 | +
|
16 | 25 | Returns: |
17 | | - logging.Handler: CloudLoggingHandler configured with custom logName and |
18 | | - trace filter, or NullHandler if Cloud Logging is disabled |
19 | | - |
| 26 | + bool: True if Cloud Logging was successfully initialized, False if not |
| 27 | +
|
20 | 28 | Environment Variables: |
21 | 29 | USE_GCLOUD_LOGGING: Set to "1" to enable Cloud Logging |
22 | 30 | GOOGLE_CLOUD_PROJECT: GCP project ID (auto-detected on Cloud Run) |
23 | 31 | """ |
24 | | - |
25 | 32 | if os.environ.get('USE_GCLOUD_LOGGING', '0') != '1': |
26 | | - return logging.NullHandler() |
27 | | - |
28 | | - # Import Google Cloud packages only when Cloud Logging is enabled |
29 | | - # This ensures dev/CI/test environments never import these packages |
| 33 | + logger.info( |
| 34 | + "Cloud Logging disabled (USE_GCLOUD_LOGGING != 1)" |
| 35 | + ) |
| 36 | + return False |
| 37 | + |
30 | 38 | try: |
31 | | - from google.cloud.logging import Client as CloudLoggingClient |
32 | | - from google.cloud.logging.handlers import CloudLoggingHandler |
33 | | - from testbed.core.utils.logging_filters import CloudRunTraceFilter |
34 | | - |
35 | | - client = CloudLoggingClient() |
36 | | - |
37 | | - cloud_logging_handler = CloudLoggingHandler( |
38 | | - client, |
39 | | - name="testbed" |
| 39 | + import google.cloud.logging |
| 40 | + |
| 41 | + client = google.cloud.logging.Client() |
| 42 | + |
| 43 | + client.setup_logging(log_level=logging.INFO) |
| 44 | + |
| 45 | + logger.info( |
| 46 | + "Cloud Logging initialized with automatic trace correlation" |
40 | 47 | ) |
41 | | - |
42 | | - cloud_logging_handler.addFilter(CloudRunTraceFilter()) |
43 | | - |
44 | | - return cloud_logging_handler |
45 | | - |
| 48 | + return True |
| 49 | + |
46 | 50 | except Exception as e: |
47 | | - logger = logging.getLogger(__name__) |
48 | 51 | logger.warning( |
49 | 52 | f"Failed to initialize Cloud Logging: {e}. " |
50 | | - "Falling back to NullHandler. Logs will not appear in Cloud Logging." |
| 53 | + "Falling back to console logging." |
51 | 54 | ) |
52 | | - return logging.NullHandler() |
| 55 | + return False |
0 commit comments