Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need clear documentation for configuration #214

Open
pythonhubdev opened this issue Apr 14, 2024 · 1 comment
Open

Need clear documentation for configuration #214

pythonhubdev opened this issue Apr 14, 2024 · 1 comment

Comments

@pythonhubdev
Copy link

Hi, I have started using hypercorn very recently to move away from uvicorn or gunicorn there are certain issues I'm facing while trying to configure it through either a TOML file or a python module.

  • use_reloader was not working when I was trying to use it through python code and serve it
from hypercorn.config import Config as HypercornConfig

class HypercornApplication:
    """
    Custom Hypercorn application.

    This class is used to start Hypercorn with the FastAPI application.
    """

    def __init__(
        self,
        app: FastAPI,
        host: str = settings.host,
        port: int = settings.port,
        workers: int = settings.workers_count,
        reload: bool = settings.reload,
        **kwargs: Any,
    ) -> None:
        self.app = app
        self.host = host
        self.port = port
        self.workers = workers
        self.reload = reload
        self.kwargs = kwargs

    def create_config(self) -> HypercornConfig:
        """
        Create and return a Hypercorn configuration object.
        """
        config = HypercornConfig()
        config.bind = [f"{self.host}:{self.port}"]
        config.workers = self.workers
        config.use_reloader = self.reload
        config.loglevel = logging.getLevelName(logging.ERROR)
        config.accesslog = "-"

        # Additional configuration options can be set here
        for key, value in self.kwargs.items():
            setattr(config, key, value)
        return config

    def run(self) -> None:
        """
        Run the FastAPI application with Hypercorn.
        """
        config = self.create_config()
        logger.info(f"Starting Hypercorn on {self.host}:{self.port}")
        asyncio.run(serve(self.app, config))  # type: ignore
  • The second issue was with logging I couldn't configure the hypercorn log either with a custom logger class or with the config. Nothing worked for me other than the config.loglevel this is the only config that worked for me for the logs. When I tried to configure with TOML file even the loglevel was not getting affected.

Can you provide an example at least one TOML based configuration file and one python file to customise the logger. This will be very useful.

@joshft91
Copy link

The second issue was with logging I couldn't configure the hypercorn log either with a custom logger class or with the config

This is what I'm struggling with right now. I can't seem to find any documentation or examples that outline the proper way to pass a custom logger class. All I'd like to do is change the formatting: https://github.com/pgjones/hypercorn/blob/main/src/hypercorn/logging.py#L41

Is there an easier way to change the default log formatting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants