X Tutup
Skip to content

Commit a034317

Browse files
committed
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 python-telegram-bot#21
1 parent d90a4c9 commit a034317

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

examples/echobot.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@
22

33
'''Simple Bot to reply Telegram messages'''
44

5+
import logging
56
import telegram
67
import time
78

8-
# Telegram Bot Authorization Token
9-
bot = telegram.Bot('TOKEN')
109

11-
# This will be our global variable to keep the latest update_id when requesting
12-
# for updates. It starts with the latest update_id if available.
13-
try:
14-
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
15-
except IndexError:
16-
LAST_UPDATE_ID = None
10+
LAST_UPDATE_ID = None
1711

1812

19-
def echo():
13+
def main():
14+
global LAST_UPDATE_ID
15+
16+
logging.basicConfig(
17+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18+
19+
# Telegram Bot Authorization Token
20+
bot = telegram.Bot('TOKEN')
21+
22+
# This will be our global variable to keep the latest update_id when requesting
23+
# for updates. It starts with the latest update_id if available.
24+
try:
25+
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
26+
except IndexError:
27+
LAST_UPDATE_ID = None
28+
29+
while True:
30+
echo(bot)
31+
time.sleep(3)
32+
33+
34+
def echo(bot):
2035
global LAST_UPDATE_ID
2136

2237
# Request updates from last updated_id
@@ -36,6 +51,4 @@ def echo():
3651

3752

3853
if __name__ == '__main__':
39-
while True:
40-
echo()
41-
time.sleep(3)
54+
main()

examples/roboed.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
__author__ = 'leandrotoledodesouza@gmail.com'
77

8+
import logging
89
import telegram
910
import urllib
1011

1112

1213
def main():
14+
logging.basicConfig(
15+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
1316
bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token
1417

15-
global LAST_UPDATE_ID
1618
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update
1719

1820
while True:

telegram/bot.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
1919
ReplyMarkup, InputFile, TelegramObject)
2020

21-
logging.basicConfig(
22-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
23-
2421

2522
class Bot(TelegramObject):
2623

telegram_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import logging
12
import unittest
23
from tests.test_bot import BotTest
34

45
if __name__ == '__main__':
6+
logging.basicConfig(
7+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
58
testsuite = unittest.TestLoader().loadTestsFromTestCase(BotTest)
69
unittest.TextTestRunner(verbosity=1).run(testsuite)

0 commit comments

Comments
 (0)
X Tutup