added file logging for coretk, using a time rotating logger, added command line arg to choose coretk log level

This commit is contained in:
Blake Harnden 2020-01-28 12:30:12 -08:00
parent 7cc52f13d6
commit 6aa7d2175d
2 changed files with 11 additions and 5 deletions

View file

@ -1,4 +1,3 @@
import logging
import os import os
import shutil import shutil
from pathlib import Path from pathlib import Path
@ -16,6 +15,7 @@ ICONS_PATH = HOME_PATH.joinpath("icons")
MOBILITY_PATH = HOME_PATH.joinpath("mobility") MOBILITY_PATH = HOME_PATH.joinpath("mobility")
XMLS_PATH = HOME_PATH.joinpath("xmls") XMLS_PATH = HOME_PATH.joinpath("xmls")
CONFIG_PATH = HOME_PATH.joinpath("gui.yaml") CONFIG_PATH = HOME_PATH.joinpath("gui.yaml")
LOG_PATH = HOME_PATH.joinpath("gui.log")
# local paths # local paths
DATA_PATH = Path(__file__).parent.joinpath("data") DATA_PATH = Path(__file__).parent.joinpath("data")
@ -52,9 +52,7 @@ def copy_files(current_path, new_path):
def check_directory(): def check_directory():
if HOME_PATH.exists(): if HOME_PATH.exists():
logging.info("~/.coretk exists")
return return
logging.info("creating ~/.coretk")
HOME_PATH.mkdir() HOME_PATH.mkdir()
BACKGROUNDS_PATH.mkdir() BACKGROUNDS_PATH.mkdir()
CUSTOM_EMANE_PATH.mkdir() CUSTOM_EMANE_PATH.mkdir()

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import logging import logging
from logging.handlers import TimedRotatingFileHandler
from core.gui import appconfig from core.gui import appconfig
from core.gui.app import Application from core.gui.app import Application
@ -9,16 +10,23 @@ from core.gui.images import Images
if __name__ == "__main__": if __name__ == "__main__":
# parse flags # parse flags
parser = argparse.ArgumentParser(description=f"CORE Python Tk GUI") parser = argparse.ArgumentParser(description=f"CORE Python Tk GUI")
parser.add_argument("-l", "--level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="INFO",
help="logging level")
parser.add_argument("-p", "--proxy", action="store_true", help="enable proxy") parser.add_argument("-p", "--proxy", action="store_true", help="enable proxy")
args = parser.parse_args() args = parser.parse_args()
# check home directory exists and create if necessary
appconfig.check_directory()
# setup logging # setup logging
log_format = "%(asctime)s - %(levelname)s - %(module)s:%(funcName)s - %(message)s" log_format = "%(asctime)s - %(levelname)s - %(module)s:%(funcName)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=log_format) stream_handler = logging.StreamHandler()
file_handler = TimedRotatingFileHandler(filename=appconfig.LOG_PATH, when="D", backupCount=5)
log_level = logging.getLevelName(args.level)
logging.basicConfig(level=log_level, format=log_format, handlers=[stream_handler, file_handler])
logging.getLogger("PIL").setLevel(logging.ERROR) logging.getLogger("PIL").setLevel(logging.ERROR)
# start app # start app
Images.load_all() Images.load_all()
appconfig.check_directory()
app = Application(args.proxy) app = Application(args.proxy)
app.mainloop() app.mainloop()