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

Refactor to use structlog #8747

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Main JSON interface views."""

import json
import logging
import sys
from pathlib import Path

Expand All @@ -10,6 +9,7 @@
from django.http import JsonResponse
from django.utils.translation import gettext_lazy as _

import structlog
from django_q.models import OrmQ
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import permissions, serializers
Expand All @@ -31,7 +31,7 @@
from .status import check_system_health, is_worker_running
from .version import inventreeApiText

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class LicenseViewSerializer(serializers.Serializer):
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""AppConfig for InvenTree app."""

import logging
from importlib import import_module
from pathlib import Path

Expand All @@ -11,6 +10,7 @@
from django.db import transaction
from django.db.utils import IntegrityError, OperationalError

import structlog
from allauth.socialaccount.signals import social_account_updated

import InvenTree.conversion
Expand All @@ -19,7 +19,7 @@
from common.settings import get_global_setting, set_global_setting
from InvenTree.config import get_setting

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class InvenTreeConfig(AppConfig):
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/auth_overrides.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Overrides for allauth and adjacent packages to enforce InvenTree specific auth settings and restirctions."""

import logging
from urllib.parse import urlencode

from django import forms
Expand All @@ -10,6 +9,7 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

import structlog
from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.forms import LoginForm, SignupForm, set_form_field_order
from allauth.core.exceptions import ImmediateHttpResponse
Expand All @@ -27,7 +27,7 @@
from common.settings import get_global_setting
from InvenTree.exceptions import log_error

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


# override allauth
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/backends.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Custom backend implementations."""

import datetime
import logging
import time

from django.db.utils import IntegrityError, OperationalError, ProgrammingError

import structlog
from maintenance_mode.backends import AbstractStateBackend

import common.models

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class InvenTreeMaintenanceModeBackend(AbstractStateBackend):
Expand Down
5 changes: 3 additions & 2 deletions src/backend/InvenTree/InvenTree/cache.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Configuration options for InvenTree external cache."""

import logging
import socket

import structlog

import InvenTree.config
import InvenTree.ready

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def cache_setting(name, default=None, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions src/backend/InvenTree/InvenTree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import datetime
import json
import logging
import os
import random
import shutil
Expand All @@ -13,7 +12,9 @@
from django.core.files.base import ContentFile
from django.core.files.storage import Storage

logger = logging.getLogger('inventree')
import structlog

logger = structlog.get_logger('inventree')
CONFIG_DATA = None
CONFIG_LOOKUPS = {}

Expand Down
5 changes: 3 additions & 2 deletions src/backend/InvenTree/InvenTree/conversion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Helper functions for converting between units."""

import logging
import re
from typing import Optional

Expand All @@ -11,7 +10,9 @@

_unit_registry = None

logger = logging.getLogger('inventree')
import structlog

logger = structlog.get_logger('inventree')


def get_unit_registry():
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# -*- coding: utf-8 -*-

import logging
import sys
import traceback

Expand All @@ -11,11 +10,12 @@
from django.utils.translation import gettext_lazy as _

import rest_framework.views as drfviews
import structlog
from rest_framework import serializers
from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.response import Response

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def log_error(path, error_name=None, error_info=None, error_data=None):
Expand Down
5 changes: 2 additions & 3 deletions src/backend/InvenTree/InvenTree/exchange.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Custom exchange backend which hooks into the InvenTree plugin system to fetch exchange rates from an external API."""

import logging

from django.db.transaction import atomic

import structlog
from djmoney.contrib.exchange.backends.base import SimpleExchangeBackend
from djmoney.contrib.exchange.models import ExchangeBackend, Rate

from common.currency import currency_code_default, currency_codes
from common.settings import get_global_setting

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class InvenTreeExchange(SimpleExchangeBackend):
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import inspect
import io
import logging
import os
import os.path
import re
Expand All @@ -22,6 +21,7 @@
from django.utils.translation import gettext_lazy as _

import bleach
import structlog
from bleach import clean
from djmoney.money import Money
from PIL import Image
Expand All @@ -31,7 +31,7 @@

from .settings import MEDIA_URL, STATIC_URL

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def extract_int(reference, clip=0x7FFFFFFF, allow_negative=False):
Expand Down
6 changes: 3 additions & 3 deletions src/backend/InvenTree/InvenTree/helpers_email.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Code for managing email functionality in InvenTree."""

import logging

from django.conf import settings
from django.core import mail as django_mail

import structlog

import InvenTree.ready
import InvenTree.tasks

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def is_email_configured():
Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/helpers_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Provides helper functions used throughout the InvenTree project that access the database."""

import io
import logging
from decimal import Decimal
from typing import Optional
from urllib.parse import urljoin
Expand All @@ -12,6 +11,7 @@
from django.utils.translation import gettext_lazy as _

import requests
import structlog
from djmoney.contrib.exchange.models import convert_money
from djmoney.money import Money
from PIL import Image
Expand All @@ -24,7 +24,7 @@
from common.settings import get_global_setting
from InvenTree.format import format_money

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def get_base_url(request=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Check if there are any pending database migrations, and run them."""

import logging

from django.core.management.base import BaseCommand

import structlog

from InvenTree.tasks import check_for_migrations

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class Command(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Custom management command to cleanup old settings that are not defined anymore."""

import logging

from django.core.management.base import BaseCommand

logger = logging.getLogger('inventree')
import structlog

logger = structlog.get_logger('inventree')


class Command(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
- This is crucial after importing any fixtures, etc
"""

import logging

from django.core.management.base import BaseCommand

import structlog
from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class Command(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
- May be required after importing a new dataset, for example
"""

import logging
import os

from django.core.management.base import BaseCommand
from django.db.utils import OperationalError, ProgrammingError

import structlog
from PIL import UnidentifiedImageError

from company.models import Company
from part.models import Part

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class Command(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Check if there are any pending database migrations, and run them."""

import logging

from django.core.management.base import BaseCommand

import structlog

from InvenTree.tasks import check_for_migrations

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class Command(BaseCommand):
Expand Down
9 changes: 5 additions & 4 deletions src/backend/InvenTree/InvenTree/metadata.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Custom metadata for DRF."""

import logging

from django.core.exceptions import PermissionDenied
from django.http import Http404

import structlog
from rest_framework import exceptions, serializers
from rest_framework.fields import empty
from rest_framework.metadata import SimpleMetadata
Expand All @@ -17,7 +16,7 @@
from InvenTree.helpers import str2bool
from InvenTree.serializers import DependentField

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


class InvenTreeMetadata(SimpleMetadata):
Expand Down Expand Up @@ -389,7 +388,9 @@ def get_field_info(self, field):
model = field.queryset.model
else:
logger.debug(
'Could not extract model for:', field_info.get('label'), '->', field
'Could not extract model for: %s -> %s',
field_info.get('label'),
field,
)
model = None

Expand Down
4 changes: 2 additions & 2 deletions src/backend/InvenTree/InvenTree/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Middleware for InvenTree."""

import logging
import sys

from django.conf import settings
Expand All @@ -9,14 +8,15 @@
from django.shortcuts import redirect
from django.urls import Resolver404, include, path, resolve, reverse_lazy

import structlog
from allauth_2fa.middleware import AllauthTwoFactorMiddleware, BaseRequire2FAMiddleware
from error_report.middleware import ExceptionProcessor

from common.settings import get_global_setting
from InvenTree.urls import frontendpatterns
from users.models import ApiToken

logger = logging.getLogger('inventree')
logger = structlog.get_logger('inventree')


def get_token_from_request(request):
Expand Down
Loading
Loading