Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing all the print calls by calls to log #272

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions catalog/management/commands/download_program_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
from rich import print
from rich.progress import MofNCompleteColumn, Progress, SpinnerColumn

from www.logger_settings import logger


class Command(BaseCommand):
help = ""

def handle(self, *args, **options):
with open("programs.json") as f:
programs: list[dict] = json.load(f)
print("\n[bold blue]Listing the course content of all programs...[/]\n")
logger.debug("\n[bold blue]Listing the course content of all programs...[/]\n")

failed: list = []
program_content: dict[str, dict[str, dict]] = {}
Expand Down Expand Up @@ -46,19 +48,21 @@ def handle(self, *args, **options):
response = requests.get(URL)
if not response.ok:
if "parent" in progam:
print(
logger.debug(
f"[yellow]Skip:[/] [magenta]{progam['slug'].upper()}[/] with bogus parent {progam['parent'].upper()}"
)
else:
print(
logger.debug(
f"[red]Error:[/] [magenta]{progam['slug'].upper()}[/] failed with {response.status_code}"
)
print(" ", URL)
logger.debug(" ", URL)
continue

except Exception as e:
print(f"[red]Error:[/] Failed to GET {progam['slug'].upper()}")
print(" URL", URL)
logger.debug(
f"[red]Error:[/] Failed to GET {progam['slug'].upper()}"
)
logger.debug(" URL", URL)
progress.console.print_exception()
continue

Expand All @@ -81,7 +85,7 @@ def handle(self, *args, **options):
}
except Exception as e:
failed.append(progam["slug"])
print(f"Error while listing content of {progam['slug']}")
logger.debug(f"Error while listing content of {progam['slug']}")
progress.console.print_exception()

with open(
Expand Down
10 changes: 6 additions & 4 deletions catalog/management/commands/download_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from rich import print
from rich.progress import MofNCompleteColumn, Progress, SpinnerColumn

from www.logger_settings import logger


class Command(BaseCommand):
help = ""
Expand All @@ -20,7 +22,7 @@ def handle(self, *args, **options):
programs = []

parent_programs: set[str] = set()
print("[bold blue]Gathering the list of available programs...[/]\n")
logger.debug("[bold blue]Gathering the list of available programs...[/]\n")

with Progress(
SpinnerColumn(),
Expand Down Expand Up @@ -107,12 +109,12 @@ def handle(self, *args, **options):
progress.update(task1, completed=self.PAGE_SIZE * page)
page += 1

print(
logger.debug(
f"Found {len(parent_programs)} programs containing options, ignoring those..."
)
print(parent_programs)
logger.debug(parent_programs)
programs = [p for p in programs if p["slug"] not in parent_programs]

print(f"Found {len(programs)} distinct programs, dumping to json...")
logger.debug(f"Found {len(programs)} distinct programs, dumping to json...")
with open("programs.json", "w") as f:
json.dump(programs, f, indent=4)
9 changes: 5 additions & 4 deletions catalog/management/parser/load_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
from django.utils.text import slugify

from catalog.models import Category, Course
from www.logger_settings import logger

with open("catalog/management/parser/data/tree.json") as tree_file:
tree = json.load(tree_file)

a = 0
ulb, _created = Category.objects.get_or_create(name="ULB", slug="root")
for fac_name, fac_info in tree["ULB"].items():
print(fac_name)
logger.debug(fac_name)
fac_obj, _created = Category.objects.get_or_create(
name=fac_name, slug=slugify(fac_name), description=fac_info["color"]
)
fac_obj.parents.add(ulb)

for program_slug, program_info in fac_info["programs"].items():
print(" ", program_info["name"])
logger.debug(" ", program_info["name"])
program_obj, _created = Category.objects.get_or_create(
name=program_info["name"], slug=program_slug
)
program_obj.parents.add(fac_obj)

for bloc_name, bloc_info in program_info["blocs"].items():
print(" ", bloc_name)
logger.debug(" ", bloc_name)
bloc_obj, _created = Category.objects.get_or_create(
name=f"Bloc {bloc_name}",
slug=f"{program_slug}-bloc-{bloc_name}",
Expand All @@ -34,7 +35,7 @@

for course_mnemo, course_info in bloc_info["courses"].items():
a += 1
print(str(a).zfill(4), course_info["title"])
logger.debug(str(a).zfill(4), course_info["title"])
course_obj, _created = Course.objects.get_or_create(slug=course_mnemo)
if _created:
course_obj.name = course_info["title"]
Expand Down
3 changes: 2 additions & 1 deletion documents/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from catalog.models import Course
from tags.models import Tag
from users.models import User
from www.logger_settings import logger

try:
import magic
except ImportError:
import platform

if platform.machine() == "arm64" and platform.system() == "Darwin":
print(
logger.debug(
"There is a bug with libmagic on Apple Silicon, disabling the feature for now"
)
else:
Expand Down
3 changes: 2 additions & 1 deletion documents/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from PyPDF2 import PdfReader

from documents.models import Document, DocumentError
from www.logger_settings import logger

from .exceptions import (
DocumentProcessingError,
Expand All @@ -30,7 +31,7 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
return None

doc_id = args[0]
print(f"Document {doc_id} failed.")
logger.debug(f"Document {doc_id} failed.")

document = Document.objects.get(id=doc_id)
document.state = Document.DocumentState.ERROR
Expand Down
14 changes: 14 additions & 0 deletions www/logger_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
import sys

logger = logging.getLogger()

# specify here to change log level
log_level = logging.DEBUG
logger.setLevel(log_level)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
12 changes: 8 additions & 4 deletions www/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# https://django-environ.readthedocs.io/en/latest/tips.html#docker-style-file-based-variables
from sentry_sdk.utils import get_default_release

from www.logger_settings import logger

env = environ.FileAwareEnv()

# Set the project base directory
Expand Down Expand Up @@ -158,8 +160,10 @@
AWS_S3_SECRET_ACCESS_KEY = env("STORAGE_SECRET_KEY")
AWS_STORAGE_BUCKET_NAME = env("STORAGE_MEDIA_BUCKET_NAME")
elif not DEBUG:
print("Warning: no storage configured but DEBUG=False, using local filesystem.")
print("You DO NOT want this in production!")
logger.debug(
"Warning: no storage configured but DEBUG=False, using local filesystem."
)
logger.debug("You DO NOT want this in production!")

if DEBUG:
INSTALLED_APPS.extend(["django_extensions"])
Expand Down Expand Up @@ -209,7 +213,7 @@


if DEBUG:
print(
logger.debug(
"Warning: you are running Dochub with DEBUG=True. This is dangerous if your server is publicly accessible."
)
print("You should set DEBUG=False in production.\n\n")
logger.debug("You should set DEBUG=False in production.\n\n")