diff --git a/examples/echobot.py b/examples/echobot.py index ac1e6ab..f504ed5 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -1,4 +1,4 @@ -from fbchat import log, Client +from fbchat import Client # Subclass fbchat.Client and override required methods class EchoBot(Client): @@ -6,7 +6,7 @@ class EchoBot(Client): self.markAsDelivered(thread_id, message_object.uid) self.markAsRead(thread_id) - log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name)) + print("{} from {} in {}".format(message_object, thread_id, thread_type.name)) # If you're not the author, echo if author_id != self.uid: diff --git a/examples/keepbot.py b/examples/keepbot.py index 73a213c..43b4382 100644 --- a/examples/keepbot.py +++ b/examples/keepbot.py @@ -1,4 +1,4 @@ -from fbchat import log, Client +from fbchat import Client from fbchat.models import * # Change this to your group id @@ -19,21 +19,21 @@ old_nicknames = { class KeepBot(Client): def onColorChange(self, author_id, new_color, thread_id, thread_type, **kwargs): if old_thread_id == thread_id and old_color != new_color: - log.info( + print( "{} changed the thread color. It will be changed back".format(author_id) ) self.changeThreadColor(old_color, thread_id=thread_id) def onEmojiChange(self, author_id, new_emoji, thread_id, thread_type, **kwargs): if old_thread_id == thread_id and new_emoji != old_emoji: - log.info( + print( "{} changed the thread emoji. It will be changed back".format(author_id) ) self.changeThreadEmoji(old_emoji, thread_id=thread_id) def onPeopleAdded(self, added_ids, author_id, thread_id, **kwargs): if old_thread_id == thread_id and author_id != self.uid: - log.info("{} got added. They will be removed".format(added_ids)) + print("{} got added. They will be removed".format(added_ids)) for added_id in added_ids: self.removeUserFromGroup(added_id, thread_id=thread_id) @@ -44,12 +44,12 @@ class KeepBot(Client): and removed_id != self.uid and author_id != self.uid ): - log.info("{} got removed. They will be re-added".format(removed_id)) + print("{} got removed. They will be re-added".format(removed_id)) self.addUsersToGroup(removed_id, thread_id=thread_id) def onTitleChange(self, author_id, new_title, thread_id, thread_type, **kwargs): if old_thread_id == thread_id and old_title != new_title: - log.info( + print( "{} changed the thread title. It will be changed back".format(author_id) ) self.changeThreadTitle( @@ -64,7 +64,7 @@ class KeepBot(Client): and changed_for in old_nicknames and old_nicknames[changed_for] != new_nickname ): - log.info( + print( "{} changed {}'s' nickname. It will be changed back".format( author_id, changed_for ) diff --git a/examples/removebot.py b/examples/removebot.py index 8b1a941..61571fb 100644 --- a/examples/removebot.py +++ b/examples/removebot.py @@ -1,4 +1,4 @@ -from fbchat import log, Client +from fbchat import Client from fbchat.models import * @@ -6,7 +6,7 @@ class RemoveBot(Client): def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs): # We can only kick people from group chats, so no need to try if it's a user chat if message_object.text == "Remove me!" and thread_type == ThreadType.GROUP: - log.info("{} will be removed from {}".format(author_id, thread_id)) + print("{} will be removed from {}".format(author_id, thread_id)) self.removeUserFromGroup(author_id, thread_id=thread_id) else: # Sends the data to the inherited onMessage, so that we can still see when a message is recieved diff --git a/fbchat/__init__.py b/fbchat/__init__.py index fa58ba9..afc2ae2 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -3,6 +3,10 @@ :copyright: (c) 2015 - 2019 by Taehoon Kim :license: BSD 3-Clause, see LICENSE for more details. """ +import logging as _logging + +# Set default logging handler to avoid "No handler found" warnings. +_logging.getLogger(__name__).addHandler(_logging.NullHandler()) # These imports are far too general, but they're needed for backwards compatbility. from .models import * diff --git a/fbchat/_client.py b/fbchat/_client.py index ec7efee..451bc01 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -56,13 +56,7 @@ class Client: return self._uid def __init__( - self, - email, - password, - user_agent=None, - max_tries=5, - session_cookies=None, - logging_level=logging.INFO, + self, email, password, user_agent=None, max_tries=5, session_cookies=None ): """Initialize and log in the client. @@ -72,7 +66,6 @@ class Client: user_agent: Custom user agent to use when sending requests. If `None`, user agent will be chosen from a premade list max_tries (int): Maximum number of times to try logging in session_cookies (dict): Cookies from a previous session (Will default to login if these are invalid) - logging_level (int): Configures the `logging level `_. Defaults to ``logging.INFO`` Raises: FBchatException: On failed login @@ -85,8 +78,6 @@ class Client: self._markAlive = True self._buddylist = dict() - handler.setLevel(logging_level) - # If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login if ( not session_cookies diff --git a/fbchat/_core.py b/fbchat/_core.py index e6df0aa..693584f 100644 --- a/fbchat/_core.py +++ b/fbchat/_core.py @@ -1,7 +1,7 @@ import logging import aenum -log = logging.getLogger("client") +log = logging.getLogger("fbchat") class Enum(aenum.Enum): diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 9a186ec..fb9f857 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -1,5 +1,6 @@ import json import re +from ._core import log from . import _util from ._exception import FBchatException @@ -54,7 +55,7 @@ def response_to_json(content): else: rtn[int(key[1:])] = value["data"] - _util.log.debug(rtn) + log.debug(rtn) return rtn diff --git a/fbchat/_message.py b/fbchat/_message.py index 9f6afa6..672f6c1 100644 --- a/fbchat/_message.py +++ b/fbchat/_message.py @@ -1,8 +1,8 @@ import attr import json from string import Formatter +from ._core import log, Enum from . import _util, _attachment, _location, _file, _quick_reply, _sticker -from ._core import Enum class EmojiSize(Enum): @@ -324,7 +324,7 @@ class Message: for mention in _util.parse_json(data["data"]["prng"]) ] except Exception: - _util.log.exception("An exception occured while reading attachments") + log.exception("An exception occured while reading attachments") if data.get("attachments"): try: @@ -361,7 +361,7 @@ class Message: rtn.attachments.append(attachment) except Exception: - _util.log.exception( + log.exception( "An exception occured while reading attachments: {}".format( data["attachments"] ) diff --git a/fbchat/_state.py b/fbchat/_state.py index 8062b16..a81adf0 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -4,6 +4,7 @@ import re import requests import random +from ._core import log from . import _graphql, _util, _exception FB_DTSG_REGEX = re.compile(r'name="fb_dtsg" value="(.*?)"') @@ -50,7 +51,7 @@ def _2fa_helper(session, code, r): data["nh"] = soup.find("input", {"name": "nh"})["value"] data["submit[Submit Code]"] = "Submit Code" data["codes_submitted"] = 0 - _util.log.info("Submitting 2FA code.") + log.info("Submitting 2FA code.") r = session.post(url, data=data) @@ -63,7 +64,7 @@ def _2fa_helper(session, code, r): data["name_action_selected"] = "save_device" data["submit[Continue]"] = "Continue" - _util.log.info("Saving browser.") + log.info("Saving browser.") # At this stage, we have dtsg, nh, name_action_selected, submit[Continue] r = session.post(url, data=data) @@ -71,7 +72,7 @@ def _2fa_helper(session, code, r): return r del data["name_action_selected"] - _util.log.info("Starting Facebook checkup flow.") + log.info("Starting Facebook checkup flow.") # At this stage, we have dtsg, nh, submit[Continue] r = session.post(url, data=data) @@ -80,7 +81,7 @@ def _2fa_helper(session, code, r): del data["submit[Continue]"] data["submit[This was me]"] = "This Was Me" - _util.log.info("Verifying login attempt.") + log.info("Verifying login attempt.") # At this stage, we have dtsg, nh, submit[This was me] r = session.post(url, data=data) @@ -90,7 +91,7 @@ def _2fa_helper(session, code, r): del data["submit[This was me]"] data["submit[Continue]"] = "Continue" data["name_action_selected"] = "save_device" - _util.log.info("Saving device again.") + log.info("Saving device again.") # At this stage, we have dtsg, nh, submit[Continue], name_action_selected r = session.post(url, data=data) return r @@ -207,7 +208,7 @@ class State: def _do_refresh(self): # TODO: Raise the error instead, and make the user do the refresh manually # It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! - _util.log.warning("Refreshing state and resending request") + log.warning("Refreshing state and resending request") new = State.from_session(session=self._session) self.user_id = new.user_id self._fb_dtsg = new._fb_dtsg diff --git a/fbchat/_util.py b/fbchat/_util.py index cb8a677..873da23 100644 --- a/fbchat/_util.py +++ b/fbchat/_util.py @@ -7,8 +7,8 @@ from mimetypes import guess_type from os.path import basename from urllib.parse import parse_qs, urlparse import warnings -import logging import requests +from ._core import log from ._exception import ( FBchatException, FBchatFacebookError, @@ -17,13 +17,6 @@ from ._exception import ( FBchatPleaseRefresh, ) -# Log settings -log = logging.getLogger("client") -log.setLevel(logging.DEBUG) -# Creates the console handler -handler = logging.StreamHandler() -log.addHandler(handler) - #: Default list of user agents USER_AGENTS = [ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",