Skip to content

Make log file more readable #11

@disooqi

Description

@disooqi

OPTION ONE

print json.dumps(my_dict, indent=1, sort_keys=True)

Also, see (https://docs.python.org/3/howto/logging-cookbook.html#implementing-structured-logging)

import json
import logging

class StructuredMessage:
def init(self, message, /, **kwargs):
self.message = message
self.kwargs = kwargs

def __str__(self):
    return '%s >>> %s' % (self.message, json.dumps(self.kwargs))

_ = StructuredMessage # optional, to improve readability

logging.basicConfig(level=logging.INFO, format='%(message)s')
logging.info(_('message 1', foo='bar', bar='baz', num=123, fnum=123.456))

If the above script is run, it prints:
message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"}

OPTION TWO

Also, use log collection system such as Graylog and its Python logging handlers that send log messages into it or Logstash.

For more details in in the 2nd option visit https://paul.querna.org/articles/2011/12/26/log-for-machines-in-json/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions