Package: python-telegram-handler
A python logging handler that sends logs via Telegram Bot ApiInstallationVia pip:pipinstallpython-telegram-handlerUsageRegister a new telegram bot and obtain aauthentication token. (Instructions herehttps://core.telegram.org/bots#3-how-do-i-create-a-bot)After that, you must obtain achat_id. You can do that using included simple script. Start a new conversation with newly created bot, write something to it (it is important to initiate conversation first).Also, there is an ability for handler to automatically retrievechat_id. This will be done on handler initialization. But you still have to start a conversation with bot. Be aware: if program stops, server restarts, or something else will happen - handler will try to retrieve chat id from telegram, and may fail, if it will not find a NEW message from you. So i recommend you to use a script below for obtaining chat id.Then run a command:python-mtelegram_handler<yourtokenhere>If all went ok, achat_idwill be printed to stdout.Usingtokenandchat_id, configure log handler in your desired way.
For example, using dictConfig:{'version':1,'handlers':{'telegram':{'class':'telegram_handler.TelegramHandler','token':'your token','chat_id':'chat id'}},'loggers':{'my_logger':{'handlers':['telegram'],'level':'DEBUG'}}}FormattingCurrently the format of sent messages is very simple:<code>%(asctime)s</code><b>%(levelname)s</b>\nFrom%(name)s:%(funcName)s\n%(message)sException traceback will be formatted as pre-formatted fixed-width code block. (https://core.telegram.org/bots/api#html-style)If you want to tweak it, configure atelegram_handler.HtmlFormatterwith your desired format string.
Using a dictConfig:...{'formatters':{'telegram':{'class':'telegram_handler.HtmlFormatter','fmt':'%(levelname)s%(message)s'}}'handlers':{'telegram':{'class':'telegram_handler.TelegramHandler','formatter':'telegram','token':'your token','chat_id':'chat id'}}}...If you wish, you can enable emoji symbols in HtmlFormatter. Just specifyuse_emoji=Truein HtmlFormatter settings.
This will add to levelname a :white_circle: for DEBUG, :large_blue_circle: for INFO, and :red_circle: for WARNING and ERROR levels.ProxyIn case if you have to use this package inside the country where Telegram servers are blocked by gowrnment you can specify proxy urls in config.
Using a dictConfig:...{'handlers':{'telegram':{'class':'telegram_handler.TelegramHandler','formatter':'telegram','token':'your token','chat_id':'chat id','proxies':{'http':'socks5://user:pass@host:port','https':'socks5://user:pass@host:port'}}}}...Important!If you plan to usesocksproxy make sure you haverequestspackage withsockssupport installed:pipinstallrequests[socks]History2.2.0 (2019-08-19)Add proxy support (#20) (by )2.1.0 (2019-04-25)Send message as file if it is too largeFixes in MarkdownFormatterFixes in HTMLFormatterChat id retrieval fixesDrop py33 support from testsUpdate setup.py classifiersRemoved print()-calls in HtmlFormatter (#12) (by Lukas Garberg)2.0.2 (2017-11-20)fix TypeError in HtmlFormatter.format (by tompipen)2.0 (2017-03-01)Refactored HtmlFormatter and MarkdownFormatterRefactored TelegramHandlerNo more need to call a command to obtain a chat_id - it will be obtained automatically on handler initRewritten testsEscape LogRecord things in HtmlFormatterAdded optional emoji symbols in HtmlFormatter.1.1.3 (2016-09-22)Setting escape_message field of StyledFormatter missed (@ihoru)1.1.2 (2016-05-13)Fixed setup.py requires option (changed to install_requires)1.1.1 (2016-04-20)Use HTML Formatter as default formatter for telegram handler1.1.0 (2016-04-20)Introduced HTML FormatterAdded log text escaping (closed #3)Added requests timeout setting (closed  #1)Catching and logging all requests and their exceptions (closed #2)1.0.0 (2016-04-19)First PyPi release0.1.0 (2016-04-19)Initial release