diff --git a/.gitignore b/.gitignore index e478148..f04f3ba 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ tts.spec random*.txt quotes.txt tts.zip +tts.log +tts.log.? \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index b43d575..d3f96cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,20 @@ All notable changes to this project will be documented in this file. If there is a `Changed` section please read carefully, as this often means that you will need to adapt your `config.yml`. +## [1.8.1] - 2022-10-23 + +### Added 1.8.1 + +* Added logging to file + ## [1.8.0] - 2022-10-23 ### Added 1.8.0 * Option to disable TTS audio output for !wiki function * Option to configure the number of sentences fetched by !wiki function -* !wikitoff command -* !wikiton command +* !wtoff command +* !wton command ### Changed 1.8.0 diff --git a/tts.exe b/tts.exe index 2e958b5..773e0dc 100644 Binary files a/tts.exe and b/tts.exe differ diff --git a/tts.py b/tts.py index 2e62190..4c3c09b 100644 --- a/tts.py +++ b/tts.py @@ -38,6 +38,7 @@ from urllib.parse import parse_qs from urllib.error import HTTPError from http.server import HTTPServer, BaseHTTPRequestHandler from socketserver import ThreadingMixIn +from logging.handlers import RotatingFileHandler import yaml import requests @@ -1571,6 +1572,13 @@ def load_config(): return CONF +def exceptionlog(etype, evalue, traceback): + """ Log Exceptions to file """ + logging.error( + "Logging an uncaught exception", + exc_info=(etype, evalue, traceback) + ) + def main(): """ Main loop """ @@ -1578,7 +1586,12 @@ def main(): CONF = load_config() lastreload = datetime.datetime.now() - logging.getLogger().setLevel(CONF['LOG_LEVEL']) + + for handler in rootLogger.handlers: + if isinstance(handler, logging.StreamHandler): + handler.setLevel(CONF['LOG_LEVEL']) + + sys.excepthook = exceptionlog if CONF['LOG_LEVEL'] == 'DEBUG': sys.tracebacklimit = 5 @@ -1646,10 +1659,20 @@ def main(): os.kill(os.getpid(), signal.SIGTERM) if __name__ == "__main__": - logging.basicConfig( - level=logging.DEBUG, - format='%(asctime)s %(module)s %(threadName)s %(levelname)s: %(message)s' + logFormatter = logging.Formatter( + "%(asctime)s %(module)s %(threadName)s %(levelname)s: %(message)s" ) + rootLogger = logging.getLogger() + + fileHandler = RotatingFileHandler("tts.log", backupCount=9) + fileHandler.setFormatter(logFormatter) + rootLogger.addHandler(fileHandler) + fileHandler.doRollover() + + consoleHandler = logging.StreamHandler() + consoleHandler.setFormatter(logFormatter) + rootLogger.addHandler(consoleHandler) + rootLogger.level = logging.DEBUG sys.tracebacklimit = 3