Skip to content

Instantly share code, notes, and snippets.

@lamak-qaizar
Last active November 27, 2020 07:47
Show Gist options
  • Save lamak-qaizar/d5480f662af54e6c7ba563ff9b5ac6de to your computer and use it in GitHub Desktop.
Save lamak-qaizar/d5480f662af54e6c7ba563ff9b5ac6de to your computer and use it in GitHub Desktop.
Redis SLOWLOG pretty printer with formatted timestamps
# 1. Requirements:
# python3.6, redis-py
# 2. Run instructions:
# python redis-slowlog-pretty-printer.py <redis endpoint> <port> <# logs to get>
# eg. python redis-slowlog-pretty-printer.py localhost 6379 10
# 3. Sample output:
# <local date/time> [ <execution time> ]: <command>
# 2017-06-22 00:00:00 [ 10.0 s ]: b'keys *rk54jhk345hnmmlk4lk*'
import redis
import datetime
import sys
pool = redis.ConnectionPool(host=sys.argv[1], port=sys.argv[2], db=0)
r = redis.Redis(connection_pool=pool)
slowlog = r.slowlog_get(sys.argv[3])
for slowlog_entry in slowlog:
timestamp = datetime.datetime.fromtimestamp(int(slowlog_entry['start_time']))
duration = round(int(slowlog_entry['duration']) / 1000 / 1000, 0)
command = slowlog_entry['command']
print(timestamp.strftime('%Y-%m-%d %H:%M:%S'), ' [', duration, 's', ']: ', command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment