Python Log Messages With Dynamic Formatter Variables

In this post I will explain how it is possible to replace logger formatter variables with dynamic values. Take the following log message for example:

[2020-04-02 15:37:01] myapp INFO: Logged in successfully.

What if we wanted to add some additional data after the INFO part, but this data is dynamic, or is only available at runtime. Like a username or an app name for example.

Let's take a look at how to achieve this.


Django: ORing A List of Q Objects

Django allows using the OR bitwise operator | with Q objects for more complex lookups:

Q(question__startswith='Who') | Q(question__startswith='What')

However this requires explicitely typing each Q object. How can we apply this OR opperation to all the Q objects in a list?

In Python 3 we can do so like this:

from functools import reduce
from operator import __or__

qs = MyModel.objects.filter(reduce(__or__, filters))

Where filters is a list containing multiple Q objects like the ones shown in the first example.


Coding Challenge: Rectangular Intersection

Difficulty: Medium


Given two rectangles, write a function that can find the rectangular intersection between the two rectangles. The rectangles are always straight and never diagonal (each side is parallel with either the x axis or the y axis). Consider only positive axis.

Rectangular Intersection

The rectangles are defined as a Rectangle class as shown below:

Rectangle(left_x, bottom_y, width, height)

# Example
r = Rectangle(2, 7, 30 8)

Your output rectangle should be returned using this format.


Coding Challenge: Longest Substring Without Repeating Characters

Difficulty: Easy


Given a string, find the length of the longest substring without repeating characters.


  • Given abcabcbb, the answer is abc, which the length is 3.
  • Given bbbbb, the answer is b, with the length of 1.
  • Given pwwkew, the answer is wke, with the length of 3. Note that the answer must be a substring, pwke is a subsequence and not a substring.

Custom Log Files With Django and Elastic Beanstalk

With Amazon ElasticBeanstalk it is possible to view log files of your deployed application. However, ElasticBeanstalk by default only returns certain logs like /var/log/httpd/error_log or /var/log/httpd/access_log if you are using Apache httpd.

If you are generating custom logs in your Django application and using file handlers to save them to log files, you will probably want to be able to access and read them easily from the ElasticBeanstalk console.

In this post I will show you how to achieve this, using Django as our backend framework.

django elasticbeanstalk
← Newer Posts Older Posts →