Skip to content

Commit

Permalink
Enhance logging.
Browse files Browse the repository at this point in the history
This change configures the logger to print log lines and sets the root
logger to `ERROR` in order to silence chatty libraries. This setting
does not affect application logs since they're overridden a couple
lines later with the desired log level from config.

Additionally, there's a change that is Copilot specific that
synergizes with logging line numbers for easier bug tracking. There's
no fundamental change in business logic.
  • Loading branch information
blkt committed Jan 16, 2025
1 parent 8106c53 commit 1627c13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
12 changes: 5 additions & 7 deletions src/codegate/codegate_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def setup_logging(
[
structlog.processors.CallsiteParameter.MODULE,
structlog.processors.CallsiteParameter.PATHNAME,
structlog.processors.CallsiteParameter.LINENO,
]
),
]
Expand Down Expand Up @@ -136,21 +137,18 @@ def setup_logging(
stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO)
stderr_handler.addFilter(lambda record: record.levelno > logging.INFO)

# Get root logger and configure it
# Get root logger and configure it. By default we drop all
# messages below ERROR to silence chatty libraries.
root_logger = logging.getLogger()
root_logger.setLevel(log_level.value)
root_logger.setLevel(logging.ERROR)

# 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").disabled = True
logging.getLogger("uvicorn.error").disabled = True
logging.getLogger("aiosqlite").disabled = True

# Create a logger for our package
logging.getLogger("codegate").setLevel(log_level.value)
logger = structlog.get_logger("codegate")
logger.debug(
"Logging initialized",
Expand Down
38 changes: 25 additions & 13 deletions src/codegate/providers/copilot/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ async def process_body(
"""Common processing logic for all strategies"""
try:
normalized_body = self.normalizer.normalize(body)
except Exception as e:
logger.error(f"Pipeline processing error: {e}")
return body, None

headers_dict = {}
for header in headers:
try:
name, value = header.split(":", 1)
headers_dict[name.strip().lower()] = value.strip()
except ValueError:
continue
headers_dict = {}
for header in headers:
try:
name, value = header.split(":", 1)
headers_dict[name.strip().lower()] = value.strip()
except ValueError:
continue

try:
result = await self.instance.process_request(
request=normalized_body,
provider=self.provider_name,
Expand All @@ -111,25 +115,33 @@ async def process_body(
extra_headers=CopilotPipeline._get_copilot_headers(headers_dict),
is_copilot=True,
)
except Exception as e:
logger.error(f"Pipeline processing error: {e}")
return body, None

if result.context.shortcut_response:
if result.context.shortcut_response:
try:
# Return shortcut response to the user
body = CopilotPipeline._create_shortcut_response(
result, normalized_body.get("model", "gpt-4o-mini")
)
logger.info(f"Pipeline created shortcut response: {body}")
except Exception as e:
logger.error(f"Pipeline processing error: {e}")
return body, None

elif result.request:
elif result.request:
try:
# the pipeline did modify the request, return to the user
# in the original LLM format
body = self.normalizer.denormalize(result.request)
# Uncomment the below to debug the request
# logger.debug(f"Pipeline processed request: {body}")

return body, result.context
except Exception as e:
logger.error(f"Pipeline processing error: {e}")
return body, None
return body, result.context
except Exception as e:
logger.error(f"Pipeline processing error: {e}")
return body, None


class CopilotFimNormalizer:
Expand Down

0 comments on commit 1627c13

Please sign in to comment.