👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
My current format string is:
formatter = logging.Formatter("%(asctime)s : %(message)s")
and I want to add a new field called app_name
which will have a different value in each script that contains this formatter.
import logging
formatter = logging.Formatter("%(asctime)s %(app_name)s : %(message)s")
syslog.setFormatter(formatter)
logger.addHandler(syslog)
But I"m not sure how to pass that app_name
value to the logger to interpolate into the format string. I can obviously get it to appear in the log message by passing it each time but this is messy.
I"ve tried:
logging.info("Log message", app_name="myapp")
logging.info("Log message", {"app_name", "myapp"})
logging.info("Log message", "myapp")
but none work.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How do I add custom field to Python log format string?, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How do I add custom field to Python log format string?
- Deutsch How do I add custom field to Python log format string?
- Français How do I add custom field to Python log format string?
- Español How do I add custom field to Python log format string?
- Türk How do I add custom field to Python log format string?
- Русский How do I add custom field to Python log format string?
- Português How do I add custom field to Python log format string?
- Polski How do I add custom field to Python log format string?
- Nederlandse How do I add custom field to Python log format string?
- 中文 How do I add custom field to Python log format string?
- 한국어 How do I add custom field to Python log format string?
- 日本語 How do I add custom field to Python log format string?
- हिन्दी How do I add custom field to Python log format string?
Moscow | 2023-01-27
string Python module is always a bit confusing 😭 How do I add custom field to Python log format string? is not the only problem I encountered. I am just not quite sure it is the best method
Warsaw | 2023-01-27
Simply put and clear. Thank you for sharing. How do I add custom field to Python log format string? and other issues with sys Python module was always my weak point 😁. Will get back tomorrow with feedback
Milan | 2023-01-27
Simply put and clear. Thank you for sharing. How do I add custom field to Python log format string? and other issues with StackOverflow was always my weak point 😁. I just hope that will not emerge anymore