Formatting Python protocols by layer

I am using the python protocol library, but I want the debug logs to have a different format than the warning and error logs. Is it possible?

ETA: I want warnings and errors to appear like:

%(levelname)s: %(message)s

      

but the debug statements appear as

DEBUG: (only Brian cares about this) : %(message)s

      

all other questions I have seen are format change, but this change is for EVERYTHING.

+3


source to share


1 answer


First of all, check if you really need it. The release of journals with different recording formats tends to be difficult to perceive by both humans and machines.
Maybe you want different formats for different log destinations (vs console file), which will also have different verbosity (file will have a debug log with more information).

Now you can use custom Formatter

:



class MultiformatFormatter(logging.Formatter):
    def __init__(self,<args>):
        <...>
    def format(self,record):
        if record.levelno <= logging.DEBUG:
            s=<generate string one way>
        else:
            s=<generate string another way>
        return s
<...>
#for each handler that this should apply to
handler.setFormatter(MultiformatFormatter(<args>))

      

+1


source







All Articles