Package: logger-tg
logger-tglogger-tgis a Python package that seamlessly integrates logging with Telegram, allowing developers to receive log messages directly in a Telegram chat. This tool is especially useful for monitoring applications in real-time, receiving immediate error notifications, and keeping track of important information without the need to constantly check log files.FeaturesEasy Integration:Set up with just a few lines of code.Real-Time Notifications:Receive log messages instantly on your Telegram.Flexible Logging Levels:Supports all standard logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).Customizable:Easily configure the logger to suit your needs, including custom message formatting.Asynchronous Support:Utilizes asynchronous communication with Telegram for efficient message delivery.Dynamic Logging Methods:Use custom method names to dynamically log messages at any level, enhancing code readability and simplifying the search for log output locations.Installationlogger-tgcan be installed using pip. Ensure you have Python 3.6 or newer.pipinstalllogger-tgQuick StartTo get started withlogger-tg, you need to configure it with your Telegram bot token and recipient chat ID. Here's a quick guide:Create a Telegram Bot:If you haven't already, create a bot by chatting withBotFatheron Telegram and save the bot token.Find Your Chat ID:You can use the@userinfoboton Telegram to find your chat ID.Basic Setupfromlogger_tgimportconfigure_logger,BaseLogger# Configure global settingsconfigure_logger(bot_token='your_bot_token',recipient_id=your_chat_id)# Initialize the loggerlogger=BaseLogger('MyAppLogger')# Start logginglogger.info('This is a test message!')ConfigurationCustom Log Handlerslogger-tgallows for custom log handlers, enabling you to log to files, console, and Telegram simultaneously.fromlogging.handlersimportTimedRotatingFileHandlerfromlogger_tgimportBaseLogger,TgLoggerSettings# File handler for logging to a filefile_handler=TimedRotatingFileHandler('app.log',when='midnight',interval=1)file_handler.suffix='%Y-%m-%d'# Console handler for logging to stderrconsole_handler=logging.StreamHandler()# Initialize the logger with custom handlerslogger=BaseLogger().get_logger('MyAppLogger',console_log_handler=console_handler,file_log_handler=file_handler)logger.info('Logging to console and file.')Dynamic Logging Methodslogger-tgsupports dynamic logging methods, allowing you to log messages at any level with a single line of code. This feature enhances flexibility and readability by letting you specify the log level directly in the method call.Examplefromlogger_tgimportBaseLoggerlogger=BaseLogger(__name__)logger.dynamic_method('error','An error occurred!')# output:# ERROR: dynamic_method: An error occurred!This will log an error message "dynamic_method: An error occurred!" using the dynamic method feature. The first argument after the method name specifies the log level, making it straightforward to adjust the severity of your logs on the fly.Using dynamic methods not only makes your code more intuitive but also simplifies locating where specific logs are generated, especially when debugging or monitoring your application.Advanced Usagelogger-tgalso supports asynchronous logging to Telegram, which can be particularly useful for applications with high logging throughput or when you wish to avoid blocking the main thread.Asynchronous Error Logging
To log errors asynchronously to Telegram:importasynciofromlogger_tgimportBaseLoggerlogger=BaseLogger('AsyncAppLogger')asyncdeflog_error():logger.error('Asynchronous error logging.')# Running the asynchronous log functionasyncio.run(log_error())This approach ensures that your application remains responsive, even when sending multiple log messages to Telegram.ContributingContributions are welcome! If you'd like to contribute, please fork the repository and use a main branch. Pull requests are warmly welcome.Licenselogger-tgis available under the MIT license. See theLICENSEfile for more info.AcknowledgmentsThanks to the authors ofaiohttp.