Skip to content

Pydantic

Logfire has an Pydantic plugin to detect Pydantic models. The plugin provides logs and metrics for model validation.

To enable the plug-in, do one of the following:

  • Set the LOGFIRE_PYDANTIC_PLUGIN_RECORD environment variable to all.

  • Set pydantic_plugin_record in pyproject.toml, for example:

[tool.logfire]
pydantic_plugin_record = "all"
  • [pydantic_plugin ][logfire.configure(pydantic_plugin)] parameters are used in 在 logfire.configure, for example:

    Log import

    logfire. configure (pydantic_plugin = logfire. PydanticPlugin (record ='all'))

Note that if only the last option is used, it will only detect when calling logfire.configure. _after_definition and import of models.

third-party modules

By default, the plug-in does not detect third-party modules to avoid interference. You can create a plug-in for the [include][logfire 的用户启用检测。PydanticPlugin.include] Configuration.

import logfire

logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record='all', include={'openai'}))

You can also disable detection of your own modules using [exclude ][logfire]. PydanticPlugin. exclude] configuration.

import logfire

logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record='all', exclude={'app.api.v1'}))

model configuration

If you want finer control over plug-ins, you can use the [plugin_settings ][pydantic.config.ConfigDict.plugin_settings] class parameter in Pydantic models.

from logfire.integrations.pydantic import PluginSettings
from pydantic import BaseModel


class Foo(BaseModel, plugin_settings=PluginSettings(logfire={'record': 'failure'})):
    ...

record

record Used to configure what to log. It can be one of the following values:

  • off: Disable detection. This is the default.

  • all: Send tracking and metrics for all events.

  • failure: Send metrics for all validations, only trace for failed validations.

  • 指标: Send indicators only.


本文总阅读量次