#168 cli: add --debug option
Merged 3 years ago by kparal. Opened 3 years ago by kparal.

file modified
+12 -12
@@ -4,7 +4,6 @@ 

  import logging

  import logging.handlers

  import os

- import sys

  

  from . import config

  
@@ -71,41 +70,42 @@ 

      from werkzeug.contrib.fixers import ProxyFix

      app.wsgi_app = ProxyFix(app.wsgi_app, num_proxies=1)

  

- # setup logging

- fmt = '[%(filename)s:%(lineno)d] ' if app.debug else '%(module)-12s '

- fmt += '%(asctime)s %(levelname)-7s %(message)s'

- datefmt = '%Y-%m-%d %H:%M:%S'

- loglevel = logging.DEBUG if app.debug else logging.INFO

- formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)

- 

  

  def setup_logging():

+     fmt = '[%(filename)s:%(lineno)d] ' if app.debug else '%(module)-12s '

+     fmt += '%(asctime)s %(levelname)-7s %(message)s'

+     datefmt = '%Y-%m-%d %H:%M:%S'

+     loglevel = logging.DEBUG if app.debug else logging.INFO

+     formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)

+ 

      root_logger = logging.getLogger('')

      root_logger.setLevel(logging.DEBUG)

      root_logger.handlers.clear()

      app.logger.handlers.clear()

  

      if app.config['STREAM_LOGGING']:

-         app.logger.debug("doing stream logging")

          stream_handler = logging.StreamHandler()

          stream_handler.setLevel(loglevel)

          stream_handler.setFormatter(formatter)

          root_logger.addHandler(stream_handler)

+         app.logger.debug("doing stream logging")

  

      if app.config['SYSLOG_LOGGING']:

-         app.logger.debug("doing syslog logging")

          syslog_handler = logging.handlers.SysLogHandler(

              address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_LOCAL4)

          syslog_handler.setLevel(loglevel)

          syslog_handler.setFormatter(formatter)

          root_logger.addHandler(syslog_handler)

+         app.logger.debug("doing syslog logging")

  

      if app.config['FILE_LOGGING'] and app.config['LOGFILE']:

-         app.logger.debug("doing file logging to %s" % app.config['LOGFILE'])

-         file_handler = logging.handlers.RotatingFileHandler(app.config['LOGFILE'], maxBytes=500000, backupCount=5)

+         file_handler = logging.handlers.RotatingFileHandler(

+             app.config['LOGFILE'], maxBytes=500000, backupCount=5)

          file_handler.setLevel(loglevel)

          file_handler.setFormatter(formatter)

          root_logger.addHandler(file_handler)

+         app.logger.debug("doing file logging to %s" % app.config['LOGFILE'])

+ 

  

  setup_logging()

with --debug, the function setup_logging() is called twice, probably not a problem...

  

file modified
+9 -1
@@ -3,7 +3,7 @@ 

  from argparse import ArgumentParser

  import secrets

  

- from blockerbugs import db, app

+ from blockerbugs import db, app, setup_logging

  from blockerbugs.models.milestone import Milestone

  from blockerbugs.models.release import Release

  from blockerbugs.util.bug_sync import BugSync
@@ -233,6 +233,9 @@ 

  def main():

      parser = ArgumentParser()

  

+     parser.add_argument('--debug', action='store_true', default=False,

+                         help='Enable debug logs')

+ 

      subparsers = parser.add_subparsers(dest='command', metavar="<COMMAND>")

  

      init_db_parser = subparsers.add_parser('init_db', help='Initialize DB')
@@ -328,6 +331,11 @@ 

          parser.print_help()

          sys.exit(1)

  

+     if args.debug:

+         app.config['DEBUG'] = True

+         # re-initialize logging with changed params

+         setup_logging()

+ 

      # run the requested command

      args.func(args)

  

Build succeeded.

with --debug, the function setup_logging() is called twice, probably not a problem...

with --debug, the function setup_logging() is called twice, probably not a problem...

Yes I know. It will print a logging statement again, otherwise there should be no drawback. I figured the duplicated statement is not a big deal, rather than work around it somehow or remove the log output completely.

It would be nice to be able to type python run_cli.py sync --debug (i.e. --debug at the end). But don't think it's worth it if this would mean adding --debug argument to all subparsers.

with --debug, the function setup_logging() is called twice, probably not a problem...

Yes I know. It will print a logging statement again, otherwise there should be no drawback. I figured the duplicated statement is not a big deal, rather than work around it somehow or remove the log output completely.

Took me a while to figure out what's the purpose of calling it in the cli too, can you maybe add comment there for "future us"? Just my two cents though... :)

LGTM otherwise.

I think the reason is that app.debug is set too late

It would be nice to be able to type python run_cli.py sync --debug (i.e. --debug at the end). But don't think it's worth it if this would mean adding --debug argument to all subparsers.

I haven't found an easier way to do this.

1 new commit added

  • Frantisek asks me to add more comments, what has happened to us?
3 years ago

Took me a while to figure out what's the purpose of calling it in the cli too, can you maybe add comment there for "future us"? Just my two cents though... :)

Done.

Build succeeded.

I assume I have ack from both of you :)

Commit 3fac7a2 fixes this pull-request

Pull-Request has been merged by kparal

3 years ago