This article provides a comprehensive guide on how to save logs in Python using the built-in logging module. It walks you through the process of setting up a log file, configuring logging levels, and customizing log formats to suit your needs. Whether you’re debugging or tracking application behavior, these techniques will help you effectively manage your logs and gain better insights into your Python applications.
Suppose you need to save some information about an event and when it happened to a log file. One of the most appropriate file formats for this is JSON.
JSON (JavaScript Object Notation) is a lightweight format that is used everywhere to exchange data. It is based on a subset of the JavaScript language (the way JavaScript builds objects). As noted in MDN, some JavaScript is not JSON, and some JSON is not JavaScript.
An example of where this format is used very extensively is in web service responses. In the “old” days, web services used XML as the primary format for transferring data, but since JSON came along (the JSON format is defined in RFC 4627 by Douglas Crockford), it has become the preferred format because it is much lighter.
You can find a lot more information on the official JSON website and in this excellent answer on StackOverflow.
Here’s an example function to save information to a log in Python. We will call it at the time of an event, and it will save all the necessary information to the log.
def write_to_log(file_object, event_name, description, value):
"""Write message to a log file"""
event_time = str(datetime.datetime.now())
data = OrderedDict()
data['time'] = event_time
data['event'] = event_name
data['details'] = description
data['value'] = value
json.dump(data, file_object, separators=(', ', ':'))
file_object.write('\n')
First, we need to open the log file in order to add information to it. We will use the log file augment mode and for this we will use the checkbox “a” - append.
LOG = open("log.txt", "a", encoding='utf-8')
Then we record information about the event in the log:
for i in range(5):
EVENT_NAME = 'EVENT_' + str(i)
WHAT_HAPPENED = 'HAPPENED_' + str(i)
SOME_VALUE = i
write_to_log(LOG, EVENT_NAME, WHAT_HAPPENED, SOME_VALUE)
This will create a nicely formatted log file:
{"time":"2019-11-03 13:38:56.822216", "event":"EVENT_0", "details":"HAPPENED_0", "value":0}
{"time":"2019-11-03 13:38:56.822317", "event":"EVENT_1", "details":"HAPPENED_1", "value":1}
{"time":"2019-11-03 13:38:56.822369", "event":"EVENT_2", "details":"HAPPENED_2", "value":2}
{"time":"2019-11-03 13:38:56.822410", "event":"EVENT_3", "details":"HAPPENED_3", "value":3}
{"time":"2019-11-03 13:38:56.822449", "event":"EVENT_4", "details":"HAPPENED_4", "value":4}
I hope this helps you.
One email when there's a new post.