Set extra record cause "KeyError" issue in FastAPI
See original GitHub issueWhen i set some extra record in fastAPI startup event caused something error
Versions:
- python 3.8.12
- loguru 0.6.0
- fastapi 0.75
Code:
def logger_init():
logger.remove()
logger.add(
sys.stdout,
format="{extra[event]}",
)
context_logger = logger.bind(event="access")
return context_logger
app = FastAPI()
@app.on_event("startup")
async def startup_event():
logger = logger_init()
logger.info("Start up")
Error message :
--- Logging error in Loguru Handler #1 ---
Record was: {'elapsed': datetime.timedelta(microseconds=270683), 'exception': None, 'extra': {}, 'file': (name='__init__.py', path='/app/./app/__init__.py'), 'function': 'startup_event', 'level': (name='INFO', no=20, icon='ℹ️'), 'line': 100, 'message': '--- Startup Event ---', 'module': '__init__', 'name': 'app', 'process': (id=229, name='SpawnProcess-102'), 'thread': (id=139944207652672, name='MainThread'), 'time': datetime(2022, 7, 6, 14, 51, 4, 487669, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC'))}
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/loguru/_handler.py", line 155, in emit
formatted = precomputed_format.format_map(formatter_record)
KeyError: 'event'
--- End of logging error ---
Issue Analytics
- State:
- Created a year ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
[BUG] OpenAPI KeyError on body parameter when field types ...
Error Reason: fastapi converts a dataclasses.dataclass into class pydantic.BaseModel model. Thereby, fastapi treats the same dataclass model as ...
Read more >Handling Errors - FastAPI
When a request contains invalid data, FastAPI internally raises a RequestValidationError . And it also includes a default exception handler for it. To...
Read more >MemoryError with FastApi and SpaCy - nlp - Stack Overflow
The SpaCy tokenizer seems to cache each token in a map internally. Consequently, each new token increases the size of that map.
Read more >loguru Documentation - Read the Docs
Loguru helps you identify problems by allowing the entire stack trace to ... your logger messages by modifying the extra record attribute.
Read more >AWS Lambda function errors in Python
This page describes how to view Lambda function invocation errors for the Python runtime using the Lambda console and the AWS CLI.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@GGGoingdown Thanks for the fully reproducible example.
The
provider
value is only present in the logger object returned by.bind()
. Theextra
dict of defaultlogger
is empty, which causes aKeyError
during formatting. You can set a default value withlogger.configure(extra={"provider": None})
for example.Use the
patch()
function then: