From 2762933c83287184d7d9f9351d7c1d58490ad7e1 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 11 May 2022 07:52:35 -0700 Subject: [PATCH] Configure logging with logging_config.json See https://docs.python.org/3/library/logging.config.html for details on the schema for this file. --- logging_config.json | 27 +++++++++++++++++++++++++++ roguebasin/__main__.py | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 logging_config.json diff --git a/logging_config.json b/logging_config.json new file mode 100644 index 0000000..0c03b92 --- /dev/null +++ b/logging_config.json @@ -0,0 +1,27 @@ +{ + "version": 1, + "formatters": { + "default": { + "format": "%(asctime)s %(name)s: %(message)s", + "datefmt": "%Y-%m-%d %I:%M:%S" + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "formatter": "default", + "level": "DEBUG", + "stream": "ext://sys.stdout" + } + }, + "loggers": { + "actions.movement": { + "level": "DEBUG", + "handlers": ["console"] + } + }, + "root": { + "level": "DEBUG", + "handlers": ["console"] + } +} \ No newline at end of file diff --git a/roguebasin/__main__.py b/roguebasin/__main__.py index 8232c2a..a396d7a 100644 --- a/roguebasin/__main__.py +++ b/roguebasin/__main__.py @@ -1,7 +1,9 @@ # Eryn Wells import argparse +import json import logging +import logging.config import os.path import sys import tcod @@ -23,29 +25,51 @@ def parse_args(argv, *a, **kw): return args def init_logging(args): - root_logger = logging.getLogger('') - root_logger.setLevel(logging.DEBUG if args.debug else logging.INFO) + '''Set up the logging system by (preferrably) reading a logging configuration file.''' + logging_config_path = find_logging_config() + if logging_config_path: + with open(logging_config_path, encoding='utf-8') as logging_config_file: + logging_config = json.load(logging_config_file) + logging.config.dictConfig(logging_config) + LOG.info('Found logging configuration at %s', logging_config_path) + else: + root_logger = logging.getLogger('') + root_logger.setLevel(logging.DEBUG if args.debug else logging.INFO) - stderr_handler = logging.StreamHandler() - stderr_handler.setFormatter(logging.Formatter("%(asctime)s %(name)s: %(message)s")) + stderr_handler = logging.StreamHandler() + stderr_handler.setFormatter(logging.Formatter("%(asctime)s %(name)s: %(message)s")) - root_logger.addHandler(stderr_handler) + root_logger.addHandler(stderr_handler) + +def walk_up_directories_of_path(path): + while path and path != '/': + path = os.path.dirname(path) + yield path def find_fonts_directory(): '''Walk up the filesystem tree from this script to find a fonts/ directory.''' - parent_dir = os.path.dirname(__file__) - while parent_dir and parent_dir != '/': + for parent_dir in walk_up_directories_of_path(__file__): possible_fonts_dir = os.path.join(parent_dir, 'fonts') - LOG.debug('Checking for fonts dir at %s', possible_fonts_dir) if os.path.isdir(possible_fonts_dir): LOG.info('Found fonts dir %s', possible_fonts_dir) break - parent_dir = os.path.dirname(parent_dir) else: return None return possible_fonts_dir +def find_logging_config(): + '''Walk up the filesystem from this script to find a logging_config.json''' + for parent_dir in walk_up_directories_of_path(__file__): + possible_logging_config_file = os.path.join(parent_dir, 'logging_config.json') + if os.path.isfile(possible_logging_config_file): + LOG.info('Found logging config file %s', possible_logging_config_file) + break + else: + return None + + return possible_logging_config_file + def main(argv): args = parse_args(argv[1:], prog=argv[0])