dev: enable logger instead of printing

This commit is contained in:
pablohashescobar 2023-10-30 14:41:44 +05:30
parent f231ac0a79
commit 93cfa13955
16 changed files with 82 additions and 53 deletions

View File

@ -1,5 +1,6 @@
# Python imports
import zoneinfo
import logging
# Django imports
from django.urls import resolve
@ -58,6 +59,8 @@ class BaseViewSet(TimezoneMixin, ModelViewSet, BasePaginator):
try:
return self.model.objects.all()
except Exception as e:
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
raise APIException("Please check the view", status.HTTP_400_BAD_REQUEST)
@ -81,10 +84,13 @@ class BaseViewSet(TimezoneMixin, ModelViewSet, BasePaginator):
return Response({"error": f"{model_name} does not exist."}, status=status.HTTP_404_NOT_FOUND)
if isinstance(e, KeyError):
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return Response({"error": f"key {e} does not exist"}, status=status.HTTP_400_BAD_REQUEST)
print(e) if settings.DEBUG else print("Server Error")
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return Response({"error": "Something went wrong please try again later"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@ -161,8 +167,9 @@ class BaseAPIView(TimezoneMixin, APIView, BasePaginator):
if isinstance(e, KeyError):
return Response({"error": f"key {e} does not exist"}, status=status.HTTP_400_BAD_REQUEST)
print(e) if settings.DEBUG else print("Server Error")
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return Response({"error": "Something went wrong please try again later"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

View File

@ -2,6 +2,7 @@
import uuid
import requests
import os
import logging
# Django imports
from django.utils import timezone
@ -49,6 +50,8 @@ def validate_google_token(token, client_id):
}
return data
except Exception as e:
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
raise exceptions.AuthenticationFailed("Error with Google connection.")

View File

@ -1,6 +1,7 @@
# Python imports
import csv
import io
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
@ -420,6 +421,6 @@ def analytic_export_task(email, data, slug):
csv_buffer = generate_csv_from_rows(rows)
send_export_email(email, slug, csv_buffer)
except Exception as e:
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
@ -39,8 +42,7 @@ def email_verification(first_name, email, token, current_site):
msg.send()
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -4,6 +4,7 @@ import io
import json
import boto3
import zipfile
import logging
from urllib.parse import urlparse, urlunparse
# Django imports
@ -368,8 +369,7 @@ def issue_export_task(provider, workspace_id, project_ids, token_id, multiple, s
exporter_instance.status = "failed"
exporter_instance.reason = str(e)
exporter_instance.save(update_fields=["status", "reason"])
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
@ -37,8 +40,7 @@ def forgot_password(first_name, email, uidb64, token, current_site):
msg.send()
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -3,6 +3,7 @@ import json
import requests
import uuid
import jwt
import logging
from datetime import datetime
# Django imports
@ -190,8 +191,7 @@ def service_importer(service, importer_id):
importer = Importer.objects.get(pk=importer_id)
importer.status = "failed"
importer.save()
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,6 +1,7 @@
# Python imports
import json
import requests
import logging
# Django imports
from django.conf import settings
@ -1538,8 +1539,7 @@ def issue_activity(
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,6 +1,7 @@
# Python imports
import json
from datetime import timedelta
import logging
# Django imports
from django.utils import timezone
@ -86,8 +87,8 @@ def archive_old_issues():
]
return
except Exception as e:
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return
@ -158,7 +159,7 @@ def close_old_issues():
]
return
except Exception as e:
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
@ -30,8 +33,7 @@ def magic_link(email, key, token, current_site):
msg.send()
return
except Exception as e:
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
return

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
@ -50,8 +53,7 @@ def project_invitation(email, project_id, token, current_site):
except (Project.DoesNotExist, ProjectMemberInvite.DoesNotExist) as e:
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.conf import settings
@ -29,8 +32,7 @@ def send_welcome_slack(user_id, created, message):
print(f"Got an error: {e.response['error']}")
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -1,3 +1,6 @@
# Python imports
import logging
# Django imports
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
@ -66,8 +69,7 @@ def workspace_invitation(email, workspace_id, token, current_site, invitor):
except (Workspace.DoesNotExist, WorkspaceMemberInvite.DoesNotExist) as e:
return
except Exception as e:
# Print logs if in DEBUG mode
if settings.DEBUG:
print(e)
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -3,6 +3,7 @@ import uuid
import string
import random
import pytz
import logging
# Django imports
from django.db import models
@ -139,5 +140,7 @@ def send_welcome_slack(sender, instance, created, **kwargs):
print(f"Got an error: {e.response['error']}")
return
except Exception as e:
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return

View File

@ -70,9 +70,7 @@ AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "secret-key")
# The name of the bucket to store files in.
AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_S3_BUCKET_NAME", "uploads")
# The full URL to the S3 endpoint. Leave blank to use the default region URL.
AWS_S3_ENDPOINT_URL = os.environ.get(
"AWS_S3_ENDPOINT_URL", "http://plane-minio:9000"
)
AWS_S3_ENDPOINT_URL = os.environ.get("AWS_S3_ENDPOINT_URL", "http://plane-minio:9000")
# Default permissions
AWS_DEFAULT_ACL = "public-read"
AWS_QUERYSTRING_AUTH = False
@ -98,7 +96,7 @@ CSRF_COOKIE_SECURE = True
# Redis URL
REDIS_URL = os.environ.get("REDIS_URL")
# Caches
# Caches
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
@ -137,31 +135,29 @@ LOGGING = {
"style": "{",
},
"json": {
'()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'fmt': '%(levelname)s %(asctime)s %(name)s %(message)s'
"()": "pythonjsonlogger.jsonlogger.JsonFormatter",
"fmt": "%(levelname)s %(asctime)s %(module)s %(name)s %(message)s",
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
},
"file": {
"level": "DEBUG",
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': "debug.log",
'when': 'midnight',
'interval': 1, # One day
'backupCount': 5, # Keep last 5 days of logs,
"formatter": "json"
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "debug.log",
"when": "midnight",
"interval": 1, # One day
"backupCount": 5, # Keep last 5 days of logs,
"formatter": "json",
},
},
"loggers": {
"plane": {
"level": "ERROR",
"handlers": ["console", "file"],
"propagate": True,
"propagate": False,
},
},
}

View File

@ -1,5 +1,9 @@
# Python imports
import requests
from requests.auth import HTTPBasicAuth
import logging
# Third party imports
from sentry_sdk import capture_exception
@ -49,5 +53,7 @@ def jira_project_issue_summary(email, api_token, project_key, hostname):
),
}
except Exception as e:
logger = logging.getLogger("plane")
logger.error(e)
capture_exception(e)
return {"error": "Something went wrong could not fetch information from jira"}