Skip to content

Commit c0861dd

Browse files
committed
Fixed logging
1 parent 317a304 commit c0861dd

25 files changed

+117
-136
lines changed

backend.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ ENV PYTHONDONTWRITEBYTECODE 1
77
EXPOSE 5000
88
ADD .env .
99
RUN export $(cat .env | xargs)
10-
CMD exec gunicorn --chdir /www --bind :5000 --workers 1 --threads 1 PyMatcha:application
10+
CMD exec gunicorn --chdir /www --bind :5000 --workers 1 --threads 1 PyMatcha:application --log-level INFO

backend/PyMatcha/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"FLASK_DEBUG",
4848
"FLASK_HOST",
4949
"FLASK_SECRET_KEY",
50-
"ENABLE_LOGGING",
5150
"CELERY_BROKER_URL",
5251
"CELERY_RESULT_BACKEND",
5352
"DB_HOST",
@@ -69,7 +68,6 @@
6968
raise EnvironmentError(f"{item} is not set in the server's environment or .env file. It is required.")
7069

7170
from PyMatcha.utils.static import (
72-
ENABLE_LOGGING,
7371
FLASK_SECRET_KEY,
7472
CELERY_RESULT_BACKEND,
7573
CELERY_BROKER_URL,
@@ -83,8 +81,7 @@
8381
REDIS_HOST,
8482
)
8583

86-
if ENABLE_LOGGING == "True":
87-
setup_logging()
84+
setup_logging()
8885

8986
sentry_sdk.init(
9087
dsn="https://bb17c14c99d448e2804bf2f105d4ec52@o450203.ingest.sentry.io/5434438",

backend/PyMatcha/models/notification.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from typing import Dict
2424
from typing import Optional
2525

26-
from flask import current_app
2726
from PyMatcha.utils import create_notifications_table
2827
from PyMatcha.utils.orm import Field
2928
from PyMatcha.utils.orm import Model
@@ -62,7 +61,7 @@ def create(
6261
return None
6362
if not dt_received:
6463
dt_received = datetime.utcnow()
65-
current_app.logger.debug(f"Creating notification for user {user_id} from {trigger_id} with type {type}")
64+
logging.debug(f"Creating notification for user {user_id} from {trigger_id} with type {type}")
6665
new_notif = Notification(
6766
user_id=user_id, content=content, type=type, link_to=link_to, is_seen=is_seen, dt_received=dt_received
6867
)

backend/PyMatcha/models/user.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,15 @@ def get_likes_sent(self):
272272
return Like.get_multis(liker_id=self.id)
273273

274274
def get_blocks(self):
275+
logging.debug("Getting all blocks for user {}".format(self.id))
275276
return Block.get_multis(blocker_id=self.id)
276277

277278
def get_all_notifications(self):
279+
logging.debug("Getting all notifications for user {}".format(self.id))
278280
return Notification.get_multis(user_id=self.id)
279281

280282
def get_unread_notifications(self):
283+
logging.debug("Getting all unread notifications for user {}".format(self.id))
281284
return Notification.get_multis(user_id=self.id, is_seen=False)
282285

283286
def already_likes(self, liked_id: int) -> bool:

backend/PyMatcha/routes/api/auth/email.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
1919
import datetime
20+
import logging
2021

2122
from flask import Blueprint
22-
from flask import current_app
2323
from flask import render_template
2424
from flask import request
2525
from itsdangerous import BadSignature
@@ -46,27 +46,27 @@ def confirm_email(token):
4646
email, token_type = confirm_token(token, expiration=7200)
4747
except (SignatureExpired, BadSignature) as e:
4848
if e == SignatureExpired:
49-
current_app.logger.debug(f"Signature Expired for token {token}")
49+
logging.debug(f"Signature Expired for token {token}")
5050
raise BadRequestError("Signature Expired.", "Request another email confirmation and try again.")
5151
else:
52-
current_app.logger.debug(f"Bad Expired for token {token}")
52+
logging.debug(f"Bad Expired for token {token}")
5353
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
5454
else:
5555
if token_type != "confirm":
56-
current_app.logger.debug(f"Wrong token type for token {token}")
56+
logging.debug(f"Wrong token type for token {token}")
5757
raise BadRequestError("Wrong token type.")
5858
try:
5959
u = get_user(email)
6060
except NotFoundError:
6161
raise NotFoundError("User not found.")
6262
if u.is_confirmed:
63-
current_app.logger.debug("User already confirmed")
63+
logging.debug("User already confirmed")
6464
raise BadRequestError("Email already confirmed.", "")
6565
else:
6666
u.is_confirmed = True
6767
u.confirmed_on = datetime.datetime.utcnow()
6868
u.save()
69-
current_app.logger.debug(f"User {u.id} confirmed.")
69+
logging.debug(f"User {u.id} confirmed.")
7070
return Success("Confirmation successful.")
7171

7272

@@ -81,12 +81,12 @@ def request_new_email_conf():
8181
pass
8282
else:
8383
if u.is_confirmed:
84-
current_app.logger.debug("Already confirmed.")
84+
logging.debug("Already confirmed.")
8585
else:
86-
current_app.logger.debug("/auth/confirm/new -> User found, sending new confirmation email")
86+
logging.debug("/auth/confirm/new -> User found, sending new confirmation email")
8787
token = generate_confirmation_token(email=email, token_type="confirm")
8888
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
8989
rendered_html = render_template("confirm_email.html", link=link)
9090
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
91-
current_app.logger.debug("/auth/confirm/new -> New confirmation email sent if user exists in database")
91+
logging.debug("/auth/confirm/new -> New confirmation email sent if user exists in database")
9292
return Success("New confirmation email sent if user exists in database and isn't already confirmed.")

backend/PyMatcha/routes/api/auth/login.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
1919
import datetime
20+
import logging
2021

2122
from flask import Blueprint
22-
from flask import current_app
2323
from flask import request
2424
from flask_jwt_extended import create_access_token
2525
from flask_jwt_extended import create_refresh_token
@@ -51,16 +51,16 @@ def auth_login():
5151
try:
5252
u = get_user(username)
5353
except NotFoundError:
54-
current_app.logger.debug("User not found for login")
54+
logging.debug("User not found for login")
5555
raise UnauthorizedError("Incorrect username or password.")
5656
if not check_password(u.password, password):
57-
current_app.logger.debug("Password invalid for login")
57+
logging.debug("Password invalid for login")
5858
raise UnauthorizedError("Incorrect username or password.")
5959

6060
if not u.is_confirmed:
61-
current_app.logger.debug("User is trying to login unconfirmed")
61+
logging.debug("User is trying to login unconfirmed")
6262
raise UnauthorizedError("User needs to be confirmed first.", "Try again when you have confirmed your email.")
63-
current_app.logger.debug(f"Creating token for {u.id}")
63+
logging.debug(f"Creating token for {u.id}")
6464
access_token = create_access_token(identity=u.get_jwt_info(), fresh=True)
6565
refresh_token = create_refresh_token(identity=u.get_jwt_info())
6666
access_jti = get_jti(access_token)
@@ -69,7 +69,7 @@ def auth_login():
6969
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
7070
redis.set("is_revoked_jti:" + refresh_jti, "false", REFRESH_TOKEN_EXPIRES * 1.2)
7171

72-
current_app.logger.debug("Returning access token for user {}".format(username))
72+
logging.debug("Returning access token for user {}".format(username))
7373
u.is_online = True
7474
u.dt_lastseen = datetime.datetime.utcnow()
7575
u.save()
@@ -81,7 +81,7 @@ def auth_login():
8181
@jwt_refresh_token_required
8282
def refresh():
8383
current_user = get_jwt_identity()
84-
current_app.logger.info(f"Refreshing token for {current_user['id']}")
84+
logging.info(f"Refreshing token for {current_user['id']}")
8585
access_token = create_access_token(identity=current_user)
8686
access_jti = get_jti(encoded_token=access_token)
8787
redis.set("is_revoked_jti:" + access_jti, "false", ACCESS_TOKEN_EXPIRES * 1.2)
@@ -98,5 +98,5 @@ def logout():
9898
refresh_jti = get_jti(refresh_token)
9999
redis.set("is_revoked_jti:" + access_jti, "true", ACCESS_TOKEN_EXPIRES * 1.2)
100100
redis.set("is_revoked_jti:" + refresh_jti, "true", REFRESH_TOKEN_EXPIRES * 1.2)
101-
current_app.logger.info(f"Revoked tokens with jtis {access_jti}, {refresh_jti}")
101+
logging.info(f"Revoked tokens with jtis {access_jti}, {refresh_jti}")
102102
return Success("Logout successful.")

backend/PyMatcha/routes/api/auth/password.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19+
import logging
20+
1921
from flask import Blueprint
20-
from flask import current_app
2122
from flask import render_template
2223
from flask import request
2324
from itsdangerous import BadSignature
@@ -46,15 +47,15 @@ def forgot_password():
4647
try:
4748
get_user(data["email"])
4849
except NotFoundError:
49-
current_app.logger.debug("User not found, no email sent")
50+
logging.debug("User not found, no email sent")
5051
pass
5152
else:
5253
token = generate_confirmation_token(email=data["email"], token_type="reset")
5354
link = FRONTEND_PASSWORD_RESET_URL + token
5455
rendered_html = render_template("password_reset.html", link=link)
55-
current_app.logger.debug("Sending worker request to send email")
56+
logging.debug("Sending worker request to send email")
5657
send_mail_html.delay(dest=data["email"], subject="Reset your password on PyMatcha", html=rendered_html)
57-
current_app.logger.debug("Password reset mail sent successfully for user.")
58+
logging.debug("Password reset mail sent successfully for user.")
5859
return Success("Password reset mail sent successfully if user exists in DB.")
5960

6061

@@ -67,26 +68,26 @@ def reset_password():
6768
email, token_type = confirm_token(token, expiration=7200)
6869
except (SignatureExpired, BadSignature) as e:
6970
if e == SignatureExpired:
70-
current_app.logger.debug(f"Signature Expired for {token}")
71+
logging.debug(f"Signature Expired for {token}")
7172
raise BadRequestError("Signature Expired.", "Request another password reset and try again.")
7273
else:
73-
current_app.logger.debug(f"Bad Signature for {token}")
74+
logging.debug(f"Bad Signature for {token}")
7475
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
7576
else:
7677
if token_type != "reset":
77-
current_app.logger.debug(f"Wrong token type for {token}")
78+
logging.debug(f"Wrong token type for {token}")
7879
raise BadRequestError("Wrong token type.")
7980
try:
8081
u = get_user(email)
8182
except NotFoundError:
8283
raise NotFoundError("User not found.")
8384
if u.previous_reset_token == token:
84-
current_app.logger.debug("Token already used")
85+
logging.debug("Token already used")
8586
raise BadRequestError("Token already used", "Please request a new one.")
8687
u.password = hash_password(data["password"])
8788
u.previous_reset_token = token
8889
u.save()
89-
current_app.logger.debug("Password reset successfully")
90+
logging.debug("Password reset successfully")
9091
return Success("Password reset successful.")
9192

9293

@@ -99,17 +100,17 @@ def check_token_validity():
99100
email, token_type = confirm_token(token, expiration=7200)
100101
except (SignatureExpired, BadSignature) as e:
101102
if e == SignatureExpired:
102-
current_app.logger.debug(f"Signature Expired for {token}")
103+
logging.debug(f"Signature Expired for {token}")
103104
raise BadRequestError("Signature Expired.", "Request another password reset and try again.")
104105
else:
105-
current_app.logger.debug(f"Bad Signature for {token}")
106+
logging.debug(f"Bad Signature for {token}")
106107
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
107108
else:
108109
try:
109110
u = get_user(email)
110111
except NotFoundError:
111112
raise NotFoundError("User not found.")
112113
if u.previous_reset_token == token:
113-
current_app.logger.debug("Token already used")
114+
logging.debug("Token already used")
114115
raise BadRequestError("Token already used", "Please request a new one.")
115116
return Success("Reset token is correct.")

backend/PyMatcha/routes/api/auth/register.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19+
import logging
20+
1921
from flask import Blueprint
20-
from flask import current_app
2122
from flask import render_template
2223
from flask import request
2324
from PyMatcha.models.user import User
@@ -39,7 +40,7 @@ def api_create_user():
3940
data = request.get_json()
4041
data["email"] = data["email"].lower()
4142
try:
42-
current_app.logger.debug("Trying to register new user")
43+
logging.debug("Trying to register new user")
4344
new_user = User.register(
4445
email=data["email"],
4546
username=data["username"],
@@ -48,12 +49,12 @@ def api_create_user():
4849
last_name=data["last_name"],
4950
)
5051
except ConflictError as e:
51-
current_app.logger.warning("Conflict error on user register: {}".format(e))
52+
logging.warning("Conflict error on user register: {}".format(e))
5253
raise e
5354
else:
5455
token = generate_confirmation_token(email=data["email"], token_type="confirm")
5556
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
5657
rendered_html = render_template("confirm_email.html", link=link)
5758
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
58-
current_app.logger.info("Registered new user successfully.")
59+
logging.info("Registered new user successfully.")
5960
return SuccessOutputMessage("email", new_user.email, "New user successfully created.")

backend/PyMatcha/routes/api/debug.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
2+
import logging
23

34
from flask import Blueprint
4-
from flask import current_app
55
from flask import jsonify
66
from flask import request
77
from flask_jwt_extended import current_user
@@ -34,19 +34,19 @@ def debug_confirm_user(uid):
3434
"""
3535
This route confirms a given user without having to send and receive an email
3636
"""
37-
current_app.logger.debug("/debug/users/confirm/{} -> Call".format(uid))
37+
logging.debug("/debug/users/confirm/{} -> Call".format(uid))
3838
try:
3939
u = get_user(uid)
4040
except NotFoundError:
41-
current_app.logger.debug("/debug/users/confirm -> User not found")
41+
logging.debug("/debug/users/confirm -> User not found")
4242
raise NotFoundError("User {} not found".format(uid))
4343
if u.is_confirmed:
44-
current_app.logger.debug("/debug/users/confirm -> User already confirmed")
44+
logging.debug("/debug/users/confirm -> User already confirmed")
4545
return Success("User already confirmed.")
4646
u.is_confirmed = True
4747
u.confirmed_on = datetime.datetime.utcnow()
4848
u.save()
49-
current_app.logger.debug("/debug/users/confirm -> User {} confirmed.".format(u.id))
49+
logging.debug("/debug/users/confirm -> User {} confirmed.".format(u.id))
5050
return Success("User successfully confirmed.")
5151

5252

backend/PyMatcha/routes/api/history.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19+
import logging
20+
1921
from flask import Blueprint
20-
from flask import current_app
2122
from flask_jwt_extended import current_user
2223
from flask_jwt_extended import jwt_required
2324
from PyMatcha.models.user import User
@@ -44,7 +45,7 @@ def history_viewed_people():
4445
user_dict["common_tags"] = common_tags
4546
user_dict["distance"] = distance
4647
viewed_people.append(user_dict)
47-
current_app.logger.info(f"Returning viewed profiles for user {current_user.id}")
48+
logging.info(f"Returning viewed profiles for user {current_user.id}")
4849
return SuccessOutput("viewed", viewed_people)
4950

5051

@@ -63,7 +64,7 @@ def history_viewed_me():
6364
user_dict["common_tags"] = common_tags
6465
user_dict["distance"] = distance
6566
viewed_people.append(user_dict)
66-
current_app.logger.info(f"Returning profiles who viewed user {current_user.id}")
67+
logging.info(f"Returning profiles who viewed user {current_user.id}")
6768
return SuccessOutput("viewed_me", viewed_people)
6869

6970

@@ -82,7 +83,7 @@ def history_liked_people():
8283
user_dict["common_tags"] = common_tags
8384
user_dict["distance"] = distance
8485
liked_people.append(user_dict)
85-
current_app.logger.info(f"Returning liked profiles for user {current_user.id}")
86+
logging.info(f"Returning liked profiles for user {current_user.id}")
8687
return SuccessOutput("liked", liked_people)
8788

8889

@@ -101,7 +102,7 @@ def history_liked_me():
101102
user_dict["common_tags"] = common_tags
102103
user_dict["distance"] = distance
103104
liked_people.append(user_dict)
104-
current_app.logger.info(f"Returning profiles who liked user {current_user.id}")
105+
logging.info(f"Returning profiles who liked user {current_user.id}")
105106
return SuccessOutput("liked_me", liked_people)
106107

107108

@@ -120,5 +121,5 @@ def history_blocked():
120121
user_dict["common_tags"] = common_tags
121122
user_dict["distance"] = distance
122123
blocked_people.append(user_dict)
123-
current_app.logger.info(f"Returning blocked profiles for user {current_user.id}")
124+
logging.info(f"Returning blocked profiles for user {current_user.id}")
124125
return SuccessOutput("blocked", blocked_people)

0 commit comments

Comments
 (0)