From 4626f2233973e0cab9de2488cdff0a658f91eacd Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 14 Jul 2018 13:14:53 +0200 Subject: [PATCH] config documentation --- .gitignore | 1 + doc | 2 +- matemat/webserver/config.py | 19 +++++++++++++++++++ matemat/webserver/httpd.py | 4 ++-- matemat/webserver/test/abstract_httpd_test.py | 3 ++- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index fef1fd9..925c5c5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ *.sqlite3 *.db **/matemat.conf +static/upload/ diff --git a/doc b/doc index d5dc5f7..c68df9d 160000 --- a/doc +++ b/doc @@ -1 +1 @@ -Subproject commit d5dc5f794ad1b959b9d4dce47eeb6068e5a75115 +Subproject commit c68df9d86af1d8d0ebb6b6609efeef14f7103761 diff --git a/matemat/webserver/config.py b/matemat/webserver/config.py index 6c2ac4d..1913c4a 100644 --- a/matemat/webserver/config.py +++ b/matemat/webserver/config.py @@ -8,13 +8,30 @@ from configparser import ConfigParser def parse_logging(symbolic_level: str, symbolic_target: str) -> Tuple[int, logging.Handler]: + """ + Parse the LogLevel and LogTarget from configuration. + The LogLevel value may be either an integer or a log level literal defined in the Python builtin logging module. + The LogTarget value may be one of the following: + - `stderr` to log to error output (default) + - `stdout` to log to standard output + - `none` to disable logging + - Any other value is interpreted as a filename + + :param symbolic_level: The value for the LogLevel key. + :param symbolic_target: The value for the LogTarget key. + :return: A tuple of the log level (as an int) and the logging.Handler derived from the log target string. + """ try: + # Attempt to cast the log level into an int level: int = int(symbolic_level) except ValueError: try: + # If this fails, look up the name in the logging class level = int(logging.getLevelName(symbolic_level)) except ValueError: + # LogLevel value was neither an int nor a known literal raise ValueError(f'Unknown log level: {symbolic_level}') + # Special handling of the strings "stderr", "stdout" and "none" if symbolic_target == 'stderr': target: logging.Handler = logging.StreamHandler(sys.stderr) elif symbolic_target == 'stdout': @@ -22,7 +39,9 @@ def parse_logging(symbolic_level: str, symbolic_target: str) -> Tuple[int, loggi elif symbolic_target == 'none': target = logging.NullHandler() else: + # Fallback: Interpret string as filename target = logging.FileHandler(os.path.abspath(os.path.expanduser(symbolic_target))) + # Return log level and log target return level, target diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index 6a211e9..1aae08e 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -120,8 +120,8 @@ class MatematHTTPServer(HTTPServer): # Set up logger self.logger: logging.Logger = logging.getLogger('matemat.webserver') self.logger.setLevel(log_level) - # Set up log handler - log_handler.setFormatter(logging.Formatter('%(asctime)s @ %(name)s [%(levelname)s]: %(message)s')) + # Set up the log handler's (obtained from config parsing) format string + log_handler.setFormatter(logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s')) self.logger.addHandler(log_handler) diff --git a/matemat/webserver/test/abstract_httpd_test.py b/matemat/webserver/test/abstract_httpd_test.py index d691caa..0817d41 100644 --- a/matemat/webserver/test/abstract_httpd_test.py +++ b/matemat/webserver/test/abstract_httpd_test.py @@ -119,8 +119,9 @@ class MockServer: # Set up logger self.logger: logging.Logger = logging.getLogger('matemat unit test') self.logger.setLevel(0) + # Initalize a log handler to stderr and set the log format sh: logging.StreamHandler = logging.StreamHandler() - sh.setFormatter(logging.Formatter('%(asctime)s @ %(name)s [%(levelname)s]: %(message)s')) + sh.setFormatter(logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s')) self.logger.addHandler(sh)