Logging to telegram, or the story of how I made a python library

The python logging module is a powerful development tool. It helps you track errors, monitor application performance, and even collect statistics about the use of your service. In this article I will tell you how you can expand the capabilities of this module and where does the telegram come from.





Intro

-, , , . , GPU (colab). , . , , runtime , , runtime , .





, - , , . - , , , . 





(jupyter notebook) , , logging , , . 





Logging.handlers

, , . , , , . , . , logging , StreamHandler. , , .





Tg-logger

, , . , .





, , , , @tg_logger_demo_bot.





:





  • ( )





  • user_id ( @tg_logger_demo_bot /id



    )





pip.





pip install tg-logger
      
      







import logging
import tg_logger

# Telegram data
token = "1234567890:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
users = [1111111111]

# Base logger
logger = logging.getLogger('foo')
logger.setLevel(logging.INFO)

# Logging bridge setup
tg_logger.setup(logger, token=token, users=users)

# Test
logger.info("Hello from tg_logger by otter18")

      
      



, .





# Logging bridge setup
tg_logger.setup(logger, token=token, users=users)
      
      



setup() logger, . , setup(). , , , .





TgFileLogger

It is also worth mentioning that I also added the function of sending files.





import tg_logger

# Telegram data
token = "1234567890:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
users = [1111111111]

# TgFileLogger example
tg_files_logger = tg_logger.TgFileLogger(
    token=token,  # tg bot token
    users=users,  # list of user_id
    timeout=10  # 10 seconds by default
)

file_name = "test.txt"
with open(file_name, 'w') as example_file:
    example_file.write("Hello from tg_logger by otter18")

tg_files_logger.send(file_name, "Test file")
      
      



Outro

  • All source code is on github: github.com/otter18/tg_logger .





  • English documentation is available at Read the Docs .





  • The library is available for download via pip : pypi.org/project/tg-logger/ .








All Articles