File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,33 @@ This will cause all logs to have the `trace_id=123` pair regardless of including
218218
219219> Note, the defaults object uses format strings as values. This allows for variables templating. See "Aliases" guide for more information.
220220
221+ ** Exclude ignored keys**
222+
223+ Sometimes log records include fields that you don't want in your output.
224+ This often happens when other libraries or frameworks add extra keys to the ` LogRecord ` that are not relevant to your log format.
225+
226+ You can explicitly exclude unwanted keys by using the ` ignored_keys ` parameter.
227+
228+ ``` py
229+ import logging
230+ from logfmter import Logfmter
231+
232+ formatter = Logfmter(
233+ keys = [" at" ],
234+ mapping = {" at" : " levelname" },
235+ datefmt = " %Y-%m-%d " ,
236+ ignored_keys = [" color_message" ],
237+ )
238+
239+ handler = logging.StreamHandler()
240+ handler.setFormatter(formatter)
241+
242+ logging.basicConfig(handlers = [handler])
243+
244+ logging.info(" Started server process [%s ]" , 97819 , extra = {" color_message" : " Started server process [%d ]" })
245+ # at=INFO msg="Started server process [97819]"
246+ ```
247+
221248## Extension
222249
223250You can subclass the formatter to change its behavior.
You can’t perform that action at this time.
0 commit comments