2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Enable logging using debug=True

This commit is contained in:
Dan 2018-04-23 13:37:50 +02:00
parent 7054310138
commit 3d9b1cd4b0

View File

@ -26,6 +26,7 @@ import os
import re import re
import shutil import shutil
import struct import struct
import sys
import tempfile import tempfile
import threading import threading
import time import time
@ -57,6 +58,9 @@ from .input_media_video import InputMediaVideo
from .style import Markdown, HTML from .style import Markdown, HTML
from .syncer import Syncer 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__) log = logging.getLogger(__name__)
@ -257,16 +261,32 @@ class Client:
""" """
self.dispatcher.add_handler(handler, group) 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. """Use this method to start the Client after creating it.
Requires no parameters. 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: Raises:
:class:`Error <pyrogram.Error>` :class:`Error <pyrogram.Error>`
""" """
if self.is_started: if self.is_started:
raise ConnectionError("Client has already been 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): if self.BOT_TOKEN_RE.match(self.session_name):
self.token = self.session_name self.token = self.session_name
self.session_name = self.session_name.split(":")[0] self.session_name = self.session_name.split(":")[0]