mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #209 from grillazz/198-add-simple-caching
add structure file logging with log files rotating
This commit is contained in:
commit
6f82883612
@ -1,4 +1,3 @@
|
|||||||
import logging
|
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Query, Request, status
|
from fastapi import APIRouter, Depends, Query, Request, status
|
||||||
@ -34,7 +33,7 @@ async def redis_check(request: Request):
|
|||||||
try:
|
try:
|
||||||
redis_info = await redis_client.info()
|
redis_info = await redis_client.info()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Redis error: {e}")
|
await logger.aerror(f"Redis error: {e}")
|
||||||
return redis_info
|
return redis_info
|
||||||
|
|
||||||
|
|
||||||
@ -88,7 +87,7 @@ async def smtp_check(
|
|||||||
"subject": subject,
|
"subject": subject,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Sending email with data: %s", 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,
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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()
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,27 +11,63 @@ from whenever._whenever import Instant
|
|||||||
from app.utils.singleton import SingletonMetaNoArgs
|
from app.utils.singleton import SingletonMetaNoArgs
|
||||||
|
|
||||||
|
|
||||||
# TODO: merge this wrapper with the one in structlog under one hood of AppLogger
|
class RotatingBytesLogger:
|
||||||
class BytesToTextIOWrapper:
|
"""Logger that respects RotatingFileHandler's rotation capabilities."""
|
||||||
def __init__(self, handler, encoding="utf-8"):
|
|
||||||
|
def __init__(self, handler):
|
||||||
self.handler = handler
|
self.handler = handler
|
||||||
self.encoding = encoding
|
|
||||||
|
|
||||||
def write(self, b):
|
def msg(self, message):
|
||||||
if isinstance(b, bytes):
|
"""Process a message and pass it through the handler's emit method."""
|
||||||
self.handler.stream.write(b.decode(self.encoding))
|
if isinstance(message, bytes):
|
||||||
else:
|
message = message.decode("utf-8")
|
||||||
self.handler.stream.write(b)
|
|
||||||
self.handler.flush()
|
|
||||||
|
|
||||||
def flush(self):
|
# Create a log record that will trigger rotation checks
|
||||||
self.handler.flush()
|
record = logging.LogRecord(
|
||||||
|
name="structlog",
|
||||||
|
level=logging.INFO,
|
||||||
|
pathname="",
|
||||||
|
lineno=0,
|
||||||
|
msg=message.rstrip("\n"),
|
||||||
|
args=(),
|
||||||
|
exc_info=None
|
||||||
|
)
|
||||||
|
|
||||||
def close(self):
|
# Check if rotation is needed before emitting
|
||||||
self.handler.close()
|
if self.handler.shouldRollover(record):
|
||||||
|
self.handler.doRollover()
|
||||||
|
|
||||||
|
# Emit the record through the handler
|
||||||
|
self.handler.emit(record)
|
||||||
|
|
||||||
|
# Required methods to make it compatible with structlog
|
||||||
|
def debug(self, message):
|
||||||
|
self.msg(message)
|
||||||
|
|
||||||
|
def info(self, message):
|
||||||
|
self.msg(message)
|
||||||
|
|
||||||
|
def warning(self, message):
|
||||||
|
self.msg(message)
|
||||||
|
|
||||||
|
def error(self, message):
|
||||||
|
self.msg(message)
|
||||||
|
|
||||||
|
def critical(self, message):
|
||||||
|
self.msg(message)
|
||||||
|
|
||||||
|
|
||||||
@define(slots=True)
|
class RotatingBytesLoggerFactory:
|
||||||
|
"""Factory that creates loggers that respect file rotation."""
|
||||||
|
|
||||||
|
def __init__(self, handler):
|
||||||
|
self.handler = handler
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return RotatingBytesLogger(self.handler)
|
||||||
|
|
||||||
|
|
||||||
|
@define
|
||||||
class AppStructLogger(metaclass=SingletonMetaNoArgs):
|
class AppStructLogger(metaclass=SingletonMetaNoArgs):
|
||||||
_logger: structlog.BoundLogger = field(init=False)
|
_logger: structlog.BoundLogger = field(init=False)
|
||||||
|
|
||||||
@ -40,8 +76,7 @@ class AppStructLogger(metaclass=SingletonMetaNoArgs):
|
|||||||
_log_path = Path(f"{_log_date}_{os.getpid()}.log")
|
_log_path = Path(f"{_log_date}_{os.getpid()}.log")
|
||||||
_handler = RotatingFileHandler(
|
_handler = RotatingFileHandler(
|
||||||
filename=_log_path,
|
filename=_log_path,
|
||||||
mode="a",
|
maxBytes=10 * 1024 * 1024, # 10MB
|
||||||
maxBytes=10 * 1024 * 1024,
|
|
||||||
backupCount=5,
|
backupCount=5,
|
||||||
encoding="utf-8"
|
encoding="utf-8"
|
||||||
)
|
)
|
||||||
@ -55,9 +90,7 @@ class AppStructLogger(metaclass=SingletonMetaNoArgs):
|
|||||||
structlog.processors.TimeStamper(fmt="iso", utc=True),
|
structlog.processors.TimeStamper(fmt="iso", utc=True),
|
||||||
structlog.processors.JSONRenderer(serializer=orjson.dumps),
|
structlog.processors.JSONRenderer(serializer=orjson.dumps),
|
||||||
],
|
],
|
||||||
logger_factory=structlog.BytesLoggerFactory(
|
logger_factory=RotatingBytesLoggerFactory(_handler)
|
||||||
file=BytesToTextIOWrapper(_handler)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self._logger = structlog.get_logger()
|
self._logger = structlog.get_logger()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user