log: change error printing to logging
This commit is contained in:
parent
63300e6012
commit
cdd7a2569f
1 changed files with 14 additions and 23 deletions
37
discorss.py
37
discorss.py
|
|
@ -262,24 +262,25 @@ class Discorss:
|
||||||
os.environ["TZ"] = "America/Toronto"
|
os.environ["TZ"] = "America/Toronto"
|
||||||
time.tzset()
|
time.tzset()
|
||||||
self.now = time.mktime(time.localtime())
|
self.now = time.mktime(time.localtime())
|
||||||
|
# Set up logging
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
logging.basicConfig(
|
||||||
|
filename=self.log_file_path,
|
||||||
|
encoding="utf-8",
|
||||||
|
level=logging.WARNING,
|
||||||
|
datefmt="%m/%d/%Y %H:%M:%S",
|
||||||
|
format="%(asctime)s [%(threadName)s] -> %(levelname)s: %(message)s",
|
||||||
|
)
|
||||||
# Check for log and config files/paths, create empty directories if needed
|
# Check for log and config files/paths, create empty directories if needed
|
||||||
# TODO: change output to log file, as warning/error
|
# TODO: change output to log file, as warning/error
|
||||||
if not Path(self.log_dir).exists():
|
if not Path(self.log_dir).exists():
|
||||||
print(
|
self.logger.warning("No log file path exists. Yark! We'll try and make %s...", self.log_dir)
|
||||||
"No log file path exists. Yark! We'll try and make {}...".format(
|
|
||||||
self.log_dir
|
|
||||||
)
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
Path(self.log_dir).mkdir(parents=True, exist_ok=True)
|
Path(self.log_dir).mkdir(parents=True, exist_ok=True)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
print(
|
self.logger.critical("The path {} already exists and is not a directory!".format(self.log_dir))
|
||||||
"The path {} already exists and is not a directory!".format(
|
|
||||||
self.log_dir
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if not Path(self.config_file_path).exists():
|
if not Path(self.config_file_path).exists():
|
||||||
print(
|
self.logger.warning(
|
||||||
"No config file at {}! Snarf. We'll try and make {}...".format(
|
"No config file at {}! Snarf. We'll try and make {}...".format(
|
||||||
self.config_file_path, self.config_dir
|
self.config_file_path, self.config_dir
|
||||||
)
|
)
|
||||||
|
|
@ -287,7 +288,7 @@ class Discorss:
|
||||||
try:
|
try:
|
||||||
Path(self.config_dir).mkdir(parents=True, exist_ok=True)
|
Path(self.config_dir).mkdir(parents=True, exist_ok=True)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
print(
|
self.warning.critical(
|
||||||
"The config dir {} already exists and is not a directory! Please fix manually. Quitting!".format(
|
"The config dir {} already exists and is not a directory! Please fix manually. Quitting!".format(
|
||||||
self.config_dir
|
self.config_dir
|
||||||
)
|
)
|
||||||
|
|
@ -297,20 +298,11 @@ class Discorss:
|
||||||
# Loading the config file
|
# Loading the config file
|
||||||
with open(self.config_file_path, "r") as config_file:
|
with open(self.config_file_path, "r") as config_file:
|
||||||
self.app_config = json.load(config_file)
|
self.app_config = json.load(config_file)
|
||||||
# Set up logging
|
|
||||||
self.logger = logging.getLogger(__name__)
|
|
||||||
logging.basicConfig(
|
|
||||||
filename=self.log_file_path,
|
|
||||||
encoding="utf-8",
|
|
||||||
level=logging.INFO,
|
|
||||||
datefmt="%m/%d/%Y %H:%M:%S",
|
|
||||||
format="%(asctime)s [%(threadName)s] -> %(levelname)s: %(message)s",
|
|
||||||
)
|
|
||||||
self.logger.info("========= Started discorss.py ==========")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
self.setup() # Handle the config and log paths
|
self.setup() # Handle the config and log paths
|
||||||
|
self.logger.info("Starting DiscoRSS run...")
|
||||||
try:
|
try:
|
||||||
last_check = self.app_config["lastupdate"]
|
last_check = self.app_config["lastupdate"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|
@ -324,7 +316,6 @@ class Discorss:
|
||||||
self.app_config["lastupdate"] = self.now
|
self.app_config["lastupdate"] = self.now
|
||||||
with open(self.config_file_path, "w") as config_file:
|
with open(self.config_file_path, "w") as config_file:
|
||||||
json.dump(self.app_config, config_file, indent=4)
|
json.dump(self.app_config, config_file, indent=4)
|
||||||
self.logger.info("========= Ended discord.py =========")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue