Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename "levelname" to "level" in Python log messages?

Tags:

python

logging

I have a Python logging configuration which looks like this:

LOGGING_CONFIG:
    version: 1
    formatters:
        human:
            class: logging.Formatter
            format: '[%(asctime)s]:[%(levelname)s]: %(message)s'
        json:
            class: pythonjsonlogger.jsonlogger.JsonFormatter
            format: '%(asctime)s %(levelname)s %(message)s'
    handlers:
        console:
            class: logging.StreamHandler
            level: DEBUG
            formatter: json  # change this to 'human' when you run it locally, json in production
        file:
            class: logging.FileHandler
            filename: logfile.log
            level: DEBUG
            formatter: json
    root:  # Logging for 3rd party stuff
        level: INFO
        handlers:
            - console
            - file
    project_name:  # Logging this module
        level: DEBUG
        handlers:
            - console
            - file

For another system, I need the structured logging to have some fixed names. One example is that it should not be levelname, but level. How can I rename log fields?

What I have:

{"asctime": "2018-10-22 14:50:19,923", "levelname": "info", "message": "foobar"}

What I want:

{"asctime": "2018-10-22 14:50:19,923", "level": "info", "message": "foobar"}
like image 566
Martin Thoma Avatar asked Oct 22 '18 12:10

Martin Thoma


People also ask

How do I create a log record in Python?

LogRecord instances are created automatically by the Logger every time something is logged, and can be created manually via makeLogRecord () (for example, from a pickled event received over the wire). class logging. LogRecord (name, level, pathname, lineno, msg, args, exc_info, func=None, sinfo=None) ¶

What is the hierarchy of loggers in Python?

The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger (__name__). That’s because in a module, __name__ is the module’s name in the Python package namespace. class logging. Logger ¶

What is the logging API in Python?

The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules. The module provides a lot of functionality and flexibility.

What is logname in logrecord?

name – The name of the logger used to log the event represented by this LogRecord. Note that this name will always have this value, even though it may be emitted by a handler attached to a different (ancestor) logger.


1 Answers

levelname will not appear in normal Python loggers but in the custom formatter you used. From what I understood you could use below snippet to customize the dict it outputs:

class CustomJsonFormatter(jsonlogger.JsonFormatter):
    def add_fields(self, log_record, record, message_dict):
        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
        log_record['level'] = log_record['levelname']
        del log_record['levelname']

Then replace pythonjsonlogger.jsonlogger.JsonFormatter with the address of CustomJsonFormatter.

like image 177
Arman Ordookhani Avatar answered Nov 15 '22 03:11

Arman Ordookhani