From d91a4c2b4d08aff2fe7c07e5e07b097ea4f05f8f Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 27 Oct 2016 17:19:33 +0200 Subject: [PATCH] Inserted logger --- logs/Readme.md | 24 ++++++++++++++++++++++++ logs/pyros.log | 13 +++++++++++++ logs/routine_manager-views.log | 0 pyrosrun.sh | 8 +++++++- src/logger/__init__.py | 0 src/logger/config.py | 25 +++++++++++++++++++++++++ src/routine_manager/views.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 logs/Readme.md create mode 100644 logs/pyros.log create mode 100644 logs/routine_manager-views.log create mode 100644 src/logger/__init__.py create mode 100644 src/logger/config.py diff --git a/logs/Readme.md b/logs/Readme.md new file mode 100644 index 0000000..b2e19d2 --- /dev/null +++ b/logs/Readme.md @@ -0,0 +1,24 @@ +File Logs are organized by module + +For example, logs for the module MONITORING must be in the file : + +> monitoring.logs + +The logs are formated like this : + +> '%(filename)s : %(lineno)s -> %(message)s' +> "filename : line -> message". + +To use the logger you must import logger.config + +> import logger.config as l +> log = l.setupLogger("name", "file_name") + +Basic log : + +> log.info('Your message') + +if you want to log in the file pyros.log you must use logging + +> import logger.config as l +> l.logging.info('Your message') diff --git a/logs/pyros.log b/logs/pyros.log new file mode 100644 index 0000000..509493a --- /dev/null +++ b/logs/pyros.log @@ -0,0 +1,13 @@ +config- +config -> Logger instantiated +config -> Logger instantiated +config -> Logger instantiated +config -> Logger instantiated +config -> Logger instantiated +config -> Logger instantiated +config -> Logger instantiated +2016-10-27 15:17:43,031 : config -> Logger instantiated +2016-10-27 15:17:52,770 : config -> Logger instantiated +2016-10-27 15:18:03,562 : config -> Logger instantiated +2016-10-27 15:18:18,357 : config -> Logger instantiated +2016-10-27 15:18:25,094 : config -> Logger instantiated diff --git a/logs/routine_manager-views.log b/logs/routine_manager-views.log new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/logs/routine_manager-views.log diff --git a/pyrosrun.sh b/pyrosrun.sh index 1216fa0..a206574 100755 --- a/pyrosrun.sh +++ b/pyrosrun.sh @@ -10,7 +10,8 @@ COMMANDS="\n \t'simul_on' : Starts the simulators\n \t'simul_off' : Stops the simulators\n \t'start' : Starts the simulators then the celery workers, then the web server\n -\t'stop' : Stops the celery workers then the simulators" +\t'stop' : Stops the celery workers then the simulators\n +\t'clear_logs' : Clear all pyros .log files from /logs" NEEDED_COMMAND="One command is needed. Possible commands : $COMMANDS" INVALID_COMMAND="Invalid command. Possible commands : $COMMANDS" @@ -74,6 +75,11 @@ case "$1" in ./kill_all.sh cd - ;; + "clear_logs") + cd ../logs + rm -f *.log + cd - + ;; *) echo -e $INVALID_COMMAND ;; diff --git a/src/logger/__init__.py b/src/logger/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/logger/__init__.py diff --git a/src/logger/config.py b/src/logger/config.py new file mode 100644 index 0000000..411dd9c --- /dev/null +++ b/src/logger/config.py @@ -0,0 +1,25 @@ +from django.conf import settings +import logging +import sys + +# maybe reset files with a variable from settings for the logging +# if (settings.RESETLOGS): + + +logging.basicConfig(filename='%s/../logs/pyros.log'%(settings.BASE_DIR), format='%(asctime)s : %(module)s -> %(message)s', level=logging.DEBUG) + +if (settings.DEBUG): + logging.info('Logger instantiated') + +def setupLogger(logger_name, log_file, level=logging.INFO): + l = logging.getLogger(logger_name) + formatter = logging.Formatter('%(filename)s : %(lineno)s -> %(message)s') + fileHandler = logging.FileHandler('%s/../logs/%s.log'%(settings.BASE_DIR, log_file), mode='w') + fileHandler.setFormatter(formatter) + # streamHandler = logging.StreamHandler() + # streamHandler.setFormatter(formatter) + + l.setLevel(level) + l.addHandler(fileHandler) + # l.addHandler(streamHandler) + return (logging.getLogger(logger_name)) diff --git a/src/routine_manager/views.py b/src/routine_manager/views.py index 1dfd83c..65b7532 100644 --- a/src/routine_manager/views.py +++ b/src/routine_manager/views.py @@ -7,6 +7,11 @@ from .validators import check_plan_validity, check_album_validity, check_sequenc from .RequestSerializer import RequestSerializer import scheduler +""" logger """ +from django.conf import settings +import logger.config as l +log = l.setupLogger("l1", "routine_manager-views") + """ XML Export / Import utils """ from wsgiref.util import FileWrapper from django.utils.encoding import smart_str @@ -24,6 +29,9 @@ def requests_list(request, status=0, message=""): Retrieves and display the routines list (routine manager main page) """ + if settings.DEBUG: + log.info("From requests_list") + if status == "-1": error = True elif status == "1": @@ -54,6 +62,9 @@ def action_request(request, req_id, action, status=0, message=""): req = Request.objects.get(id=req_id) depth_level = 1 + if settings.DEBUG: + log.info("From action_request") + if status == "-1": error = True elif status == "1": @@ -91,6 +102,9 @@ def request_validate(request, req_id): req = Request.objects.get(id=req_id) form = RequestForm(instance=req, data=request.POST) + if (settings.DEBUG): + log.info("From request_validate") + if action == "cancel": if req.name == "New request": req.delete() @@ -123,6 +137,9 @@ def action_sequence(request, seq_id, action, status=0, message=""): req_id = req.id depth_level = 2 + if (settings.DEBUG): + log.info("From action_sequence") + if status == "-1": error = True elif status == "1": @@ -158,6 +175,9 @@ def sequence_validate(request, seq_id): Possible actions : Cancel, Save, Save and add album, Delete """ + if (settings.DEBUG): + log.info("From sequence_validate") + seq_id = int(seq_id) action = request.POST.get("action") seq = Sequence.objects.get(id=seq_id) @@ -197,6 +217,9 @@ def action_album(request, alb_id, action, status=0, message=""): seq_id = alb.sequence.id depth_level = 3 + if (settings.DEBUG): + log.info("From action_album") + if status == "-1": error = True elif status == "1": @@ -235,6 +258,10 @@ def album_validate(request, alb_id): alb = Album.objects.get(id=alb_id) form = AlbumForm(instance=alb, data=request.POST) + if (settings.DEBUG): + log.info("From album_validate") + + if action == "cancel": if alb.name == "New album": alb.delete() @@ -272,6 +299,9 @@ def action_plan(request, plan_id, action, status=0, message=""): alb_id = plan.album.id depth_level = 4 + if (settings.DEBUG): + log.info("From action_plan") + if status == "-1": error = True elif status == "1": @@ -310,6 +340,10 @@ def plan_validate(request, plan_id): plan = Plan.objects.get(id=plan_id) form = PlanForm(instance=plan, data=request.POST) + if (settings.DEBUG): + log.info("From plan_validate") + + if action == "cancel": if plan.name == "New plan": plan.delete() @@ -376,6 +410,9 @@ def submit_request(request, req_id, redir): Submits a request and its sequences for scheduling """ + if (settings.DEBUG): + log.info("From submit_request") + req = Request.objects.get(id=req_id) error = False if req.complete == False: @@ -409,6 +446,9 @@ def unsubmit_request(request, req_id): Unsubmits a request and remove its sequences from scheduling """ + if (settings.DEBUG): + log.info("From unsubmit_request") + req = Request.objects.get(id=req_id) # TODO: uncomment pour la production @@ -438,6 +478,9 @@ def export_request(request, req_id): Create an XML file with the given request, and send a download request to the user """ + if (settings.DEBUG): + log.info("From export_request") + req = Request.objects.get(id=req_id) if req.complete == False: message = "Request must be completely valid to be serialized" @@ -463,6 +506,9 @@ def import_request(request): Don't do anything if there is a single error into the file """ + if (settings.DEBUG): + log.info("From import_request") + if request.method == "POST": file = request.FILES.get("request_file") if file is None: -- libgit2 0.21.2