madzak/python-json-logger

How to include all available fields in a log record + some custom fields?

nyue opened this issue · 0 comments

nyue commented

Currently, I am writing a custom formatter to include all fields with the intention of adding some of my own later.

I have two sets of questions

Q1 : Is this the correct way? i.e. subclass the formatter and than copying field by field over ? or am I going about it the wrong way ?

Ultimately, I will want to include most of the default fields plus I am going to add some custom in-house fields.

class CustomJsonFormatter(jsonlogger.JsonFormatter):
    
    def add_fields(self, log_record, record, message_dict):
        import datetime
        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)

        log_record['args'] = record.args
        # log_record['asctime'] = record.asctime
        log_record['created'] = record.created
        log_record['exc_info'] = record.exc_info
        log_record['exc_text'] = record.exc_text
        log_record['filename'] = record.filename
        log_record['funcName'] = record.funcName
        log_record['levelname'] = record.levelname
        log_record['levelno'] = record.levelno
        log_record['lineno'] = record.lineno
        log_record['module'] = record.module
        log_record['msecs'] = record.msecs
        log_record['message'] = record.message
        log_record['msg'] = record.msg
        log_record['name'] = record.name
        log_record['pathname'] = record.pathname
        log_record['process'] = record.process
        log_record['processName'] = record.processName
        log_record['relativeCreated'] = record.relativeCreated
        log_record['stack_info'] = record.stack_info
        log_record['thread'] = record.thread
        log_record['threadName'] = record.threadName

Q2 : What if I want most but not all the default fields, is there some pythonic way to do that ?