[feat](logging): Make logging Pythonic

Signed-off-by: Dom Rodriguez <shymega@shymega.org.uk>
This commit is contained in:
Dom Rodriguez 2020-09-17 23:16:54 +01:00
parent 6747275463
commit 98e25338c5
No known key found for this signature in database
GPG Key ID: 72DCF1231E54BD43

View File

@ -7,14 +7,20 @@ import os
import sys import sys
import feedparser import feedparser
import logging import logging
import listparser
from os import environ
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
import json
log = None log = None
LOG_LEVEL = environ.get("SR_LOG_LEVEl", "ERROR")
fg = None fg = None
FEED_OUT_PATH = None FEED_OUT_PATH = None
FEED_OUT_TYPE = None FEED_OUT_TYPE = None
FEED_LIST_PATH = None FEED_LIST_PATH = None
FEEDS = [] FEEDS = []
CFG = None
def setup_logging() -> None: def setup_logging() -> None:
@ -23,14 +29,12 @@ def setup_logging() -> None:
""" """
global log global log
log = logging.getLogger("singlerss") log = logging.getLogger(__name__)
out_handler = logging.StreamHandler(sys.stderr) log.setLevel(LOG_LEVEL)
out_handler.setFormatter( ch = logging.StreamHandler(sys.stderr)
logging.Formatter( ch.setLevel(LOG_LEVEL)
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s' ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
)) log.addHandler(ch)
log.addHandler(out_handler)
log.setLevel(logging.ERROR)
return None return None
@ -69,11 +73,8 @@ def parse_rss_feed(url) -> feedparser.FeedParserDict:
# Hopefully this should parse.. # Hopefully this should parse..
return feedparser.parse(url) return feedparser.parse(url)
except Exception: except Exception:
log.warninging("Failed to parse RSS feed.") log.warning("Failed to parse RSS feed.")
# Now, we could handle gracefully. # Now, we could handle gracefully.
# This code is a WIP, but maybe we shouldn't crash?
log.warninging("Cannot continue, we want all the feeds to work!")
sys.exit(1)
def main(): def main():