forked from stacklok/codegate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegate_logging.py
More file actions
147 lines (124 loc) · 4.93 KB
/
codegate_logging.py
File metadata and controls
147 lines (124 loc) · 4.93 KB
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
import logging
import sys
from enum import Enum
from typing import Optional
import structlog
class LogLevel(str, Enum):
"""Valid log levels."""
ERROR = "ERROR"
WARNING = "WARNING"
INFO = "INFO"
DEBUG = "DEBUG"
@classmethod
def _missing_(cls, value: str) -> Optional["LogLevel"]:
"""Handle case-insensitive lookup of enum values."""
try:
# Convert to uppercase and look up directly
return cls[value.upper()]
except (KeyError, AttributeError):
raise ValueError(
f"'{value}' is not a valid LogLevel. "
f"Valid levels are: {', '.join(level.value for level in cls)}"
)
class LogFormat(str, Enum):
"""Valid log formats."""
JSON = "JSON"
TEXT = "TEXT"
@classmethod
def _missing_(cls, value: str) -> Optional["LogFormat"]:
"""Handle case-insensitive lookup of enum values."""
try:
# Convert to uppercase and look up directly
return cls[value.upper()]
except (KeyError, AttributeError):
raise ValueError(
f"'{value}' is not a valid LogFormat. "
f"Valid formats are: {', '.join(format.value for format in cls)}"
)
def setup_logging(
log_level: Optional[LogLevel] = None, log_format: Optional[LogFormat] = None
) -> logging.Logger:
"""Configure the logging system.
Args:
log_level: The logging level to use. Defaults to INFO if not specified.
log_format: The log format to use. Defaults to JSON if not specified.
This configures two handlers:
- stderr_handler: For ERROR, CRITICAL, and WARNING messages
- stdout_handler: For INFO and DEBUG messages
"""
if log_level is None:
log_level = LogLevel.INFO
if log_format is None:
log_format = LogFormat.JSON
# The configuration was taken from structlog documentation
# https://www.structlog.org/en/stable/standard-library.html
# Specifically the section "Rendering Using structlog-based Formatters Within logging"
# Adds log level and timestamp to log entries
shared_processors = [
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="%Y-%m-%dT%H:%M:%S.%03dZ", utc=True),
structlog.processors.CallsiteParameterAdder(
[
structlog.processors.CallsiteParameter.MODULE,
]
),
]
# Not sure why this is needed. I think it is a wrapper for the standard logging module.
# Should allow to log both with structlog and the standard logging module:
# import logging
# import structlog
# logging.getLogger("stdlog").info("woo")
# structlog.get_logger("structlog").info("amazing", events="oh yes")
structlog.configure(
processors=shared_processors
+ [
# Prepare event dict for `ProcessorFormatter`.
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
# The config aboves adds the following keys to all log entries: _record & _from_structlog.
# remove_processors_meta removes them.
processors = shared_processors + [structlog.stdlib.ProcessorFormatter.remove_processors_meta]
# Choose the processors based on the log format
if log_format == LogFormat.JSON:
processors = processors + [
structlog.processors.dict_tracebacks,
structlog.processors.JSONRenderer(),
]
else:
processors = processors + [structlog.dev.ConsoleRenderer()]
formatter = structlog.stdlib.ProcessorFormatter(
# foreign_pre_chain run ONLY on `logging` entries that do NOT originate within structlog.
foreign_pre_chain=shared_processors,
processors=processors,
)
# Create handlers for stdout and stderr
stdout_handler = logging.StreamHandler(sys.stdout)
stderr_handler = logging.StreamHandler(sys.stderr)
# Set formatters
stdout_handler.setFormatter(formatter)
stderr_handler.setFormatter(formatter)
# Configure log routing
stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO)
stderr_handler.addFilter(lambda record: record.levelno > logging.INFO)
# Get root logger and configure it
root_logger = logging.getLogger()
root_logger.setLevel(log_level.value)
# Remove any existing handlers and add our new ones
root_logger.handlers.clear()
root_logger.addHandler(stdout_handler)
root_logger.addHandler(stderr_handler)
# Set explicitly the log level for other modules
logging.getLogger("sqlalchemy").setLevel(logging.WARNING)
# Create a logger for our package
logger = structlog.get_logger("codegate")
logger.debug(
"Logging initialized",
extra={
"log_level": log_level.value,
"log_format": log_format.value,
"handlers": ["stdout", "stderr"],
},
)