1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Better log messages

This commit is contained in:
pieter 2015-09-08 19:47:55 +02:00
parent 9f04f0ece2
commit 710fc273d6
3 changed files with 17 additions and 9 deletions

View File

@ -145,7 +145,7 @@ TeleBot supports the following filters:
|name|argument(s)|Condition|
|:---:|---| ---|
|content_types|list of strings (default `['text']`)|`True` if message.content_type is in the list of strings.|
|regexp|a regular expression as a string|`True` if `re.search(regexp_arg)` returns `True` and `message.content_type == 'text'`|
|regexp|a regular expression as a string|`True` if `re.search(regexp_arg)` returns `True` and `message.content_type == 'text'` (See [Python Regular Expressions](https://docs.python.org/2/library/re.html)|
|commands|list of strings|`True` if `message.content_type == 'text'` and `message.text` starts with a command that is in the list of strings.|
|func|a function (lambda or function reference)|`True` if the lambda or function reference returns `True`

View File

@ -4,10 +4,17 @@ from __future__ import print_function
import threading
import time
import re
import sys
import logging
logging.basicConfig()
logger = logging.getLogger('Telebot')
logger = logging.getLogger('TeleBot')
formatter = logging.Formatter('%(asctime)s (%(filename)s:%(lineno)d) %(levelname)s - %(name)s: "%(message)s"')
ch = logging.StreamHandler(sys.stderr)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.ERROR)
from telebot import apihelper, types, util
@ -72,7 +79,7 @@ class TeleBot:
self.last_update_id = update['update_id']
msg = types.Message.de_json(update['message'])
new_messages.append(msg)
logger.debug('GET %d new messages' % len(new_messages))
logger.debug('Received {} new messages'.format(len(new_messages)))
if len(new_messages) > 0:
self.process_new_messages(new_messages)
@ -113,13 +120,13 @@ class TeleBot:
try:
time.sleep(.1)
except KeyboardInterrupt:
logger.info("TeleBot: Received KeyboardInterrupt: Stopping")
logger.info("Received KeyboardInterrupt. Stopping.")
self.stop_polling()
self.polling_thread.join()
break
def __polling(self, none_stop, interval):
logger.info('TeleBot: Started polling.')
logger.info('Started polling.')
error_interval = .25
while not self.__stop_polling.wait(interval):
@ -129,13 +136,13 @@ class TeleBot:
except apihelper.ApiException as e:
if not none_stop:
self.__stop_polling.set()
logger.info("TeleBot: Exception occurred. Stopping.")
logger.info("Exception occurred. Stopping.")
else:
time.sleep(error_interval)
error_interval *= 2
logger.error(e)
logger.info('TeleBot: Stopped polling.')
logger.info('Stopped polling.')
def stop_polling(self):
self.__stop_polling.set()

View File

@ -19,8 +19,9 @@ def _make_request(token, method_name, method='get', params=None, files=None):
:return: The result parsed to a JSON dictionary.
"""
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
logger.debug("Request: method={} url={} params={} files={}".format(method, request_url, params, files))
result = requests.request(method, request_url, params=params, files=files)
logger.debug(result.text)
logger.debug("The server returned: '{}'".format(result.text))
return _check_result(method_name, result)['result']