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

chore: formatage des logs en json avant envoi à datadog #927

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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: 13 additions & 5 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from dotenv import load_dotenv
from machina import MACHINA_MAIN_STATIC_DIR, MACHINA_MAIN_TEMPLATE_DIR

from lacommunaute.utils.loggers import CustomJsonFormatter


load_dotenv()

Expand Down Expand Up @@ -311,19 +313,25 @@
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"simple": {
"format": "{levelname} {asctime} {pathname} : {message}",
"style": "{",
},
"json": {"()": CustomJsonFormatter},
},
"handlers": {
"console": {"class": "logging.StreamHandler", "formatter": "simple"},
"console": {"class": "logging.StreamHandler", "formatter": "json"},
"null": {"class": "logging.NullHandler"},
},
"loggers": {
"django": {
"handlers": ["console"],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
},
"django.security.DisallowedHost": {
"handlers": ["null"],
"propagate": False,
},
"lacommunaute": {
"handlers": ["console"],
"level": os.getenv("LACOMMUNAUTE_LOG_LEVEL", "INFO"),
},
"commands": {
"handlers": ["console"],
"level": os.getenv("COMMANDS_LOG_LEVEL", "INFO"),
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/event/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime
from logging import getLogger

from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
Expand All @@ -14,7 +14,7 @@
from lacommunaute.event.models import Event


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")


class SuccessUrlMixin:
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger

from django.conf import settings
from django.contrib.auth.mixins import UserPassesTestMixin
Expand All @@ -24,7 +24,7 @@
)


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")

Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum_conversation/forum_polls/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging
from logging import getLogger

from django.template.response import TemplateResponse
from machina.apps.forum_conversation.forum_polls.views import TopicPollVoteView as BaseTopicPollVoteView
from machina.core.db.models import get_model
from machina.core.loading import get_class


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

Topic = get_model("forum_conversation", "Topic")
TopicPollVote = get_model("forum_polls", "TopicPollVote")
Expand Down
5 changes: 3 additions & 2 deletions lacommunaute/forum_conversation/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger

from django.conf import settings
from django.contrib import messages
Expand All @@ -15,7 +15,7 @@
from lacommunaute.notification.models import Notification


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

TrackingHandler = get_class("forum_tracking.handler", "TrackingHandler")
track_handler = TrackingHandler()
Expand All @@ -26,6 +26,7 @@ def form_valid(self, *args, **kwargs):
valid = super().form_valid(*args, **kwargs)

track_handler.mark_topic_read(self.forum_post.topic, self.request.user)
logger.info("form is valid", extra={"context": self})
return valid


Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum_conversation/views_htmx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger

from django.shortcuts import get_object_or_404, render
from django.views import View
Expand All @@ -10,7 +10,7 @@
from lacommunaute.notification.models import Notification


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
TrackingHandler = get_class("forum_tracking.handler", "TrackingHandler")
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum_member/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger

from django.urls import reverse
from django.views.generic import ListView
Expand All @@ -12,7 +12,7 @@
from lacommunaute.forum_member.models import ForumProfile


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")


PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
Expand Down
5 changes: 5 additions & 0 deletions lacommunaute/forum_moderation/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from logging import getLogger

from django.contrib import messages
from django.urls import reverse
from machina.apps.forum_moderation.views import (
Expand All @@ -9,6 +11,9 @@
from lacommunaute.forum_moderation.models import BlockedEmail, BlockedPost


logger = getLogger("lacommunaute")


class TopicDeleteView(BaseTopicDeleteView):
def get_success_url(self):
messages.success(self.request, self.success_message)
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum_upvote/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger

from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
Expand All @@ -13,7 +13,7 @@
from lacommunaute.forum_upvote.models import UpVote


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
TrackingHandler = get_class("forum_tracking.handler", "TrackingHandler")
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/notification/emails.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger
from urllib.parse import urljoin

import httpx
Expand All @@ -9,7 +9,7 @@
from lacommunaute.utils.enums import Environment


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")

SIB_SMTP_URL = urljoin(settings.SIB_URL, settings.SIB_SMTP_ROUTE)
SIB_CONTACTS_URL = urljoin(settings.SIB_URL, settings.SIB_CONTACTS_ROUTE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from logging import getLogger

from django.core.management.base import BaseCommand

from lacommunaute.notification.tasks import add_user_to_list_when_register


logger = getLogger("commands")


class Command(BaseCommand):
help = "Ajouter un utilisateur à une liste Sendinblue quand il s'inscrit"

def handle(self, *args, **options):
add_user_to_list_when_register()
self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("That's all, folks!")
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from logging import getLogger

from django.core.management.base import BaseCommand

from lacommunaute.notification.enums import NotificationDelay
from lacommunaute.notification.tasks import send_messages_notifications, send_missyou_notifications


logger = getLogger("commands")


class Command(BaseCommand):
help = "Envoyer les notifications en file d'attente avec le délai paramétré"

Expand All @@ -14,10 +19,8 @@ def handle(self, *args, **options):
try:
delay = NotificationDelay(options["delay"])
except ValueError:
self.stdout.write(
self.style.ERROR(f"le délai fournit doit être un valeuer de {str(NotificationDelay.values)}")
)
logger.error("le délai fournit doit être un valeuer de %s", str(NotificationDelay.values))

send_messages_notifications(delay)
send_missyou_notifications(delay)
self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("That's all, folks!")
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from logging import getLogger

from django.core.management.base import BaseCommand

from lacommunaute.notification.tasks import send_notifs_on_unanswered_topics


logger = getLogger("commands")


class Command(BaseCommand):
help = "Envoyer une notification par email aux utilisateurs volontaires quand il y a des sujets sans réponse"

def handle(self, *args, **options):
send_notifs_on_unanswered_topics()
self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("That's all, folks!")
4 changes: 2 additions & 2 deletions lacommunaute/openid_connect/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dataclasses
import logging
from logging import getLogger

import httpx
import jwt
Expand All @@ -14,7 +14,7 @@
from lacommunaute.openid_connect.models import OIDConnectUserData, OpenID_State


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")


@dataclasses.dataclass
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/pages/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger
from typing import Any

from django.contrib.auth.mixins import UserPassesTestMixin
Expand All @@ -12,7 +12,7 @@
from lacommunaute.forum_conversation.models import Topic


logger = logging.getLogger(__name__)
logger = getLogger("lacommunaute")


class LandingPagesListView(UserPassesTestMixin, TemplateView):
Expand Down
5 changes: 5 additions & 0 deletions lacommunaute/partner/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from logging import getLogger

from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse
from django.views.generic import CreateView, DetailView, ListView, UpdateView
Expand All @@ -8,6 +10,9 @@
from lacommunaute.utils.perms import forum_visibility_content_tree_from_forums


logger = getLogger("lacommunaute")


class PartnerListView(ListView):
model = Partner
template_name = "partner/list.html"
Expand Down
5 changes: 5 additions & 0 deletions lacommunaute/search/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from logging import getLogger

from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank
from django.db.models import F
from django.views.generic import ListView
Expand All @@ -9,6 +11,9 @@
from lacommunaute.search.models import CommonIndex


logger = getLogger("lacommunaute")


class SearchView(FormMixin, ListView):
model = CommonIndex
form_class = SearchForm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from logging import getLogger

from django.core.management.base import BaseCommand

from lacommunaute.surveys.stats import collect_dsp_stats


logger = getLogger("commands")


class Command(BaseCommand):
help = "Collecter les stats django, jusqu'à la veille de l'execution"

def handle(self, *args, **options):
from_date, count = collect_dsp_stats()
self.stdout.write(self.style.SUCCESS(f"Collecting DSP stats from {from_date} to yesterday: {count} new stats"))
self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("Collecting DSP stats from %s to yesterday: %s new stats", from_date, count)
logger.info("That's all, folks!")
9 changes: 6 additions & 3 deletions lacommunaute/stats/management/commands/collect_events.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# vincenporte ~ assumed quick'n'dirty solution
# TODO: refactor it code base and test it

import json
from datetime import datetime
from logging import getLogger

from dateutil.relativedelta import relativedelta
from django.core.management.base import BaseCommand
Expand All @@ -15,6 +15,9 @@
from lacommunaute.utils.matomo import get_matomo_data


logger = getLogger("commands")


def save_to_json(stats, period):
with open(f"exports/{period}ly_events.json", "w") as f:
json.dump(stats, f)
Expand Down Expand Up @@ -93,10 +96,10 @@ def handle(self, *args, **options):
("month", timezone.make_aware(datetime(2023, 1, 1), timezone.get_current_timezone())),
("week", timezone.make_aware(datetime(2023, 1, 2), timezone.get_current_timezone())),
):
self.stdout.write(f"Collecting {period}ly events from {search_date}")
logger.info("Collecting %sly events from %s", period, search_date)
stats = []
stats = collect_matomo_events(period, search_date, stats)
stats = collect_db_events(period, search_date, stats)
save_to_json(stats, period)

self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("That's all, folks!")
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from logging import getLogger

from dateutil.relativedelta import relativedelta
from django.core.management.base import BaseCommand
Expand All @@ -8,6 +9,9 @@
from lacommunaute.utils.matomo import collect_forum_stats_from_matomo_api


logger = getLogger("commands")


class Command(BaseCommand):
help = "Collecter les stats des forum dans matomo, jusqu'au dimanche précédent l'execution"

Expand All @@ -25,4 +29,4 @@ def handle(self, *args, **options):

collect_forum_stats_from_matomo_api(from_date=from_date, to_date=to_date, period=period)

self.stdout.write(self.style.SUCCESS("That's all, folks!"))
logger.info("That's all, folks!")
Loading
Loading