From a034317c87d83b6697d3b4c3896b4ec0a33588b9 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 30 Jul 2015 07:04:59 +0100 Subject: [PATCH] Don't call logging.basicConfig() in library code Logging should be configured by the application, not by libraries it uses. Libraries should just get a logger and log to it. Fixes #21 --- examples/echobot.py | 37 +++++++++++++++++++++++++------------ examples/roboed.py | 4 +++- telegram/bot.py | 3 --- telegram_test.py | 3 +++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/examples/echobot.py b/examples/echobot.py index 4834ca2e2..3ecfc4dfa 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -2,21 +2,36 @@ '''Simple Bot to reply Telegram messages''' +import logging import telegram import time -# Telegram Bot Authorization Token -bot = telegram.Bot('TOKEN') -# This will be our global variable to keep the latest update_id when requesting -# for updates. It starts with the latest update_id if available. -try: - LAST_UPDATE_ID = bot.getUpdates()[-1].update_id -except IndexError: - LAST_UPDATE_ID = None +LAST_UPDATE_ID = None -def echo(): +def main(): + global LAST_UPDATE_ID + + logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') + + # Telegram Bot Authorization Token + bot = telegram.Bot('TOKEN') + + # This will be our global variable to keep the latest update_id when requesting + # for updates. It starts with the latest update_id if available. + try: + LAST_UPDATE_ID = bot.getUpdates()[-1].update_id + except IndexError: + LAST_UPDATE_ID = None + + while True: + echo(bot) + time.sleep(3) + + +def echo(bot): global LAST_UPDATE_ID # Request updates from last updated_id @@ -36,6 +51,4 @@ def echo(): if __name__ == '__main__': - while True: - echo() - time.sleep(3) + main() diff --git a/examples/roboed.py b/examples/roboed.py index f1c0b044f..8a7a19984 100644 --- a/examples/roboed.py +++ b/examples/roboed.py @@ -5,14 +5,16 @@ __author__ = 'leandrotoledodesouza@gmail.com' +import logging import telegram import urllib def main(): + logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token - global LAST_UPDATE_ID LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update while True: diff --git a/telegram/bot.py b/telegram/bot.py index 89c675822..df801605c 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -18,9 +18,6 @@ import logging from telegram import (User, Message, Update, UserProfilePhotos, TelegramError, ReplyMarkup, InputFile, TelegramObject) -logging.basicConfig( - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') - class Bot(TelegramObject): diff --git a/telegram_test.py b/telegram_test.py index 5d624be57..fcb4307e8 100644 --- a/telegram_test.py +++ b/telegram_test.py @@ -1,6 +1,9 @@ +import logging import unittest from tests.test_bot import BotTest if __name__ == '__main__': + logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') testsuite = unittest.TestLoader().loadTestsFromTestCase(BotTest) unittest.TextTestRunner(verbosity=1).run(testsuite)