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 django.urls import reverse_lazy
|
||||
from django.conf import settings
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
@ -41,20 +42,20 @@ class ApiMixing(View):
|
|||
# Authentication
|
||||
auth_header = self.request.headers.get('Authorization')
|
||||
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)
|
||||
|
||||
token = auth_header.split(' ')[1].strip("'").strip('"')
|
||||
try:
|
||||
uuid.UUID(token)
|
||||
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)
|
||||
|
||||
self.tk = Token.objects.filter(token=token).first()
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
@ -72,7 +73,8 @@ class NewSnapshotView(ApiMixing):
|
|||
try:
|
||||
data = json.loads(request.body)
|
||||
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)
|
||||
|
||||
# Process snapshot
|
||||
|
@ -85,7 +87,7 @@ class NewSnapshotView(ApiMixing):
|
|||
|
||||
if not data.get("uuid"):
|
||||
txt = "error: the snapshot not have uuid"
|
||||
logger.exception(txt)
|
||||
logger.error("%s", txt)
|
||||
return JsonResponse({'status': txt}, status=500)
|
||||
|
||||
exist_annotation = Annotation.objects.filter(
|
||||
|
@ -94,15 +96,20 @@ class NewSnapshotView(ApiMixing):
|
|||
|
||||
if exist_annotation:
|
||||
txt = "error: the snapshot {} exist".format(data['uuid'])
|
||||
logger.exception(txt)
|
||||
logger.warning("%s", txt)
|
||||
return JsonResponse({'status': txt}, status=500)
|
||||
|
||||
|
||||
try:
|
||||
Build(data, self.tk.owner)
|
||||
except Exception as err:
|
||||
logger.exception(err)
|
||||
return JsonResponse({'status': f"fail: {err}"}, status=500)
|
||||
if settings.DEBUG:
|
||||
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(
|
||||
uuid=data['uuid'],
|
||||
|
@ -114,7 +121,7 @@ class NewSnapshotView(ApiMixing):
|
|||
|
||||
|
||||
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)
|
||||
|
||||
url_args = reverse_lazy("device:details", args=(annotation.value,))
|
||||
|
@ -286,7 +293,7 @@ class AddAnnotationView(ApiMixing):
|
|||
key = data["key"]
|
||||
value = data["value"]
|
||||
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)
|
||||
|
||||
Annotation.objects.create(
|
||||
|
|
|
@ -17,6 +17,8 @@ from pathlib import Path
|
|||
from django.contrib.messages import constants as messages
|
||||
from decouple import config, Csv
|
||||
|
||||
from utils.logger import CustomFormatter
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
@ -205,8 +207,18 @@ LOGOUT_REDIRECT_URL = '/'
|
|||
LOGGING = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
'formatters': {
|
||||
'colored': {
|
||||
'()': CustomFormatter,
|
||||
'format': '%(levelname)s %(asctime)s %(message)s'
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"console": {"level": "DEBUG", "class": "logging.StreamHandler"},
|
||||
"console": {
|
||||
"level": "DEBUG",
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "colored"
|
||||
},
|
||||
},
|
||||
"root": {
|
||||
"handlers": ["console"],
|
||||
|
|
|
@ -36,10 +36,8 @@ class Command(BaseCommand):
|
|||
continue
|
||||
user = institution.user_set.filter(is_admin=True).first()
|
||||
if not user:
|
||||
txt = "Error No there are Admins for the institution: {}".format(
|
||||
institution.name
|
||||
)
|
||||
logger.exception(txt)
|
||||
txt = "No there are Admins for the institution: %s"
|
||||
logger.warning(txt, institution.name)
|
||||
continue
|
||||
|
||||
snapshots_path = os.path.join(filepath, "snapshots")
|
||||
|
@ -74,13 +72,12 @@ class Command(BaseCommand):
|
|||
create_index(s, user)
|
||||
create_annotation(s, user, commit=True)
|
||||
except Exception as err:
|
||||
txt = "Error: in placeholder {} \n{}".format(f_path, err)
|
||||
logger.exception(txt)
|
||||
txt = "In placeholder %s \n%s"
|
||||
logger.warning(txt, f_path, err)
|
||||
|
||||
def build_snapshot(self, s, user, f_path):
|
||||
try:
|
||||
Build(s, user)
|
||||
except Exception as err:
|
||||
txt = "Error: in Snapshot {} \n{}".format(f_path, err)
|
||||
logger.exception(txt)
|
||||
|
||||
except Exception:
|
||||
txt = "Error: in Snapshot {}".format(f_path)
|
||||
logger.error(txt)
|
||||
|
|
|
@ -4,6 +4,9 @@ import logging
|
|||
|
||||
from django.core.management.base import BaseCommand
|
||||
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
|
||||
|
||||
|
||||
|
@ -36,7 +39,7 @@ class Command(BaseCommand):
|
|||
self.read_directory(path)
|
||||
|
||||
self.parsing()
|
||||
|
||||
|
||||
def read_directory(self, directory):
|
||||
for filename in os.listdir(directory):
|
||||
filepath = os.path.join(directory, filename)
|
||||
|
@ -45,13 +48,18 @@ class Command(BaseCommand):
|
|||
|
||||
def open(self, filepath):
|
||||
with open(filepath, 'r') as file:
|
||||
content = json.loads(file.read())
|
||||
self.snapshots.append(content)
|
||||
|
||||
content = json.loads(file.read())
|
||||
path_name = save_in_disk(data, self.user.institution.name)
|
||||
self.snapshots.append((content, path_name))
|
||||
|
||||
def parsing(self):
|
||||
for s in self.snapshots:
|
||||
for s, p in self.snapshots:
|
||||
try:
|
||||
self.devices.append(Build(s, self.user))
|
||||
move_json(p, self.user.institution.name)
|
||||
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:
|
||||
txt = "Warning: Snapshot {} exist as annotation !!".format(self.uuid)
|
||||
logger.exception(txt)
|
||||
txt = "Warning: Snapshot %s exist as annotation !!"
|
||||
logger.warning(txt, self.uuid)
|
||||
return
|
||||
|
||||
for k, v in self.algorithms.items():
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import logging
|
||||
import numpy as np
|
||||
|
||||
from datetime import datetime
|
||||
|
@ -8,6 +9,9 @@ from json_repair import repair_json
|
|||
from utils.constants import CHASSIS_DH, DATASTORAGEINTERFACE
|
||||
|
||||
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
|
||||
def get_lshw_child(child, nets, component):
|
||||
if child.get('id') == component:
|
||||
nets.append(child)
|
||||
|
@ -483,12 +487,12 @@ class ParseSnapshot:
|
|||
if isinstance(x, str):
|
||||
try:
|
||||
try:
|
||||
hw = json.loads(lshw)
|
||||
hw = json.loads(x)
|
||||
except json.decoder.JSONDecodeError:
|
||||
hw = json.loads(repair_json(lshw))
|
||||
hw = json.loads(repair_json(x))
|
||||
return hw
|
||||
except Exception as ss:
|
||||
print("WARNING!! {}".format(ss))
|
||||
logger.warning("%s", ss)
|
||||
return {}
|
||||
return x
|
||||
|
||||
|
@ -497,5 +501,5 @@ class ParseSnapshot:
|
|||
return self._errors
|
||||
|
||||
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:
|
||||
txt = "Warning: Snapshot {} exist as annotation !!".format(doc["uuid"])
|
||||
logger.exception(txt)
|
||||
txt = "Snapshot %s exist as annotation !!"
|
||||
logger.warning(txt, doc["uuid"])
|
||||
return annotation
|
||||
|
||||
return Annotation.objects.create(**data)
|
||||
|
|
31
utils/logger.py
Normal file
31
utils/logger.py
Normal file
|
@ -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 a new issue