wip: async logging

This commit is contained in:
grillazz 2025-07-26 19:25:46 +02:00
parent 6ec8a3ce0a
commit a99a0e780b
9 changed files with 12 additions and 12 deletions

View File

@ -87,7 +87,7 @@ async def smtp_check(
"subject": subject, "subject": subject,
} }
await logger.info("Sending email.", email_data=email_data) await logger.ainfo("Sending email.", email_data=email_data)
await run_in_threadpool( await run_in_threadpool(
smtp.send_email, smtp.send_email,

View File

@ -21,13 +21,13 @@ async def create_multi_stuff(
db_session.add_all(stuff_instances) db_session.add_all(stuff_instances)
await db_session.commit() await db_session.commit()
except SQLAlchemyError as ex: 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( raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex) status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
) from ex ) from ex
else: else:
logger.info( await logger.ainfo(
f"{len(stuff_instances)} instances of Stuff inserted into database." f"{len(stuff_instances)} Stuff instances inserted into the database."
) )
return True return True

View File

@ -18,7 +18,7 @@ router = APIRouter(prefix="/v1/user")
async def create_user( async def create_user(
payload: UserSchema, request: Request, db_session: AsyncSession = Depends(get_db) 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()) _user: User = User(**payload.model_dump())
await _user.save(db_session) await _user.save(db_session)

View File

@ -29,5 +29,5 @@ async def get_db() -> AsyncGenerator:
try: try:
yield session yield session
except Exception as e: except Exception as e:
logger.error(f"Error getting database session: {e}") await logger.aerror(f"Error getting database session: {e}")
raise raise

View File

@ -30,7 +30,7 @@ async def lifespan(app: FastAPI):
min_size=5, min_size=5,
max_size=20, 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 yield
finally: finally:
await app.redis.close() await app.redis.close()

View File

@ -30,7 +30,7 @@ class Base(DeclarativeBase):
db_session.add(self) db_session.add(self)
return await db_session.commit() return await db_session.commit()
except SQLAlchemyError as ex: 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( raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex) status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
) from ex ) from ex

View File

@ -40,7 +40,7 @@ class AuthBearer(HTTPBearer):
raise HTTPException( raise HTTPException(
status_code=403, detail="Invalid token or expired token." 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 return credentials.credentials

View File

@ -15,9 +15,9 @@ logger = AppLogger().get_logger()
async def tick(): async def tick():
async with AsyncSessionFactory() as session: async with AsyncSessionFactory() as session:
stmt = text("select 1;") 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) result = await session.execute(stmt)
logger.info(f">>>> Result: {result.scalar()}") await logger.ainfo(f">>>> Result: {result.scalar()}")
return True return True

View File

@ -66,7 +66,7 @@ class RotatingBytesLoggerFactory:
return RotatingBytesLogger(self.handler) return RotatingBytesLogger(self.handler)
@define(slots=True) @define
class AppStructLogger(metaclass=SingletonMetaNoArgs): class AppStructLogger(metaclass=SingletonMetaNoArgs):
_logger: structlog.BoundLogger = field(init=False) _logger: structlog.BoundLogger = field(init=False)