Contents

How to save information to a log file in Python

About formats, a little bit about journaling and a Python code example

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.

What 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.

Journaling function

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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')

Example usage

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.

1
LOG = open("log.txt", "a", encoding='utf-8')

Then we record information about the event in the log:

1
2
3
4
5
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:

1
2
3
4
5
{"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.