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

Add a new env:// value converter for logging configuration #124955

Open
edgarrmondragon opened this issue Oct 4, 2024 · 1 comment
Open

Add a new env:// value converter for logging configuration #124955

edgarrmondragon opened this issue Oct 4, 2024 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@edgarrmondragon
Copy link
Contributor

edgarrmondragon commented Oct 4, 2024

Feature or enhancement

Proposal:

It would be nice if Python logging config processed environment variables in a similar way cfg:// and ext:// are at the moment. For example:

import logging
import logging.config

config = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "simple": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },
    "handlers": {
        "console": {
            "class": "logging.FileHandler",
            "level": "DEBUG",
            "formatter": "simple",
            "filename": "env://LOG_FILE",  # <---- this!
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": ["console"],
    },
}

logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
logger.info("Hello, world!")

Something like this can already be implemented by users if they subclass logging.config.DictConfigurator:

import os
from logging.config import DictConfigurator


class DictConfiguratorWithEnv(DictConfigurator):
    value_converters = {
        "env": "env_convert",
        **DictConfigurator.value_converters,
    }

    def env_convert(self, value):
        return os.environ[value]

However, it'd be nice if Python had built-in support and it might be useful for configuring things like host and port for socket handlers.

I think this is a minor change if accepted, but folks might have thoughts about why this could be a bad idea.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@edgarrmondragon edgarrmondragon added the type-feature A feature request or enhancement label Oct 4, 2024
@ericvsmith
Copy link
Member

Feature requests should be discussed at https://discuss.python.org/c/ideas/6 before opening an issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants