Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 0bf2a09

Browse files
committed
fix scope of codegate logger
1 parent 17b6836 commit 0bf2a09

File tree

4 files changed

+10
-58
lines changed

4 files changed

+10
-58
lines changed

src/codegate/cli.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Dict, Optional
88

99
import click
10+
import structlog
1011
from uvicorn.config import Config as UvicornConfig
1112
from uvicorn.server import Server
1213

@@ -19,7 +20,6 @@
1920
from codegate.providers.copilot.provider import CopilotProvider
2021
from codegate.server import init_app
2122
from codegate.storage.utils import restore_storage_backup
22-
from codegate.logger.logger import OriginLogger
2323

2424

2525
class UvicornServer:
@@ -33,9 +33,7 @@ def __init__(self, config: UvicornConfig, server: Server):
3333
self._startup_complete = asyncio.Event()
3434
self._shutdown_event = asyncio.Event()
3535
self._should_exit = False
36-
37-
logger_obj = OriginLogger("generic_server")
38-
self.logger = logger_obj.logger
36+
self.logger = structlog.get_logger("codegate").bind(origin="generic_server")
3937

4038
async def serve(self) -> None:
4139
"""Start the uvicorn server and handle shutdown gracefully."""
@@ -86,9 +84,7 @@ async def cleanup(self) -> None:
8684

8785
def validate_port(ctx: click.Context, param: click.Parameter, value: int) -> int:
8886
"""Validate the port number is in valid range."""
89-
cli_logger_obj = OriginLogger("cli")
90-
cli_logger = cli_logger_obj.logger
91-
87+
cli_logger = structlog.get_logger("codegate").bind(origin="cli")
9288
cli_logger.debug(f"Validating port number: {value}")
9389
if value is not None and not (1 <= value <= 65535):
9490
raise click.BadParameter("Port must be between 1 and 65535")
@@ -300,8 +296,7 @@ def serve(
300296

301297
# Set up logging first
302298
setup_logging(cfg.log_level, cfg.log_format)
303-
cli_logger_obj = OriginLogger("cli")
304-
logger = cli_logger_obj.logger
299+
logger = structlog.get_logger("codegate").bind(origin="cli")
305300

306301
init_db_sync(cfg.db_path)
307302

@@ -332,9 +327,7 @@ def serve(
332327
click.echo(f"Configuration error: {e}", err=True)
333328
sys.exit(1)
334329
except Exception as e:
335-
cli_logger_obj = OriginLogger("cli")
336-
logger = cli_logger_obj.logger
337-
330+
logger = structlog.get_logger("codegate").bind(origin="cli")
338331
logger.exception("Unexpected error occurred")
339332
click.echo(f"Error: {e}", err=True)
340333
sys.exit(1)
@@ -343,9 +336,7 @@ def serve(
343336
async def run_servers(cfg: Config, app) -> None:
344337
"""Run the codegate server."""
345338
try:
346-
cli_logger_obj = OriginLogger("cli")
347-
logger = cli_logger_obj.logger
348-
339+
logger = structlog.get_logger("codegate").bind(origin="cli")
349340
logger.info(
350341
"Starting server",
351342
extra={

src/codegate/logger/logger.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/codegate/providers/copilot/provider.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import asyncio
22
import re
33
import ssl
4+
from src.codegate.codegate_logging import setup_logging
5+
import structlog
46
from dataclasses import dataclass
57
from typing import Dict, List, Optional, Tuple, Union
68
from urllib.parse import unquote, urljoin, urlparse
79

8-
from src.codegate.logger.logger import OriginLogger
910
from litellm.types.utils import Delta, ModelResponse, StreamingChoices
1011

1112
from codegate.ca.codegate_ca import CertificateAuthority
@@ -22,8 +23,8 @@
2223
)
2324
from codegate.providers.copilot.streaming import SSEProcessor
2425

25-
logger_obj = OriginLogger("copilot_proxy")
26-
logger = logger_obj.logger
26+
setup_logging()
27+
logger = structlog.get_logger("codegate").bind(origin="copilot_proxy")
2728

2829
# Constants
2930
MAX_BUFFER_SIZE = 10 * 1024 * 1024 # 10MB

tests/test_cli.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,8 @@ def test_serve_default_options(cli_runner):
195195
# Use patches for run_servers and logging setup
196196
with (
197197
patch("src.codegate.cli.run_servers") as mock_run,
198-
patch("src.codegate.cli.OriginLogger") as mock_origin_logger,
199198
patch("src.codegate.cli.setup_logging") as mock_setup_logging,
200199
):
201-
202-
logger_instance = MagicMock()
203-
mock_origin_logger.return_value = logger_instance
204-
205200
# Invoke the CLI command
206201
result = cli_runner.invoke(cli, ["serve"])
207202

@@ -211,9 +206,6 @@ def test_serve_default_options(cli_runner):
211206
# Check if the logging setup was called with expected defaults
212207
mock_setup_logging.assert_called_once_with(LogLevel.INFO, LogFormat.JSON)
213208

214-
# Check if logging was done correctly
215-
mock_origin_logger.assert_called_with("cli")
216-
217209
# Validate run_servers was called once
218210
mock_run.assert_called_once()
219211

@@ -222,13 +214,8 @@ def test_serve_custom_options(cli_runner):
222214
"""Test serve command with custom options."""
223215
with (
224216
patch("src.codegate.cli.run_servers") as mock_run,
225-
patch("src.codegate.cli.OriginLogger") as mock_origin_logger,
226217
patch("src.codegate.cli.setup_logging") as mock_setup_logging,
227218
):
228-
229-
logger_instance = MagicMock()
230-
mock_origin_logger.return_value = logger_instance
231-
232219
# Invoke the CLI command with custom options
233220
result = cli_runner.invoke(
234221
cli,
@@ -261,9 +248,6 @@ def test_serve_custom_options(cli_runner):
261248
# Assert logging setup was called with the provided log level and format
262249
mock_setup_logging.assert_called_once_with(LogLevel.DEBUG, LogFormat.TEXT)
263250

264-
# Assert logger got called with the expected module name
265-
mock_origin_logger.assert_called_with("cli")
266-
267251
# Validate run_servers was called once
268252
mock_run.assert_called_once()
269253
# Retrieve the actual Config object passed to run_servers
@@ -322,20 +306,14 @@ def test_serve_with_config_file(cli_runner, temp_config_file):
322306
"""Test serve command with config file."""
323307
with (
324308
patch("src.codegate.cli.run_servers") as mock_run,
325-
patch("src.codegate.cli.OriginLogger") as mock_origin_logger,
326309
patch("src.codegate.cli.setup_logging") as mock_setup_logging,
327310
):
328-
329-
logger_instance = MagicMock()
330-
mock_origin_logger.return_value = logger_instance
331-
332311
# Invoke the CLI command with the configuration file
333312
result = cli_runner.invoke(cli, ["serve", "--config", str(temp_config_file)])
334313

335314
# Assertions to ensure the CLI ran successfully
336315
assert result.exit_code == 0
337316
mock_setup_logging.assert_called_once_with(LogLevel.DEBUG, LogFormat.JSON)
338-
mock_origin_logger.assert_called_with("cli")
339317

340318
# Validate that run_servers was called with the expected configuration
341319
mock_run.assert_called_once()
@@ -370,13 +348,8 @@ def test_serve_priority_resolution(cli_runner: CliRunner, temp_config_file: Path
370348
with (
371349
patch.dict(os.environ, {"LOG_LEVEL": "INFO", "PORT": "9999"}, clear=True),
372350
patch("src.codegate.cli.run_servers") as mock_run,
373-
patch("src.codegate.cli.OriginLogger") as mock_origin_logger,
374351
patch("src.codegate.cli.setup_logging") as mock_setup_logging,
375352
):
376-
# Set up mock logger
377-
logger_instance = MagicMock()
378-
mock_origin_logger.return_value = logger_instance
379-
380353
# Execute CLI command with specific options overriding environment and config file settings
381354
result = cli_runner.invoke(
382355
cli,
@@ -410,7 +383,6 @@ def test_serve_priority_resolution(cli_runner: CliRunner, temp_config_file: Path
410383

411384
# Ensure logging setup was called with the highest priority settings (CLI arguments)
412385
mock_setup_logging.assert_called_once_with("ERROR", "TEXT")
413-
mock_origin_logger.assert_called_with("cli")
414386

415387
# Verify that the run_servers was called with the overridden settings
416388
config_arg = mock_run.call_args[0][0] # Assuming Config is the first positional arg
@@ -438,13 +410,8 @@ def test_serve_certificate_options(cli_runner: CliRunner) -> None:
438410
"""Test serve command with certificate options."""
439411
with (
440412
patch("src.codegate.cli.run_servers") as mock_run,
441-
patch("src.codegate.cli.OriginLogger") as mock_origin_logger,
442413
patch("src.codegate.cli.setup_logging") as mock_setup_logging,
443414
):
444-
# Set up mock logger
445-
logger_instance = MagicMock()
446-
mock_origin_logger.return_value = logger_instance
447-
448415
# Execute CLI command with certificate options
449416
result = cli_runner.invoke(
450417
cli,
@@ -468,7 +435,6 @@ def test_serve_certificate_options(cli_runner: CliRunner) -> None:
468435

469436
# Ensure logging setup was called with expected arguments
470437
mock_setup_logging.assert_called_once_with("INFO", "JSON")
471-
mock_origin_logger.assert_called_with("cli")
472438

473439
# Verify that run_servers was called with the provided certificate options
474440
config_arg = mock_run.call_args[0][0] # Assuming Config is the first positional arg

0 commit comments

Comments
 (0)