-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatters.py
53 lines (45 loc) · 1.67 KB
/
formatters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
import re
import traceback
def get_line():
try:
last_line = [line for line in traceback.format_stack() if "myproject" in line][-3]
pattern = r"\"(.*?)\", line (\d+),.*?\n(.*?)\n"
match = re.search(pattern, last_line)
if match:
file_path, line_number, next_line = match.groups()
return f"{file_path}:{line_number}\n {next_line}"
except:
return ""
# https://markusholtermann.eu/2016/01/syntax-highlighting-for-djangos-sql-query-logging/
class SQLFormatter(logging.Formatter):
def format(self, record):
# Check if Pygments is available for coloring
try:
import pygments
from pygments.lexers import SqlLexer
from pygments.formatters import TerminalTrueColorFormatter
except ImportError:
pygments = None
# Check if sqlparse is available for indentation
try:
import sqlparse
except ImportError:
sqlparse = None
# Remove leading and trailing whitespaces
sql = record.sql.strip()
if sqlparse:
# Indent the SQL query
sql = sqlparse.format(sql, reindent=True)
if pygments:
# Highlight the SQL query
sql = pygments.highlight(
sql, SqlLexer(), TerminalTrueColorFormatter(style="default")
)
# Set the record's statement to the formatted query
statement = sql
line_executing_code = get_line()
if line_executing_code:
statement = f"{line_executing_code}\n{sql}"
record.statement = statement
return super(SQLFormatter, self).format(record)