Logging variable data with new format string

后端 未结 9 2136
别那么骄傲
别那么骄傲 2020-11-28 03:09

I use logging facility for python 2.7.3. Documentation for this Python version say:

the logging package pre-dates newer formatting options such as str

9条回答
  •  执念已碎
    2020-11-28 03:25

    Try logging.setLogRecordFactory in Python 3.2+:

    import collections
    import logging
    
    
    class _LogRecord(logging.LogRecord):
    
        def getMessage(self):
            msg = str(self.msg)
            if self.args:
                if isinstance(self.args, collections.Mapping):
                    msg = msg.format(**self.args)
                else:
                    msg = msg.format(*self.args)
            return msg
    
    
    logging.setLogRecordFactory(_LogRecord)
    

提交回复
热议问题