diff --git a/nutcase/app/app/__init__.py b/nutcase/app/app/__init__.py index 8bbf06e..7070532 100755 --- a/nutcase/app/app/__init__.py +++ b/nutcase/app/app/__init__.py @@ -1,39 +1,15 @@ import logging -from logging.handlers import SMTPHandler, RotatingFileHandler +# from logging.handlers import SMTPHandler, RotatingFileHandler import os from flask import Flask from config import Config_Development # , Config_Production, Config -# from flask_moment import Moment # https://momentjs.com/ -from app.api import webhook -from app.api import configuration +from app.utils import webhook +from app.utils import configuration +from app.utils import app_log_config #================================================= # Initialise components -# -# moment = Moment() - -#================================================================================================== -# Add_Level function -#================================================================================================== -def Add_Logging_Levels(): - DEBUGV_LEVEL_NUM = 9 - DEBUGVV_LEVEL_NUM = 8 - - logging.addLevelName(DEBUGV_LEVEL_NUM, "DEBUGV") - logging.addLevelName(DEBUGVV_LEVEL_NUM, "DEBUGVV") - - def debugv(self, message, *args, **kws): - if self.isEnabledFor(DEBUGV_LEVEL_NUM): - self._log(DEBUGV_LEVEL_NUM, message, args, **kws) - - def debugvv(self, message, *args, **kws): - if self.isEnabledFor(DEBUGVV_LEVEL_NUM): - self._log(DEBUGVV_LEVEL_NUM, message, args, **kws) - - logging.Logger.debugv = debugv - logging.Logger.debugvv = debugvv - return #================================================================================================== # Main app creation function @@ -43,7 +19,7 @@ def create_app(config_class=Config_Development): app.config.from_object(config_class) #==================================================================================== - # Define the app Blueprints + # Register the app Blueprints #==================================================================================== from app.main import bp as main_bp app.register_blueprint(main_bp) @@ -55,47 +31,27 @@ def create_app(config_class=Config_Development): app.register_blueprint(events_bp, url_prefix='/events') #==================================================================================== - # Set up email for the application log + # Set up the application logging #==================================================================================== - # if not app.debug and not app.testing: - # if app.config['MAIL_SERVER']: - # auth = None - # if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']: - # auth = (app.config['MAIL_USERNAME'], - # app.config['MAIL_PASSWORD']) - # secure = None - # if app.config['MAIL_USE_TLS']: - # secure = () - # mail_handler = SMTPHandler( - # mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']), - # fromaddr='no-reply@' + app.config['MAIL_SERVER'], - # toaddrs=app.config['MAIL_DEFAULT_SENDER'], subject=app.config['MAIL_DEFAULT_SUBJECT'] + " " + 'App Failure', - # credentials=auth, secure=secure) - # mail_handler.setLevel(logging.ERROR) - # app.logger.addHandler(mail_handler) + app_log_config.Add_Logging_Levels() - #==================================================================================== - # Set up the application log file - #==================================================================================== - Logfile_Directory = os.path.join(app.root_path, app.config['LOGFILE_RELATIVE_PATH']) + Logfile_Directory = os.path.join(app.config['CONFIG_PATH'], app.config['LOGFILE_SUBPATH']) if not os.path.exists(Logfile_Directory): os.mkdir(Logfile_Directory) - Logfile_Fullname = os.path.join(Logfile_Directory, 'nutcase.log') - Logfile_Handler = RotatingFileHandler(Logfile_Fullname, maxBytes=250000, backupCount=10) - Log_Format = '%(asctime)s %(levelname)-8s %(module)s: %(message)s' - Logfile_Handler.setFormatter(logging.Formatter(Log_Format)) - Logfile_Handler.name = "logfile_handler" - app.logger.addHandler(Logfile_Handler) - - Add_Logging_Levels() + app_log_config.Add_RF_Handler( app ) #==================================================================================== # Set the logging level from the environment variable LOG_LEVEL if present. #==================================================================================== - try: app.logger.setLevel( os.environ.get('LOG_LEVEL', "DEBUG").upper() ) - except Exception: app.logger.setLevel( logging.DEBUG ) - + Console_Level = os.environ.get('LOG_LEVEL', app.config['DEFAULT_CONSOLE_LEVEL']).upper() + Logfile_Level = os.environ.get('LOG_LEVEL', app.config['DEFAULT_LOGFILE_LEVEL']).upper() + + app.logger.info("Init: Console_Level {} Logfile_Level {}".format( Console_Level, Logfile_Level )) + app_log_config.Set_Log_Level( app, Console_Level, "con" ) + app_log_config.Set_Log_Level( app, Logfile_Level, "rfh" ) + app.logger.setLevel( 1 ) # Set the root logger to pass everything on + #==================================================================================== # Load the app configuration from a YAML file #==================================================================================== @@ -104,10 +60,12 @@ def create_app(config_class=Config_Development): #==================================================================================== # Log starting and call a web hook #==================================================================================== - app.logger.info("{} starting. Version {}, Logging level {}".format( - app.config['APP_NAME'], app.config['APP_VERSION'], - logging.getLevelName(app.logger.level)).lower() ) - + app.logger.info("{} starting. Version {}, Logging: console {} logfile {}".format( + app.config['APP_NAME'], app.config['APP_VERSION'], + logging.getLevelName( app_log_config.Get_Handler( app, 'con' ).level), + logging.getLevelName( app_log_config.Get_Handler( app, 'rfh' ).level), + )) + webhook.Call_Webhook( app, "ok", { "status": "up", "msg": "NUTCase starting" } ) return app diff --git a/nutcase/app/app/api/routes.py b/nutcase/app/app/api/routes.py index ed92cf0..f83643f 100755 --- a/nutcase/app/app/api/routes.py +++ b/nutcase/app/app/api/routes.py @@ -2,8 +2,8 @@ from app.api import bp -from app.api import scrape -from app.api import gui_data_format +from app.utils import scrape +from app.utils import gui_data_format #==================================================================================== # Serve the end-point /api/status diff --git a/nutcase/app/app/main/routes.py b/nutcase/app/app/main/routes.py index 7d05323..1077604 100755 --- a/nutcase/app/app/main/routes.py +++ b/nutcase/app/app/main/routes.py @@ -9,13 +9,14 @@ import os import datetime; -from app.api import server_constants -from app.api import format_to_text -from app.api import format_to_json -from app.api import apc_server_handler -from app.api import configuration -from app.api import scrape -from app.api import file_utils +from app.utils import server_constants +from app.utils import format_to_text +from app.utils import format_to_json +from app.utils import apc_server_handler +from app.utils import configuration +from app.utils import scrape +from app.utils import file_utils +from app.utils import gui_data_format #======================================================================= # before_app_request - Run before every request to check config file @@ -39,9 +40,13 @@ def route_index(): # title = 'Get URL failed', # body = 'Return code ' # )) +# TODO - reduce logging levels + for a in request.args: + current_app.logger.debug("request.args: key {} value {}".format( a, request.args[a] )) Addr = request.args.get("addr", default='default') Device = request.args.get("dev", default='default') + current_app.logger.debug("route_index: Addr {} Device {}".format( Addr, Device )) if ('target_device' not in session) or (session['target_device'] != Addr + Device): Length = current_app.config['CHART_SAMPLES'] @@ -146,7 +151,11 @@ def route_metrics(): #==================================================================================== @bp.route('/log') @bp.route('/log/') -def route_log(Filename='nutcase.log'): +def route_log(Filename=''): + if Filename == '': + Filename = current_app.config['LOGFILE_NAME'] + + # Get the number of lines requested Log_Lines = current_app.config["DEFAULT_LOG_LINES"] if Lines := request.args.get("lines"): try: @@ -155,15 +164,18 @@ def route_log(Filename='nutcase.log'): except: current_app.logger.error("Error defining log lines: {}".format( Lines )) - Logfile_Dir = os.path.join(current_app.root_path, current_app.config['LOGFILE_RELATIVE_PATH']) - Logfile_Fullname = os.path.join(Logfile_Dir, Filename ) - File_List = os.listdir(Logfile_Dir) + # Generate the HTML for the list of files + Logfile_Directory = os.path.join(current_app.config['CONFIG_PATH'], current_app.config['LOGFILE_SUBPATH']) + Logfile_Fullname = os.path.join(Logfile_Directory, Filename ) + + File_List = gui_data_format.Generate_Log_Files_Pulldown( Logfile_Directory ) + # Get lines from the requested log file rtn, Lines = file_utils.Tail_File( Logfile_Fullname, Log_Lines ) if not rtn: Lines = [ "
Log file not found
" ] - return render_template('main/log.html', title="Log file", lines=Lines, files=File_List, filename=Filename ) + return render_template('main/log.html', title="Log file", lines=Lines, files=Markup(File_List), filename=Filename ) #==================================================================================== # Serve the end-point /help diff --git a/nutcase/app/app/templates/bootstrap/bs5_base_top_navbar.html b/nutcase/app/app/templates/bootstrap/bs5_base_top_navbar.html index 017b645..dba0df8 100755 --- a/nutcase/app/app/templates/bootstrap/bs5_base_top_navbar.html +++ b/nutcase/app/app/templates/bootstrap/bs5_base_top_navbar.html @@ -34,18 +34,18 @@