21

I want to make a logging manager gui. Basically I want to get the tree structure of loggers and show their level and formatter in a pyqt gui. How can I get the format string from a formatter associated a handler object?

eg:

import logging
_logger = logging.getLogger('myLogger')

ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)s:%(levelname)s: %(message)s')
ch.setFormatter(formatter)
_logger.addHandler(ch)
_logger.propagate=0

Now I have the _logger, how can I get string '%(name)s:%(levelname)s: %(message)s' from the logging.Formatter object?

>>> _logger.handlers[0]
<logging.StreamHandler object at 0x13807610>
>>> _logger.handlers[0].formatter
<logging.Formatter object at 0x13807690>
0

1 Answer 1

36

logging.Formatter instances have a _fmt attribute:

>>> _logger.handlers[0].formatter
<logging.Formatter object at 0x102c72fd0>
>>> _logger.handlers[0].formatter._fmt
'%(name)s:%(levelname)s: %(message)s'

You can always use dir() and vars() on objects to see what names might be available:

>>> vars(_logger.handlers[0].formatter)
{'_style': <logging.PercentStyle object at 0x102e6bbf0>, '_fmt': '%(name)s:%(levelname)s: %(message)s', 'datefmt': None}

or you could just look at the source code (linked from the top of the module documentation).

Sign up to request clarification or add additional context in comments.

4 Comments

If accessing an internal _ attribute (which is generally a frowned-upon practice) is good enough for a million-pointer, it is good enough for me.
@PaulMcG: the logging module is breaking style conventions in so many ways already, and there is no other option to access the format string. Needs must!
@PaulMcG: the _ initial underscore convention is more of a warning: Watch out, this attribute could be removed or change meaning in a future version without notice. With the logging module however, I'm quite confident that if the attribute were ever to go away that there'd be a long deprecation period first, given the history of the package.
This comment was as much for my own benefit as for anyone else; I recently wrote code that accessed this _fmt field so that I could define a new formatter with some custom placeholders to it. 20 years' worth of Python coding habits gave me pause at committing such an act, but now I know I am in good company.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.