changeset 7787:efd088f7903b

kallithea-cli: introduce generic custom logging configuration for each cli command Command line commands have different needs for logging than the server process. To enable that use case, allow config sections to be enabled (and thus augment existing sections), depending on which kallithea-cli command is running.
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 31 Jul 2019 21:53:59 +0200
parents 0f33c1da2fd5
children 3ea66ef563f2
files kallithea/bin/kallithea_cli_base.py
diffstat 1 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/kallithea_cli_base.py	Mon Jul 01 10:05:00 2019 +0200
+++ b/kallithea/bin/kallithea_cli_base.py	Wed Jul 31 21:53:59 2019 +0200
@@ -13,8 +13,10 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import click
+import cStringIO
 import functools
 import os
+import re
 import sys
 
 import kallithea
@@ -31,6 +33,19 @@
 kallithea_cli_path = sys.argv[0]
 
 
+def read_config(ini_file_name, strip_section_prefix):
+    """Read ini_file_name content, and for all sections like '[X:Y]' where X is
+    strip_section_prefix, replace the section name with '[Y]'."""
+
+    def repl(m):
+        if m.group(1) == strip_section_prefix:
+            return '[%s]' % m.group(2)
+        return m.group(0)
+
+    with open(ini_file_name) as f:
+        return re.sub(r'^\[([^:]+):(.*)]', repl, f.read(), flags=re.MULTILINE)
+
+
 # This placeholder is the main entry point for the kallithea-cli command
 @click.group(context_settings=dict(help_option_names=['-h', '--help']))
 def cli():
@@ -55,7 +70,8 @@
             def runtime_wrapper(config_file, *args, **kwargs):
                 path_to_ini_file = os.path.realpath(config_file)
                 kallithea.CONFIG = paste.deploy.appconfig('config:' + path_to_ini_file)
-                logging.config.fileConfig(path_to_ini_file)
+                config_bytes = read_config(path_to_ini_file, strip_section_prefix=annotated.__name__)
+                logging.config.fileConfig(cStringIO.StringIO(config_bytes))
                 if config_file_initialize_app:
                     kallithea.config.middleware.make_app_without_logging(kallithea.CONFIG.global_conf, **kallithea.CONFIG.local_conf)
                     kallithea.lib.utils.setup_cache_regions(kallithea.CONFIG)