A way to use stdlib's logging with the new Format Specification Mini-Language.
Project description
foffinf
A way to use stdlib's logging with the new Format Specification Mini-Language.
What?
We know it's bad to do this:
logger.info(f"The result is {result:05d}")
We should never build the log message ourselves, basically because two situations that are avoided by properly using logging:
- performance: if the logging level is below INFO (in the case of the example) the resulting message is not really built
- robustness: if
result
happens to not be a number, that example will crash, but thelogging
infrastructure will just present some error message (and everything will continue)
The recommended way is:
logger.info("The result is %05d", result)
But wait, we're living in the 21st century, are we still using printf-style string formatting?
With foffinf
we can now write:
logger.info("The result is {:05d}", result)
Welcome to the future!
Trust the dino :)
How to use it
Sadly we can not set up the whole logging system in our process to this new format, because most probably we will be using 3rd party libraries that actually have log calls using the old way, and we shall not break them.
So we need to indicate which module (the one we're writing, of course) shall use it. This is done with a foffinf.formatize
call. As is standard to use the module's name, we can just:
import logging
import foffinf
foffinf.formatize(__name__)
logger = logging.getLogger(__name__)
# ...
logger.info("The result is {:05d}", result)
Affecting a specific logger module will not affect its parent or any sibling.
fofinff.formatize("mylib.mod1")
logging.getLogger("mylib.mod1") # affected!
logging.getLogger("mylib") # NOT affected
logging.getLogger("mylib.mod2") # NOT affected
logging.getLogger() # NOT affected
logging.getLogger("otherlib") # NOT affected
logging.getLogger("mylib.mod1.submod") # NOT affected
Note (in that last line) that by default it will neither affect submodules. To affect all children of a logger tree node:
fofinff.formatize("mylib.mod1", scatter=True)
logging.getLogger("mylib.mod1") # affected!
logging.getLogger("mylib") # NOT affected
logging.getLogger("mylib.mod2") # NOT affected
logging.getLogger("mylib.mod1.submod_a") # affected!
logging.getLogger("mylib.mod1.submod_b") # affected!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file foffinf-0.1.tar.gz
.
File metadata
- Download URL: foffinf-0.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29b4b1ad3fb6d6f6449302c89ec5cc2e657943a7f6e9692c5787049aa6308a40 |
|
MD5 | f003b7727a7dec23229e7fa50af04cdd |
|
BLAKE2b-256 | 1a5fc7f77fdadb44756b889928fd422852ebf4bb0410904758a3d3b6e51bc500 |
File details
Details for the file foffinf-0.1-py3-none-any.whl
.
File metadata
- Download URL: foffinf-0.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 892d00747aff46869c72074d7bacb32dd779b9aa4c42cfa397956698b678ac63 |
|
MD5 | 81f7d29705c394a555aad9007983313d |
|
BLAKE2b-256 | 7d16e8bf96b678c0c4df9b6e08409b7908e0f3efa5d1417b1d6bcde447289989 |