# Python logging improvements, mainly for containers

**URL:** <https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363>\
**Category:** Ideas\
**Tags:** logging\
**Created:** [October 9, 2024, 10:49pm UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363 "2024-10-09T22:49:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haigutus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/haigutus/32/23212_2.png) [@Haigutus](https://discuss.python.org/u/Haigutus)\
**Post date:** [October 9, 2024, 10:49pm UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363/1 "2024-10-09T22:49:18Z")

</div>

I would argue these changes would make python based app deployment easier and reduce development effort for supporting specific deployments.

1. Add native support for json logging
2. Natively support iso time format and utc time
3. Enable to overwrite logging configuration via ENV variable(s)

At the moment one needs to use cutom logger to get json format or write log parsers to get structured logs.

At the moment developer needs to foresee the need of logging to be configurable, while the need to configure logging is usually at deployment, thus it would make sense to have standard way, without modification to source code, to override logging configuration for any python app/script using the standard logger.

---

<div class="post-metadata">

**Author:** ![sirosen](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sirosen/32/8478_2.png) [@sirosen](https://discuss.python.org/u/sirosen)\
**Post date:** [October 10, 2024, 12:39am UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363/2 "2024-10-10T00:39:12Z")

</div>

I’d be interested in a stdlib way to get JSON-formatted logs, and I think containers deployed in cloud services are a good motivating use case – several services will stream stdout to some log destination like an ELK stack.

That said, I think it’s better to focus on one idea, rather than multiple. Some people will agree with one part but not others. For example, I think a JSON formatter would be interesting, but I don’t agree that this is a problem:

> [@Haigutus](#):
>
> At the moment developer needs to foresee the need of logging to be configurable

Yep! Being a developer often requires thought and foresight! Sometimes, you even make mistakes and need to change things!

* * *

JSON formatted output makes the most sense to me as a formatter. I’m not sure what pitfalls there might be in that approach, but it seems to me that it would be a little bit fiddly to configure the fields to emit, the encoder to use (can I plug and play `orjson`?), and handling of `extra`. If it can do all of that and pipe through a stream handler, it would be nifty.

---

<div class="post-metadata">

**Author:** ![Haigutus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/haigutus/32/23212_2.png) [@Haigutus](https://discuss.python.org/u/Haigutus)\
**Post date:** [October 10, 2024, 1:37am UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363/3 "2024-10-10T01:37:56Z")

</div>

On the env overriding config.

It is mostly from Deployment side of things, same code will be deployed in different env, organizations etc. I think it would be nice, if there was one straight forward way how to configure python app logging. At the moment it is either hardcoded, some part made configurable by env variables or by using the config json (the best option in my view).

Thus I think it would be nice to have somthing like PYTHON\_LOGGING\_CONFIG = path to config json

That would owerride/set the logging module configurations.

Giving standard way to deploy python based apps

---

<div class="post-metadata">

**Author:** ![sirosen](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sirosen/32/8478_2.png) [@sirosen](https://discuss.python.org/u/sirosen)\
**Post date:** [October 10, 2024, 4:36am UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363/4 "2024-10-10T04:36:22Z")

</div>

I don’t think this env var idea is likely to be worth implementing. Sure, you can add a system to do this, but it will only work if the application doesn’t configure logging and thus blow away those settings.  
If you build those sorts of capabilities into your application (which is easy to do and does not require a stdlib change), then you’ll have much finer grained control.  
My opinion is that you already need your application and infrastructure to be harmonious, and adding another way to put configuration data into the system doesn’t change that.

If you want to pursue this idea, I think you need an explicit proposal for a new method in `logging.config` which loads this configuration. That way, the time at which the config is loaded is well defined, and it’s possible to execute code before and after that step. Without an explicit hook which loads the config, I don’t think the contract is sufficiently clear.

---

<div class="post-metadata">

**Author:** ![Haigutus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/haigutus/32/23212_2.png) [@Haigutus](https://discuss.python.org/u/Haigutus)\
**Post date:** [October 10, 2024, 5:53am UTC](https://discuss.python.org/t/python-logging-improvements-mainly-for-containers/67363/5 "2024-10-10T05:53:34Z")

</div>

Maybe env is not a good solution, the idea is that there is good and stable configuration API for python logging. I think current json file is good, just additional way to enforce it is needed (instead what is in code). This way one could take existing python app and deploy it for example with structural json logging without modifications to source code.

Something like [Configuration file :: Apache Log4j](https://logging.apache.org/log4j/2.x/manual/configuration.html)

Or

> **[Logging in C# - .NET](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#configure-logging-without-code)**
>
> Learn about app logging provided by the Microsoft.Extensions.Logging NuGet package in C#.

Or

> **[env\_logger - Rust](https://docs.rs/env_logger/latest/env_logger/)**
>
> A simple logger that can be configured via environment variables, for use with the logging facade exposed by the \`log\` crate.

Or

[https://logging.apache.org/log4php/docs/configuration.html](https://logging.apache.org/log4php/docs/configuration.html)
