From 710fc273d6b6303a3d119f8023eb81fef0b7ddd3 Mon Sep 17 00:00:00 2001 From: pieter Date: Tue, 8 Sep 2015 19:47:55 +0200 Subject: [PATCH] Better log messages --- README.md | 2 +- telebot/__init__.py | 21 ++++++++++++++------- telebot/apihelper.py | 3 ++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 28bb895..54c4485 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/telebot/__init__.py b/telebot/__init__.py index 435f1c8..6e9af53 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -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() diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 1c4625c..62ccfad 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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']