@@ -24,20 +24,43 @@ poetry add fastapi-structlog
2424
2525## Using logging
2626
27- To implement logging into the application, it is
28- enough to use ` init_logging ` :
27+ To implement logging in an application, two things are necessary:
28+ define the configuration and use ` setup_logger ` :
2929
3030``` python
3131import structlog
32- from fastapi_structlog import init_logger
32+ from fastapi_structlog import setup_logger
3333
34- init_logger()
34+ log_settings = LogSettings(
35+ logger = ' test-log-lib' ,
36+ json_logs = False ,
37+ debug = True ,
38+ types = [' console' ],
39+ )
40+ setup_logger(log_settings)
3541
3642log = structlog.get_logger()
3743
3844log.info(' Hello, World!' )
3945```
4046
47+ In this case, the entire configuration is determined manually by
48+ creating an instance of ` LogSettings ` .
49+
50+ If you want to get the configuration from the environment variables,
51+ then add ` LogSettings ` as a nested model in your settings:
52+
53+ ``` python
54+ class Settings (BaseSettings ):
55+ model_config = SettingsConfigDict(
56+ arbitrary_types_allowed = True ,
57+ env_ignore_empty = True ,
58+ env_nested_delimiter = ' __' ,
59+ extra = ' ignore' ,
60+ )
61+ log: LogSettings
62+ ```
63+
4164In this case, the entire configuration is taken from
4265the ` .env ` file by means of Pedantic.
4366
@@ -46,9 +69,8 @@ example №1 (`docs_src/example_1.py`).
4669
4770## Logger configuration
4871
49- To configure the logger, you need to use ` fastapi_structlog.init_logging ` ,
50- which gets the values of the environment variables using ` pydantic ` .
51- You can specify the ` env_prefix ` argument when using prefixes.
72+ The logger configuration is implemented using the pydantic model ` LogSettings ` .
73+ You can easily integrate it into your application settings model.
5274
5375Logger configuration parameters:
5476
@@ -104,27 +126,34 @@ configuration, for example:
104126- ignoring environment variables with empty values
105127
106128``` python
129+ from pathlib import Path
130+
107131import structlog
108- from fastapi_structlog import LogSettings, setup_logger, BaseSettingsModel
132+ from pydantic_settings import SettingsConfigDict
133+
134+ from fastapi_structlog import BaseSettingsModel, LogSettings, setup_logger
109135
110136logger = structlog.get_logger()
111137
112138class Settings (BaseSettingsModel ):
113139 log: LogSettings
114140
115141 model_config = SettingsConfigDict(
116- secrets_dir = " /run/secrets" if Path(" /run/secrets" ).exists() else None ,
142+ secrets_dir = ' /run/secrets' if Path(' /run/secrets' ).exists() else None ,
117143 )
118144
119145
120146def main () -> None :
121147 settings = Settings()
122148
123- setup_logging(** settings.log.model_dump())
149+ setup_logger(settings.log)
150+
151+ logger.info(' Start' )
124152
125153
126- if __name__ == " __main__" :
154+ if __name__ == ' __main__' :
127155 main()
156+
128157```
129158
130159For more information about the integration of logging
@@ -134,16 +163,17 @@ settings, see example №4 (`docs_src/example_4.py`).
134163
135164The use of the library in FasAPI application is for the most part
136165similar to that described in the section
137- [ "Logger configuration"] ( #logger-configuration ) . To embed it into
138- your application, it is enough in the ` main ` function of the file
139- ` main.py ` (or any other entry point) before starting ` uvicorn ` (or
140- another server) , use one of the logger configuration functions. In
141- all other places, it is enough to use ` structlog.get_logger(log_name) `
142- to get the logger. It will be configured according to the settings
143- specified when launching the application.
166+ [ "Logger configuration"] ( #logger-configuration ) . To embed it in your
167+ application, it is enough in the ` main ` function of the file ` main.py ` (or at
168+ any other entry point) before starting ` uvicorn ` (or another server) or
169+ directly in ` lifespan ` , use the registrar configuration functions. In all other
170+ places, it is enough to use ` structlog.get_logger(log_name) ` to get the logger.
171+ It will be configured according to the settings specified when launching the
172+ application.
144173
145174For more information about integrating logging into a FasAPI
146- application, see example №2 (` docs_src/example_2.py ` ).
175+ application, see example №2 (` docs_src/example_2.py ` ) or example №8
176+ (` docs_src/example_8.py ` ).
147177
148178## Using middleware in a FastAPI application
149179
@@ -308,10 +338,10 @@ a different database from your main one.
308338``` python
309339engine = create_async_engine(DB_URL )
310340
311- queue_listener = init_logger (
312- env_prefix = ' LOG__ ' ,
341+ queue_listener = setup_logger (
342+ settings.log ,
313343 model = Log,
314- db_url = DB_URL ,
344+ db_url = settings.log.db.make_url() ,
315345)
316346
317347logger = structlog.get_logger()
@@ -349,6 +379,8 @@ For other usage docs_src, see `docs_src`:
349379- ` example_4 ` - example with the integration of logging settings into the application settings
350380- ` example_5 ` - example using middleware
351381- ` example_6 ` - example using Sentry and nested calls to other APIs
382+ - ` example_7 ` - expanding settings
383+ - ` example_8 ` - logging logs to the database with redefinition of some fields
352384
353385## Dependencies
354386
0 commit comments