Skip to content

Commit 9e07f55

Browse files
authored
1 merge (#4)
feat: add detailed information to cloud json formatter
1 parent 1c0232e commit 9e07f55

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

app/src/zcs/core/exception/exception.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import random
22
import string
3+
import traceback
34

45
class ZcsException(Exception):
56

6-
def __init__(self, user_message, error_source = "Unspecified", status_code = 500, internal_message = None):
7+
def __init__(self, user_message = "Unknown error.", error_source = "Unspecified", status_code = 500, internal_message = None):
78
super().__init__(user_message)
89

910
self.__error_source = error_source
@@ -33,5 +34,10 @@ def get_as_dict(self):
3334
'user_message': self.get_user_message(),
3435
'internal_message': self.get_internal_message(),
3536
'error_source': self.get_error_source(),
36-
'status_code': self.get_status_code()
37-
}
37+
'status_code': self.get_status_code(),
38+
'traceback': "".join(traceback.format_exception(self))
39+
}
40+
41+
def __str__(self):
42+
return f"{self.get_error_code()} - {self.get_error_source()} - {self.get_internal_message()} - {self.get_user_message()}"
43+

app/src/zcs/core/logger/logger.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
import json
22
import logging
33

4+
from zcs.core.exception import ZcsException
5+
6+
old_factory = logging.getLogRecordFactory()
7+
8+
def record_factory(*args, **kwargs):
9+
record = old_factory(*args, **kwargs)
10+
11+
record.error_code = None
12+
if args[4] and isinstance(args[4], ZcsException):
13+
record.error_code = args[4].get_error_code()
14+
15+
# Set original exception value if needed
16+
record.original_exception = None
17+
if args[6] is not None:
18+
for arg in args[6]:
19+
if isinstance(arg, Exception):
20+
record.original_exception = arg
21+
if isinstance(arg, ZcsException):
22+
record.error_code = arg.get_error_code()
23+
24+
return record
25+
26+
logging.setLogRecordFactory(record_factory)
27+
428
class CloudJsonFormatter(logging.Formatter):
529
def format(self, record):
30+
31+
message = record.getMessage()
32+
if record.exc_info and record.original_exception:
33+
message = str(record.original_exception)
34+
635
log_record = {
736
"severity": record.levelname,
8-
"message": record.getMessage(),
9-
"time": self.formatTime(record, self.datefmt)
37+
"message": message,
38+
"time": self.formatTime(record, self.datefmt),
39+
"logging.googleapis.com/sourceLocation": {
40+
"file": record.filename,
41+
"line": record.lineno,
42+
"function": record.funcName,
43+
"pathname": record.pathname,
44+
"traceback": self.formatException(record.exc_info) if record.exc_info else ""
45+
},
46+
"logging.googleapis.com/labels": {
47+
"logger_name": record.name,
48+
"error_code": record.error_code,
49+
},
1050
}
1151
return json.dumps(log_record)
1252

@@ -29,7 +69,7 @@ def __init__(self, verbose = False):
2969

3070
self.FORMATTERS = {
3171
logging.DEBUG: logging.Formatter(set_grey + base_format + reset),
32-
logging.INFO: logging.Formatter(set_green + base_format + reset),
72+
logging.INFO: logging.Formatter(base_format),
3373
logging.WARNING: logging.Formatter(set_yellow + base_format + reset),
3474
logging.ERROR: logging.Formatter(set_red + base_format + reset),
3575
logging.CRITICAL: logging.Formatter(set_bold_red + base_format + reset)

0 commit comments

Comments
 (0)