I'm trying to add a richFormatter() to my existing logging stack, but how can I get it to fit my own logging format: and not wrap the default richLogger outside of it?
#!/usr/bin/env python3 import logging from operator import truediv from rich.logging import RichHandler # in console we like this because vscode map the fullpath + linenumber console_fmt = "%(asctime)s.%(msecs)03d|%(levelname)s|%(pathname)s:%(lineno)d|%(message)s" datefmt = "%Y%m%d%H%M%S" log = logging.getLogger('root') log.setLevel(logging.DEBUG) ch = RichHandler(level=logging.DEBUG, show_level=True) ch.setLevel(logging.DEBUG) # this goes inside the rich format, I want it to define it entirely: ch.setFormatter(logging.Formatter(console_fmt, datefmt)) log.addHandler(ch) # just to show my existing default format as a 2nd line olog = logging.getLogger('root') ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(logging.Formatter(console_fmt, datefmt)) olog.addHandler(ch) log.info({"array": [1,2,3,4], "dict": {'one': 1, 'two': 2, 'three':3}, 'ok': True, 'notok': False}) From the output you see the first line is rich and the 2nd is my default format:
20220117133730 INFO 20220117133730.649|INFO|C:\dist\work\trk-fullstack-test\bin\logtest.py:26|{'array': [1, 2, 3, 4], 'dict': {'one': 1, 'two': 2, 'three': 3}, 'ok': True, 'notok': False} logtest.py:26 20220117133730.649|INFO|C:\dist\work\trk-fullstack-test\bin\logtest.py:26|{'array': [1, 2, 3, 4], 'dict': {'one': 1, 'two': 2, 'three': 3}, 'ok': True, 'notok': False} 2Related questions 61 How to write custom python logging handler? 5 Setting up advanced Python logging 9 Python Logging module: custom loggers Related questions 61 How to write custom python logging handler? 5 Setting up advanced Python logging 9 Python Logging module: custom loggers 41 How to configure Logging in Python 0 Configure logging in Python 0 Python customize logging 1 Writing custom log handler in python 4 How to create new custom logger function in Python 1 How can I decorate python logging output? 1 Sample logging generation python logging library Load 7 more related questions Show fewer related questions
Reset to default