classification
Title: Custom logging formatter doesnt work in 3.3.4
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: derekwallace, vinay.sajip
Priority: normal Keywords:

Created on 2014-02-22 20:31 by derekwallace, last changed 2014-02-22 22:44 by vinay.sajip. This issue is now closed.

Messages (2)
msg211949 - (view) Author: Derek Wallace (derekwallace) Date: 2014-02-22 20:31
Hi,
I use the logging module and have created a custom formatter to format the log messages different for each logging level.
This was developed with 3.3.3.

When i upgraded to 3.3.4 it no longer worked.
I got the error
TypeError: 'tuple' object is not callable



Here is the original code.
class SpecialFormatter(logging.Formatter):
    FORMATS = {logging.DEBUG : logging._STYLES['{']("{module} DEBUG: {lineno}: {message}"),
           logging.ERROR : logging._STYLES['{']("{module} ERROR: {message}"),
           logging.INFO : logging._STYLES['{']("{module}: {message}"),
           'DEFAULT' : logging._STYLES['{']("{module}: {message}")}

    def format(self, record):
        # Ugly. Should be better
        self._style = self.FORMATS.get(record.levelno, self.FORMATS['DEFAULT'])
        return logging.Formatter.format(self, record)

hdlr = logging.StreamHandler(sys.stderr)
hdlr.setFormatter(SpecialFormatter())
logging.root.addHandler(hdlr)
logging.root.setLevel(logging.INFO)



In the 3.3.4 release the strucutre of the 
logging._STYLES
changed in logging/__init__.py.

It changed from a value to a tuple.

Therefore the above code needed to change
logging._STYLES['{']("{module} DEBUG: {lineno}: {message}")

to

logging._STYLES['{'][0]("{module} DEBUG: {lineno}: {message}")


Derek
msg211969 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2014-02-22 22:44
The change was necessitated by the fix for #20242. Sorry for the inconvenience, but the fix was implemented in what seemed like the most appropriate way. Of course, _STYLES is an internal implementation detail which could change again in the future.

Having explained the reasoning behind the change, I'll close the issue.
History
Date User Action Args
2014-02-22 22:44:56vinay.sajipsetstatus: open -> closed
assignee: vinay.sajip
resolution: not a bug
messages: + msg211969
2014-02-22 21:03:07ned.deilysetnosy: + vinay.sajip
2014-02-22 20:31:55derekwallacecreate