Reputation: 429
How can I setup python's module logging
so that every line it prints to the file starts with a specific string?
Upvotes: 0
Views: 308
Reputation: 1177
You can do this via the log formatter. Here's a simple example configuring it via logging.basicConfig
:
import logging
logging.basicConfig(format="Hello at %(asctime)s %(message)s: %(levelname)s")
logging.warning("world")
Upvotes: 1
Reputation: 10406
Using python logging
Formatting the Output
You can pass any variable that can be represented as a string from your program as a message to your logs. If you for example want to log the process ID along with the level and message, you can do something like this:
import logging
logging.basicConfig(format='%(process)d-%(levelname)s-%(message)s')
logging.warning('This is a Warning')
# Output:
# 18472-WARNING-This is a Warning
For further details see documentation on realpython.
Upvotes: 0