import argparse
import os
import sys
import textwrap

from hades.agent import app
from hades.common.cli import ArgumentParser, parser as common_parser
from hades.config.base import ConfigError
from hades.config.loader import load_config, print_config_error
from hades.config.options import CeleryOption


class Formatter(argparse.HelpFormatter):
    def add_usage(self, usage, actions, groups, prefix=None):
        actions = list(actions)
        actions.append(argparse.Action([], dest='worker_options',
                                       metavar='worker options', nargs='?'))
        super().add_usage(usage, actions, groups, prefix)


def main():
    description = textwrap.dedent(
        """
        Run the celery command configured for Hades.

        All arguments except -c/--config and -A/--app are passed to the Celery
        celery as is. You may not provide the -A/--app argument.
        """
    )
    parser = ArgumentParser(description=description,
                            formatter_class=Formatter,
                            parents=[common_parser])
    parser.add_argument('-A', '--app', dest='app', help=argparse.SUPPRESS)
    parser.add_argument('command')
    args, argv = parser.parse_known_args()
    try:
        config = load_config(args.config)
    except ConfigError as e:
        print_config_error(e)
        return os.EX_CONFIG
    app.config_from_object(config.of_type(CeleryOption))
    if args.app:
        parser.error("You may not provide the -A/--app worker argument")
    argv.insert(0, parser.prog)
    argv.insert(1, args.command)
    argv.extend(['-A', 'hades.bin.agent:app'])
    if args.command == 'worker':
        argv.extend(['-n', config.HADES_CELERY_WORKER_HOSTNAME])
    return app.start(argv)


if __name__ == '__main__':
    sys.exit(main())

@@ -104,16 +104,22 @@
  import 
+(%0A    
 Argument
@@ -149,16 +149,61 @@
 n_parser
+, reset_cli_logging,%0A    setup_cli_logging,%0A)
 %0Afrom ha
@@ -1276,16 +1276,57 @@
 _args()%0A
+    setup_cli_logging(parser.prog, args)%0A
     try:
@@ -1455,16 +1455,40 @@
 _CONFIG%0A
+    reset_cli_logging()%0A
     app.

Use regular logging before handing over to celery