mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
wip: async logging
This commit is contained in:
parent
6ec8a3ce0a
commit
a99a0e780b
@ -87,7 +87,7 @@ async def smtp_check(
|
||||
"subject": subject,
|
||||
}
|
||||
|
||||
await logger.info("Sending email.", email_data=email_data)
|
||||
await logger.ainfo("Sending email.", email_data=email_data)
|
||||
|
||||
await run_in_threadpool(
|
||||
smtp.send_email,
|
||||
|
@ -21,13 +21,13 @@ async def create_multi_stuff(
|
||||
db_session.add_all(stuff_instances)
|
||||
await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
logger.error(f"Error inserting instances of Stuff: {repr(ex)}")
|
||||
await logger.aerror(f"Error inserting instances of Stuff: {repr(ex)}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
else:
|
||||
logger.info(
|
||||
f"{len(stuff_instances)} instances of Stuff inserted into database."
|
||||
await logger.ainfo(
|
||||
f"{len(stuff_instances)} Stuff instances inserted into the database."
|
||||
)
|
||||
return True
|
||||
|
||||
|
@ -18,7 +18,7 @@ router = APIRouter(prefix="/v1/user")
|
||||
async def create_user(
|
||||
payload: UserSchema, request: Request, db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
logger.info(f"Creating user: {payload}")
|
||||
await logger.ainfo(f"Creating user: {payload}")
|
||||
_user: User = User(**payload.model_dump())
|
||||
await _user.save(db_session)
|
||||
|
||||
|
@ -29,5 +29,5 @@ async def get_db() -> AsyncGenerator:
|
||||
try:
|
||||
yield session
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting database session: {e}")
|
||||
await logger.aerror(f"Error getting database session: {e}")
|
||||
raise
|
||||
|
@ -30,7 +30,7 @@ async def lifespan(app: FastAPI):
|
||||
min_size=5,
|
||||
max_size=20,
|
||||
)
|
||||
logger.info("Postgres pool created", idle_size=app.postgres_pool.get_idle_size())
|
||||
await logger.ainfo("Postgres pool created", idle_size=app.postgres_pool.get_idle_size())
|
||||
yield
|
||||
finally:
|
||||
await app.redis.close()
|
||||
|
@ -30,7 +30,7 @@ class Base(DeclarativeBase):
|
||||
db_session.add(self)
|
||||
return await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
logger.error(f"Error inserting instance of {self}: {repr(ex)}")
|
||||
await logger.aerror(f"Error inserting instance of {self}: {repr(ex)}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
|
@ -40,7 +40,7 @@ class AuthBearer(HTTPBearer):
|
||||
raise HTTPException(
|
||||
status_code=403, detail="Invalid token or expired token."
|
||||
)
|
||||
logger.info(f"Token verified: {credentials.credentials}")
|
||||
await logger.ainfo(f"Token verified: {credentials.credentials}")
|
||||
return credentials.credentials
|
||||
|
||||
|
||||
|
@ -15,9 +15,9 @@ logger = AppLogger().get_logger()
|
||||
async def tick():
|
||||
async with AsyncSessionFactory() as session:
|
||||
stmt = text("select 1;")
|
||||
logger.info(f">>>> Be or not to be...{datetime.now()}")
|
||||
await logger.ainfo(f">>>> Be or not to be...{datetime.now()}")
|
||||
result = await session.execute(stmt)
|
||||
logger.info(f">>>> Result: {result.scalar()}")
|
||||
await logger.ainfo(f">>>> Result: {result.scalar()}")
|
||||
return True
|
||||
|
||||
|
||||
|
@ -66,7 +66,7 @@ class RotatingBytesLoggerFactory:
|
||||
return RotatingBytesLogger(self.handler)
|
||||
|
||||
|
||||
@define(slots=True)
|
||||
@define
|
||||
class AppStructLogger(metaclass=SingletonMetaNoArgs):
|
||||
_logger: structlog.BoundLogger = field(init=False)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user