Advertisement
JustLooKeD

Logging

Oct 6th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | Source Code | 0 0
  1. import logging
  2. import structlog
  3. import sys
  4. from sqlalchemy import create_engine
  5.  
  6. # Настраиваем стандартное логирование
  7. logging.basicConfig(
  8.     filename="bot.log",
  9.     level=logging.INFO,
  10.     format="%(message)s",
  11. )
  12.  
  13. # Конфигурация structlog для вывода в JSON
  14. structlog.configure(
  15.     processors=[
  16.         structlog.processors.JSONRenderer()
  17.     ],
  18.     context_class=dict,
  19.     logger_factory=structlog.stdlib.LoggerFactory(),
  20.     wrapper_class=structlog.stdlib.BoundLogger,
  21.     cache_logger_on_first_use=True,
  22. )
  23.  
  24. # Пример инициализации логгера
  25. logger = structlog.get_logger()
  26.  
  27. # Настраиваем логирование для SQLAlchemy
  28. class StructlogHandler(logging.Handler):
  29.     def emit(self, record):
  30.         logger = structlog.get_logger(record.name)
  31.         logger.log(record.levelno, record.getMessage())
  32.  
  33. # Логи SQLAlchemy через structlog
  34. sqlalchemy_logger = logging.getLogger('sqlalchemy.engine')
  35. sqlalchemy_logger.setLevel(logging.INFO)
  36. sqlalchemy_logger.addHandler(StructlogHandler())
  37.  
  38. # Настройка SQLAlchemy
  39. engine = create_engine('sqlite:///example.db', echo=False)
  40.  
  41. # Пример использования
  42. async def main():
  43.     await ainfo("Bot started")
  44.     # Пример использования движка SQLAlchemy
  45.     with engine.connect() as conn:
  46.         result = conn.execute("SELECT 1")
  47.         await ainfo(f"SQL Result: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement