Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import structlog
- import sys
- from sqlalchemy import create_engine
- # Настраиваем стандартное логирование
- logging.basicConfig(
- filename="bot.log",
- level=logging.INFO,
- format="%(message)s",
- )
- # Конфигурация structlog для вывода в JSON
- structlog.configure(
- processors=[
- structlog.processors.JSONRenderer()
- ],
- context_class=dict,
- logger_factory=structlog.stdlib.LoggerFactory(),
- wrapper_class=structlog.stdlib.BoundLogger,
- cache_logger_on_first_use=True,
- )
- # Пример инициализации логгера
- logger = structlog.get_logger()
- # Настраиваем логирование для SQLAlchemy
- class StructlogHandler(logging.Handler):
- def emit(self, record):
- logger = structlog.get_logger(record.name)
- logger.log(record.levelno, record.getMessage())
- # Логи SQLAlchemy через structlog
- sqlalchemy_logger = logging.getLogger('sqlalchemy.engine')
- sqlalchemy_logger.setLevel(logging.INFO)
- sqlalchemy_logger.addHandler(StructlogHandler())
- # Настройка SQLAlchemy
- engine = create_engine('sqlite:///example.db', echo=False)
- # Пример использования
- async def main():
- await ainfo("Bot started")
- # Пример использования движка SQLAlchemy
- with engine.connect() as conn:
- result = conn.execute("SELECT 1")
- await ainfo(f"SQL Result: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement