[HELP] Logging Basic Python Class Only
TWDickson opened this issue · 0 comments
TWDickson commented
I've got a basic python class that and an interpreter written that translates my class into JSON
class LogMessage:
def __init__(self, message):
self.timestamp = datetime.datetime.now()
self.message = message
def __str__(self):
return f"{self.timestamp.isoformat()}: {self.message}"
class LogMessageEncoder(JSONEncoder):
"""
JSON Encoder for Name Match Diagnostic Class
"""
def default(self, o):
# Rough conversion
temp = o.__dict__
for e in temp.keys():
if isinstance(temp[e], datetime.datetime):
temp[e] = temp[e].isoformat()
return temp
logger = logging.getLogger()
logHandler = logging.FileHandler(f"logs/{datetime.datetime.today().strftime('%Y-%m-%dT%H-%M-%S')}-JSON-Log.log")
formatter = jsonlogger.JsonFormatter(json_encoder=modules.log_message.LogMessageEncoder)
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(logging.DEBUG)
logger.debug(LogMessage("foo"))
# logs as {"message": "2020-11-13T14:45:56.602299: foo"}
How would I log just the class (and subclasses)? without something like "message" it seems that the logger is using the str representation instead of the encoder.