This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author BNMetrics
Recipients BNMetrics, gvanrossum
Date 2018-09-29.20:17:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Issue: Currently logging.Formatter does not check if the format passed in is valid style or if the field is valid when creating the logging.Formatter object. It would be nice to have such check in the constructor of the logging.Formatter.

Here are 2 scenarios:

Scenario 1: Passing in invalid `fmt` when creating the logging.Formatter.
    import logging
    logger = logging.getLogger('my_logger')
    handler = logging.StreamHandler()

    fmt = logging.Formatter("blah-blah", style="{")

    logger.setLevel(logging.DEBUG)'This is my logging message.')

The above example would output the fmt string("blah-blah") whenever a logging operation is performed, such as ``, `logging.warning`.
And this goes the same for mismatching style and fmt, like so:
    fmt = logging.Formatter("{asctime}-{message}", style="%")

Scenario 2: Passing in invalid fields to logging.Formatter

    import logging
    logger = logging.getLogger('my_logger')
    handler = logging.StreamHandler()

    fmt = logging.Formatter("%(FuncName)s-%(message)s")

    logger.setLevel(logging.DEBUG)'This is my logging message.'
As you can see from the above example, the "%(FuncName)s" field is misspelled with a capital "F", which should have been a lowercase "f". In this scenario, we would get an interesting stacktrace:

--- Logging error ---
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/", line 992, in emit
    msg = self.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/", line 838, in format
    return fmt.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/", line 578, in format
    s = self.formatMessage(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/", line 547, in formatMessage
    return self._style.format(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/", line 391, in format
    return self._fmt % record.__dict__
KeyError: 'FuncName'
Call stack:
  File "<stdin>", line 1, in <module>
Message: 'This is my logging message.'
Arguments: ()

Personally, I think the "KeyError" here can be misleading and confusing to some. 


I would like to make a PR with the following changes:
- Checking fmt to match the style in the constructor of logging.Formatting
- When calling log message functions such as "", an "extra" key word arg can be passed for custom format fields, 
  for example:
  fmt = logging.Formatter("{cpuUsage} - {message}", style="{") # In this case, cpuUsage would be custom"my logging message", extra={"cpuUsage": "my_cpu_usage"})

  - I would like to have custom fields passed in as an additional (optional) argument into the constructor for logging.Formatter
  - Another option is to have an additional member method for adding additional fields
  - I think we could essentially have both options
  - With this, we can remove the "extra" argument in Logger.makeRecord()

Draw Backs:

- This change might essentially break some existing code where someone might use the "extra" argument in log message functions, as we would do the check on logging.Formatter level

Please let me know your thoughts, and I thought it would be nice to get some new ideas and areas I haven't thought about before I make this PR.

Best regards,
Luna Chen (BNMetrics)
Date User Action Args
2018-09-29 20:17:10BNMetricssetrecipients: + BNMetrics, gvanrossum
2018-09-29 20:17:10BNMetricssetmessageid: <>
2018-09-29 20:17:10BNMetricslinkissue34844 messages
2018-09-29 20:17:10BNMetricscreate