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

Invalid format string error when starting the server on Windows #556

Description

@protokoul

Describe the issue

Hi. I am setting up the project locally on Windows. When I start the server, it returns an error stating ValueError: Invalid format string. Error screenshot pasted below

Image

Steps to Reproduce

Set up the project locally referring to these steps on Windows and run the command codegate serve

Operating System

Microsoft Windows (Intel)

IDE and Version

N/A

Extension and Version

N/A

Provider

Other

Model

N/A

Logs

Traceback (most recent call last):
File "", line 198, in run_module_as_main
File "", line 88, in run_code
File "E:\github_forks\codegate-fork\venv\Scripts\codegate.exe_main
.py", line 4, in
from codegate.cli import main
File "E:\github_forks\codegate-fork\src\codegate\cli.py", line 20, in
from codegate.providers.copilot.provider import CopilotProvider
File "E:\github_forks\codegate-fork\src\codegate\providers\copilot_init
.py", line 1, in
from codegate.providers.copilot.provider import CopilotProvider
File "E:\github_forks\codegate-fork\src\codegate\providers\copilot\provider.py", line 26, in
setup_logging()
~~~~~~~~~~~~~^^
File "E:\github_forks\codegate-fork\src\codegate\codegate_logging.py", line 155, in setup_logging
logger.debug(
~~~~~~~~~~~~^
"Logging initialized",
^^^^^^^^^^^^^^^^^^^^^^
...<4 lines>...
},
^^
)
^
File "E:\github_forks\codegate-fork\venv\Lib\site-packages\structlog_native.py", line 134, in meth
return self._proxy_to_logger(name, event, **kw)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "E:\github_forks\codegate-fork\venv\Lib\site-packages\structlog_base.py", line 214, in _proxy_to_logger
args, kw = self._process_event(method_name, event, event_kw)
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\github_forks\codegate-fork\venv\Lib\site-packages\structlog_base.py", line 165, in _process_event
event_dict = proc(self._logger, method_name, event_dict)
File "E:\github_forks\codegate-fork\venv\Lib\site-packages\structlog\processors.py", line 490, in call
return self._stamper(event_dict)
~~~~~~~~~~~~~^^^^^^^^^^^^
File "E:\github_forks\codegate-fork\venv\Lib\site-packages\structlog\processors.py", line 551, in stamper_fmt
event_dict[key] = now().strftime(fmt)
~~~~~~~~~~~~~~^^^^^
ValueError: Invalid format string

Additional Context

The issue is due to '.%03dZ' in the custom datetime format "%Y-%m-%dT%H:%M:%S.%03dZ" used in the function setup_logging in src/codegate/codegate_logging.py.

This format gets passed to the function stamper_fmt() inside the class structlog.processors.TimeStamper. stamper_fmt() uses strftime() which works fine on Linux, but on Windows it returns an invalid format string error. Below is the result of using the same format in Ubuntu and Windows

Image

Activity

  1. lukehinds commented on Jan 12, 2025

    @lukehinds

    Hey @protokoul , thanks for the detailed transaction triage. Would you you like to PR this? I guess the main aspect will be OS scrutiny , but I am sure there is a python lib for this. The other option is we try except and if the Linux format fails, try the windows one?

  2. protokoul commented on Jan 12, 2025

    @protokoul
    ContributorAuthor

    Sure @lukehinds, I will work on a PR for this. I thought about going with the try-except approach or something like checking the OS version using if-else and then assigning the datetime format accordingly in the structlog configuration, then I felt it's better to raise an issue and ask for more opinions to see if there is a preferred approach here.

    I would also like to understand the use of %03d in the format specified. I see it adds the date padded with zeroes at the end of the date time but I didn't understand why is it used.

  3. lukehinds commented on Jan 12, 2025

    @lukehinds

    Looked into this @protokoul and I think we can take a different approach. The 03d is represents 3 digit milliseconds, and uses the c std library (which might explain why its ok on Linux). I think for Python we can do this instead:

    shared_processors = [
        structlog.processors.add_log_level,
        structlog.processors.TimeStamper(fmt="%Y-%m-%dT%H:%M:%S.%f", utc=True),
        add_origin,
        structlog.processors.CallsiteParameterAdder(
            [
                structlog.processors.CallsiteParameter.MODULE,
                structlog.processors.CallsiteParameter.PATHNAME,
            ]
        ),
    ]
    

    Try that out on window and I will check the same on Linux.

  4. protokoul commented on Jan 12, 2025

    @protokoul
    ContributorAuthor

    Thanks. I have tried the format you shared. I also append 'Z' at the end like this '%Y-%m-%dT%H:%M:%S.%fZ' and it does not return the invalid format string error now so this works, but the server does not start in Windows due to another exception probably at fastapi/uvicorn level which seems unrelated to the current issue. I am still working on setting up the project locally so I may need some time to understand what causes this other exception that I see. Let me know if I should create a PR for the current issue first.

  5. lukehinds commented on Jan 12, 2025

    @lukehinds

    hey @protokoul , go ahead make the PR and I will test this side.

    please do open another issue for the other issue and happy to try and help debug.

  6. protokoul commented on Jan 12, 2025

    @protokoul
    ContributorAuthor

    @lukehinds About the 03d in the previous format, I don't think that represented 3 digit millisecond. I ran the command multiple times in Ubuntu and I see that the last 3 digits is always 012, which is today's date with padded zero. Not really that important but thought I should share this anyway just to make sure if I understood this correctly.

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions