Skip to content

Commit cae6028

Browse files
committed
feat(logger): add ConsoleFormatter for improved log output
Introduce ConsoleFormatter to enhance console log formatting. Update log_format_adapter to return ConsoleFormatter for console log type. Ensure JSONFormatter computes asctime and message for better JSON log output consistency.
1 parent e516ee6 commit cae6028

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22

3-
from tracardi.service.logging.formater import CustomFormatter, JSONFormatter
3+
from tracardi.service.logging.formater import CustomFormatter, JSONFormatter, ConsoleFormatter
4+
45

56
def log_format_adapter():
67
type = os.environ.get('LOGGING_FORMAT', 'console')
7-
88
if type == 'console':
9-
return CustomFormatter()
9+
return ConsoleFormatter()
1010
elif type == 'json':
11-
return JSONFormatter()
11+
return JSONFormatter()
12+
else:
13+
return CustomFormatter()

tracardi/service/logging/formater.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,37 @@ def format(self, record):
2626
formatter = logging.Formatter(log_fmt)
2727
return formatter.format(record)
2828

29+
class ConsoleFormatter(logging.Formatter):
30+
format = "%(asctime)s [%(levelname)s] %(message)s | %(name)s | %(filename)s | %(lineno)d"
31+
32+
FORMATS = {
33+
logging.DEBUG: format,
34+
logging.INFO: format,
35+
logging.WARNING: format,
36+
logging.ERROR: format,
37+
logging.CRITICAL: format
38+
}
39+
40+
def format(self, record):
41+
log_fmt = self.FORMATS.get(record.levelno, self.format)
42+
formatter = logging.Formatter(log_fmt)
43+
return formatter.format(record)
44+
45+
2946

3047
class JSONFormatter(logging.Formatter):
3148
def format(self, record):
49+
# Ensure asctime and message are computed
50+
record.asctime = self.formatTime(record, self.datefmt)
51+
record.message = record.getMessage()
52+
3253
log_record = {
3354
"timestamp": record.asctime,
3455
"level": record.levelname,
3556
"message": record.message,
3657
"name": record.name,
3758
"filename": record.filename,
38-
"lineno": record.lineno
59+
"lineno": record.lineno,
3960
}
4061
return json.dumps(log_record)
4162

0 commit comments

Comments
 (0)