If you are using dictConfig, you need to consider your setting
of disable_existing_loggers. It is enabled by default, and causes
any third party module loggers to be disabled.
Using logfmter via fileConfig is not supported, because fileConfig does not support custom formatter initialization. There may be some hacks to make this work in the future. Let me know if you have ideas or really need this.
Configuration
There is no additional configuration necessary to get started using Logfmter.
However, if desired, you can modify the functionality using the following
initialization parameters.
keys
By default, the at=<levelname> key/value will be included in all log messages. These
default keys can be overridden using the keys parameter. If the key you want to include
in your output is represented by a different attribute on the log record, then you can
use the mapping parameter to provide that key/attribute mapping.
By default, a mapping of {"at": "levelname"} is used to allow the at key to reference
the log record's levelname attribute. You can override this parameter to provide your
own mappings.
This will cause all logs to have the trace_id=123 pair regardless of including
trace_id in keys or manually adding trace_id to the extra parameter or the msg object.
Note, the defaults object uses format strings as values. This allows for variables templating. See "Aliases" guide for more information.
ignored_keys
Sometimes log records include fields that you don't want in your output.
This often happens when other libraries or frameworks add extra keys to the LogRecord that are not relevant to your log format.
You can explicitly exclude unwanted keys by using the ignored_keys parameter.
import logging
from logfmter import Logfmter
formatter = Logfmter(
keys=["at"],
mapping={"at": "levelname"},
datefmt="%Y-%m-%d",
ignored_keys=["color_message"],
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.basicConfig(handlers=[handler])
logging.info("Started server process [%s]", 97819, extra={"color_message": "Started server process [%d]"})
# at=INFO msg="Started server process [97819]"
Extension
You can subclass the formatter to change its behavior.
import logging
from logfmter import Logfmter
class CustomLogfmter(Logfmter):
"""
Provide a custom logfmt formatter which formats
booleans as "yes" or "no" strings.
"""
@classmethod
def format_value(cls, value):
if isinstance(value, bool):
return "yes" if value else "no"
return super().format_value(value)
handler = logging.StreamHandler()
handler.setFormatter(CustomLogfmter())
logging.basicConfig(handlers=[handler])
logging.error({"example": True}) # at=ERROR example=yes
Guides
Aliases
Providing a format string as a default's key/value allows the realization of aliases:
If you are using nix & direnv, then your dev environment will be managed automatically. Otherwise, you will need to manually install the following software:
Additionally, if you aren't using nix, then you will need to manually build
the "external" tools found in external. These are used during testing to
verify compatibility with libraries from different ecosystems. Alternatively, you can exclude those tests with pytest -m "not external", but this is not recommended.
Getting Started
Setup
If you are using pyenv, you will need to install the correct versions of python using <runtimes.txt xargs -n 1 pyenv install -s.
Use together with logfmter: extensions, drivers, servers and companion tools.
structlog-configPythie’s Index: 81A comprehensive structlog configuration with sensible defaults for development and production environments, featuring context management, exception formatting, and path prettification.474.4K downloads/mo