extract logs with colors and depending of DEBUG var
This commit is contained in:
parent
ac1786c115
commit
e74ddc47a7
27
api/views.py
27
api/views.py
|
@ -5,6 +5,7 @@ import logging
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
|
from django.conf import settings
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
from django.shortcuts import get_object_or_404, redirect
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
@ -41,20 +42,20 @@ class ApiMixing(View):
|
||||||
# Authentication
|
# Authentication
|
||||||
auth_header = self.request.headers.get('Authorization')
|
auth_header = self.request.headers.get('Authorization')
|
||||||
if not auth_header or not auth_header.startswith('Bearer '):
|
if not auth_header or not auth_header.startswith('Bearer '):
|
||||||
logger.exception("Invalid or missing token {}".format(auth_header))
|
logger.error("Invalid or missing token %s", auth_header)
|
||||||
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
||||||
|
|
||||||
token = auth_header.split(' ')[1].strip("'").strip('"')
|
token = auth_header.split(' ')[1].strip("'").strip('"')
|
||||||
try:
|
try:
|
||||||
uuid.UUID(token)
|
uuid.UUID(token)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Invalid token {}".format(token))
|
logger.error("Invalid or missing token %s", token)
|
||||||
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
||||||
|
|
||||||
self.tk = Token.objects.filter(token=token).first()
|
self.tk = Token.objects.filter(token=token).first()
|
||||||
|
|
||||||
if not self.tk:
|
if not self.tk:
|
||||||
logger.exception("Invalid or missing token {}".format(token))
|
logger.error("Invalid or missing token %s", token)
|
||||||
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,7 +73,8 @@ class NewSnapshotView(ApiMixing):
|
||||||
try:
|
try:
|
||||||
data = json.loads(request.body)
|
data = json.loads(request.body)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
logger.exception("Invalid Snapshot of user {}".format(self.tk.owner))
|
txt = "error: the snapshot is not a json"
|
||||||
|
logger.error("%s", txt)
|
||||||
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
||||||
|
|
||||||
# Process snapshot
|
# Process snapshot
|
||||||
|
@ -85,7 +87,7 @@ class NewSnapshotView(ApiMixing):
|
||||||
|
|
||||||
if not data.get("uuid"):
|
if not data.get("uuid"):
|
||||||
txt = "error: the snapshot not have uuid"
|
txt = "error: the snapshot not have uuid"
|
||||||
logger.exception(txt)
|
logger.error("%s", txt)
|
||||||
return JsonResponse({'status': txt}, status=500)
|
return JsonResponse({'status': txt}, status=500)
|
||||||
|
|
||||||
exist_annotation = Annotation.objects.filter(
|
exist_annotation = Annotation.objects.filter(
|
||||||
|
@ -94,15 +96,20 @@ class NewSnapshotView(ApiMixing):
|
||||||
|
|
||||||
if exist_annotation:
|
if exist_annotation:
|
||||||
txt = "error: the snapshot {} exist".format(data['uuid'])
|
txt = "error: the snapshot {} exist".format(data['uuid'])
|
||||||
logger.exception(txt)
|
logger.warning("%s", txt)
|
||||||
return JsonResponse({'status': txt}, status=500)
|
return JsonResponse({'status': txt}, status=500)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
Build(data, self.tk.owner)
|
Build(data, self.tk.owner)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.exception(err)
|
if settings.DEBUG:
|
||||||
return JsonResponse({'status': f"fail: {err}"}, status=500)
|
logger.exception("%s", err)
|
||||||
|
snapshot_id = data.get("uuid", "")
|
||||||
|
txt = "It is not possible to parse snapshot: %s."
|
||||||
|
logger.error(txt, snapshot_id)
|
||||||
|
text = "fail: It is not possible to parse snapshot"
|
||||||
|
return JsonResponse({'status': text}, status=500)
|
||||||
|
|
||||||
annotation = Annotation.objects.filter(
|
annotation = Annotation.objects.filter(
|
||||||
uuid=data['uuid'],
|
uuid=data['uuid'],
|
||||||
|
@ -114,7 +121,7 @@ class NewSnapshotView(ApiMixing):
|
||||||
|
|
||||||
|
|
||||||
if not annotation:
|
if not annotation:
|
||||||
logger.exception("Error: No annotation for uuid: {}".format(data["uuid"]))
|
logger.error("Error: No annotation for uuid: %s", data["uuid"])
|
||||||
return JsonResponse({'status': 'fail'}, status=500)
|
return JsonResponse({'status': 'fail'}, status=500)
|
||||||
|
|
||||||
url_args = reverse_lazy("device:details", args=(annotation.value,))
|
url_args = reverse_lazy("device:details", args=(annotation.value,))
|
||||||
|
@ -286,7 +293,7 @@ class AddAnnotationView(ApiMixing):
|
||||||
key = data["key"]
|
key = data["key"]
|
||||||
value = data["value"]
|
value = data["value"]
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Invalid Snapshot of user {}".format(self.tk.owner))
|
logger.error("Invalid Snapshot of user %s", self.tk.owner)
|
||||||
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
||||||
|
|
||||||
Annotation.objects.create(
|
Annotation.objects.create(
|
||||||
|
|
|
@ -17,6 +17,8 @@ from pathlib import Path
|
||||||
from django.contrib.messages import constants as messages
|
from django.contrib.messages import constants as messages
|
||||||
from decouple import config, Csv
|
from decouple import config, Csv
|
||||||
|
|
||||||
|
from utils.logger import CustomFormatter
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
@ -205,8 +207,18 @@ LOGOUT_REDIRECT_URL = '/'
|
||||||
LOGGING = {
|
LOGGING = {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"disable_existing_loggers": False,
|
"disable_existing_loggers": False,
|
||||||
|
'formatters': {
|
||||||
|
'colored': {
|
||||||
|
'()': CustomFormatter,
|
||||||
|
'format': '%(levelname)s %(asctime)s %(message)s'
|
||||||
|
},
|
||||||
|
},
|
||||||
"handlers": {
|
"handlers": {
|
||||||
"console": {"level": "DEBUG", "class": "logging.StreamHandler"},
|
"console": {
|
||||||
|
"level": "DEBUG",
|
||||||
|
"class": "logging.StreamHandler",
|
||||||
|
"formatter": "colored"
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"handlers": ["console"],
|
"handlers": ["console"],
|
||||||
|
|
|
@ -36,10 +36,8 @@ class Command(BaseCommand):
|
||||||
continue
|
continue
|
||||||
user = institution.user_set.filter(is_admin=True).first()
|
user = institution.user_set.filter(is_admin=True).first()
|
||||||
if not user:
|
if not user:
|
||||||
txt = "Error No there are Admins for the institution: {}".format(
|
txt = "No there are Admins for the institution: %s"
|
||||||
institution.name
|
logger.warning(txt, institution.name)
|
||||||
)
|
|
||||||
logger.exception(txt)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
snapshots_path = os.path.join(filepath, "snapshots")
|
snapshots_path = os.path.join(filepath, "snapshots")
|
||||||
|
@ -74,13 +72,12 @@ class Command(BaseCommand):
|
||||||
create_index(s, user)
|
create_index(s, user)
|
||||||
create_annotation(s, user, commit=True)
|
create_annotation(s, user, commit=True)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
txt = "Error: in placeholder {} \n{}".format(f_path, err)
|
txt = "In placeholder %s \n%s"
|
||||||
logger.exception(txt)
|
logger.warning(txt, f_path, err)
|
||||||
|
|
||||||
def build_snapshot(self, s, user, f_path):
|
def build_snapshot(self, s, user, f_path):
|
||||||
try:
|
try:
|
||||||
Build(s, user)
|
Build(s, user)
|
||||||
except Exception as err:
|
except Exception:
|
||||||
txt = "Error: in Snapshot {} \n{}".format(f_path, err)
|
txt = "Error: in Snapshot {}".format(f_path)
|
||||||
logger.exception(txt)
|
logger.error(txt)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,9 @@ import logging
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
from utils.save_snapshots import move_json, save_in_disk
|
||||||
from evidence.parse import Build
|
from evidence.parse import Build
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,12 +49,17 @@ class Command(BaseCommand):
|
||||||
def open(self, filepath):
|
def open(self, filepath):
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
content = json.loads(file.read())
|
content = json.loads(file.read())
|
||||||
self.snapshots.append(content)
|
path_name = save_in_disk(data, self.user.institution.name)
|
||||||
|
self.snapshots.append((content, path_name))
|
||||||
|
|
||||||
def parsing(self):
|
def parsing(self):
|
||||||
for s in self.snapshots:
|
for s, p in self.snapshots:
|
||||||
try:
|
try:
|
||||||
self.devices.append(Build(s, self.user))
|
self.devices.append(Build(s, self.user))
|
||||||
|
move_json(p, self.user.institution.name)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.exception(err)
|
if settings.DEBUG:
|
||||||
|
logger.exception("%s", err)
|
||||||
|
snapshot_id = s.get("uuid", "")
|
||||||
|
txt = "It is not possible to parse snapshot: %s."
|
||||||
|
logger.error(txt, snapshot_id)
|
||||||
|
|
|
@ -90,8 +90,8 @@ class Build:
|
||||||
)
|
)
|
||||||
|
|
||||||
if annotation:
|
if annotation:
|
||||||
txt = "Warning: Snapshot {} exist as annotation !!".format(self.uuid)
|
txt = "Warning: Snapshot %s exist as annotation !!"
|
||||||
logger.exception(txt)
|
logger.warning(txt, self.uuid)
|
||||||
return
|
return
|
||||||
|
|
||||||
for k, v in self.algorithms.items():
|
for k, v in self.algorithms.items():
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -8,6 +9,9 @@ from json_repair import repair_json
|
||||||
from utils.constants import CHASSIS_DH, DATASTORAGEINTERFACE
|
from utils.constants import CHASSIS_DH, DATASTORAGEINTERFACE
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('django')
|
||||||
|
|
||||||
|
|
||||||
def get_lshw_child(child, nets, component):
|
def get_lshw_child(child, nets, component):
|
||||||
if child.get('id') == component:
|
if child.get('id') == component:
|
||||||
nets.append(child)
|
nets.append(child)
|
||||||
|
@ -483,12 +487,12 @@ class ParseSnapshot:
|
||||||
if isinstance(x, str):
|
if isinstance(x, str):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
hw = json.loads(lshw)
|
hw = json.loads(x)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
hw = json.loads(repair_json(lshw))
|
hw = json.loads(repair_json(x))
|
||||||
return hw
|
return hw
|
||||||
except Exception as ss:
|
except Exception as ss:
|
||||||
print("WARNING!! {}".format(ss))
|
logger.warning("%s", ss)
|
||||||
return {}
|
return {}
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
@ -497,5 +501,5 @@ class ParseSnapshot:
|
||||||
return self._errors
|
return self._errors
|
||||||
|
|
||||||
logger.error(txt)
|
logger.error(txt)
|
||||||
self._errors.append(txt)
|
self._errors.append("%s", txt)
|
||||||
|
|
||||||
|
|
|
@ -88,8 +88,8 @@ def create_annotation(doc, user, commit=False):
|
||||||
)
|
)
|
||||||
|
|
||||||
if annotation:
|
if annotation:
|
||||||
txt = "Warning: Snapshot {} exist as annotation !!".format(doc["uuid"])
|
txt = "Snapshot %s exist as annotation !!"
|
||||||
logger.exception(txt)
|
logger.warning(txt, doc["uuid"])
|
||||||
return annotation
|
return annotation
|
||||||
|
|
||||||
return Annotation.objects.create(**data)
|
return Annotation.objects.create(**data)
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
RED = "\033[91m"
|
||||||
|
PURPLE = "\033[95m"
|
||||||
|
YELLOW = "\033[93m"
|
||||||
|
RESET = "\033[0m"
|
||||||
|
|
||||||
|
|
||||||
|
class CustomFormatter(logging.Formatter):
|
||||||
|
def format(self, record):
|
||||||
|
if record.levelname == "ERROR":
|
||||||
|
color = RED
|
||||||
|
elif record.levelname == "WARNING":
|
||||||
|
color = PURPLE
|
||||||
|
elif record.levelname in ["INFO", "DEBUG"]:
|
||||||
|
color = YELLOW
|
||||||
|
else:
|
||||||
|
color = RESET
|
||||||
|
|
||||||
|
record.levelname = f"{color}{record.levelname}{RESET}"
|
||||||
|
|
||||||
|
if record.args:
|
||||||
|
record.msg = self.highlight_args(record.msg, record.args, color)
|
||||||
|
record.args = ()
|
||||||
|
|
||||||
|
return super().format(record)
|
||||||
|
|
||||||
|
def highlight_args(self, message, args, color):
|
||||||
|
highlighted_args = tuple(f"{color}{arg}{RESET}" for arg in args)
|
||||||
|
return message % highlighted_args
|
Loading…
Reference in New Issue