Either of these can be used in place of a format string, to allow {}- or $-formatting to be used to build the actual “message” part which appears in the formatted log output in place of “%(message)s” or “{message}” or “$message”. If you find it a little unwieldy to use the class names whenever you want to log something, you can make it more palatable if you use an alias such as$_{M}$ or$_{_}$for the message (or perhaps$_{__}$ , if you are using$_{_}$for localization). Examples of this approach are given below. Firstly, formatting with$_{str.format()}$ : Secondly, formatting with string.Template : One thing to note is that you pay no significant performance penalty with this approach: the actual formatting happens not when you make the logging call, but when (and if) the logged message is actually about to be output to a log by a handler. So the only slightly unusual thing which might trip you up is that the parentheses go around the format string and the arguments, not just the format string. That’s because the __ notation is just syntax sugar for a constructor call to one of the$_{XXXMessage}$ classes shown above. You can configure filters using$_{dictConfig()}$ , though it might not be obvious at first glance how to do it (hence this recipe). Since$_{Filter}$ is the only filter class included in the standard library, and it is unlikely to cater to many requirements (it’s only there as a base class), you will typically need to define your own$_{Filter}$ subclass with an overridden$_{filter()}$ method. To do this, specify the$_{()}$ key in the configuration dictionary for the filter, specifying a callable which will be used to create the filter (a class is the most obvious, but you can provide any callable which returns a$_{Filter}$ instance). Here is a complete example: import logging import logging.config import sys classMyFilter (logging .Filter): def __init__ (self, param = None ): self.param =param def filter(self, record): if self.param is None : allow= True else : allow=self.param not in record.msg if allow: record.msg ='changed:'+record.msg return allow LOGGING ={ 'version':1, (continues on next page) 33 22 Configuring filters with dictConfig() >>> __=DollarMessage >>> print(__( 'Message with $num $what ', num =2, what='placeholders')) Message with 2 placeholders >>> >>> __=BraceMessage >>> print(__( 'Message with {0} {1} ',2,'placeholders')) Message with 2 placeholders >>> classPoint : pass ... >>> p =Point() >>> p.x =0.5 >>> p.y =0.5 >>> print(__( 'Message with coordinates: ( {point.x:.2f} , {point.y:.2f} )', point=p)) Message with coordinates: (0.50, 0.50)