From 3d9b1cd4b00df1209a580a67e275bf63fa9b104f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 23 Apr 2018 13:37:50 +0200 Subject: [PATCH] Enable logging using debug=True --- pyrogram/client/client.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index ce7f4a27..6276793a 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -26,6 +26,7 @@ import os import re import shutil import struct +import sys import tempfile import threading import time @@ -57,6 +58,9 @@ from .input_media_video import InputMediaVideo from .style import Markdown, HTML from .syncer import Syncer +# Custom format for nice looking log lines +LOG_FORMAT = "[%(asctime)s.%(msecs)03d] %(filename)s:%(lineno)s %(levelname)s: %(message)s" + log = logging.getLogger(__name__) @@ -257,16 +261,32 @@ class Client: """ self.dispatcher.add_handler(handler, group) - def start(self): + def start(self, debug: bool = False): """Use this method to start the Client after creating it. Requires no parameters. + Args: + debug (``bool``, optional): + Enable or disable debug mode. When enabled, extra logging + lines will be printed out on your console. + Raises: :class:`Error ` """ if self.is_started: raise ConnectionError("Client has already been started") + logging.basicConfig( + format=LOG_FORMAT, + datefmt="%Y-%m-%d %H:%M:%S", + stream=sys.stdout + ) + + if debug: + logging.getLogger().setLevel(logging.INFO) + else: + logging.getLogger().setLevel(logging.WARNING) + if self.BOT_TOKEN_RE.match(self.session_name): self.token = self.session_name self.session_name = self.session_name.split(":")[0]