Просмотр исходного кода

Add log configuration infrastructure

Alec Nikolas Reiter 7 лет назад
Родитель
Сommit
35af6dcb8f
4 измененных файлов с 18 добавлено и 1 удалено
  1. 4 0
      flaskbb/app.py
  2. 10 1
      flaskbb/cli/main.py
  3. 1 0
      flaskbb/configs/config.cfg.template
  4. 3 0
      flaskbb/configs/default.py

+ 4 - 0
flaskbb/app.py

@@ -10,6 +10,7 @@
 """
 import os
 import logging
+import logging.config
 import time
 from functools import partial
 
@@ -337,6 +338,9 @@ def configure_logging(app):
         mail_handler.setFormatter(formatter)
         app.logger.addHandler(mail_handler)
 
+    if app.config.get('LOG_CONF_FILE'):
+        logging.config.fileConfig(app.config['LOG_CONF_FILE'], disable_existing_loggers=False)
+
     if app.config["SQLALCHEMY_ECHO"]:
         # Ref: http://stackoverflow.com/a/8428546
         @event.listens_for(Engine, "before_cursor_execute")

+ 10 - 1
flaskbb/cli/main.py

@@ -472,7 +472,8 @@ def generate_config(development, output, force):
         "mail_admin_address": "admin@yourdomain",
         "secret_key": binascii.hexlify(os.urandom(24)).decode(),
         "csrf_secret_key": binascii.hexlify(os.urandom(24)).decode(),
-        "timestamp": datetime.utcnow().strftime("%A, %d. %B %Y at %H:%M")
+        "timestamp": datetime.utcnow().strftime("%A, %d. %B %Y at %H:%M"),
+        "log_config_path": "",
     }
 
     if not force:
@@ -583,6 +584,14 @@ def generate_config(development, output, force):
         click.style("Mail Admin Email", fg="magenta"),
         default=default_conf.get("mail_admin_address"))
 
+    click.secho("Optional filepath to load a logging configuration file from. "
+                "See the Python logging documentation for more detail.\n"
+                "\thttps://docs.python.org/library/logging.config.html#logging-config-fileformat",
+                fg="cyan")
+    default_conf["log_config_path"] =click.prompt(
+        click.style("Logging Config Path", fg="magenta"),
+        default=default_conf.get("log_config_path"))
+
     write_config(default_conf, config_template, config_path)
 
     # Finished

+ 1 - 0
flaskbb/configs/config.cfg.template

@@ -154,6 +154,7 @@ ADMINS = ["{{ mail_admin_address }}"]
 # If SEND_LOGS is set to True, the admins (see the mail configuration) will
 # recieve the error logs per email.
 SEND_LOGS = False
+LOG_CONF_FILE = {{ log_config_path }}
 
 
 # FlaskBB Settings

+ 3 - 0
flaskbb/configs/default.py

@@ -194,3 +194,6 @@ class DefaultConfig(object):
     ADMIN_URL_PREFIX = "/admin"
     # Plugin Folder
     PLUGINS_FOLDER = os.path.join(basedir, "flaskbb", "plugins")
+
+    # Logging Config Path
+    LOG_CONF_FILE = None