Skip to content

Instantly share code, notes, and snippets.

@gridhead
Created November 9, 2021 09:09
Show Gist options
  • Save gridhead/a75ab49c9305c1af4694814f96cba973 to your computer and use it in GitHub Desktop.
Save gridhead/a75ab49c9305c1af4694814f96cba973 to your computer and use it in GitHub Desktop.
Duffy Logging
import logging
import click
import uvicorn
from ..configuration import config, read_configuration
from ..version import __version__
DEFAULT_CONFIG_FILE = "/etc/duffy.yaml"
log = logging.getLogger(__name__)
def init_config(ctx, param, filename):
try:
read_configuration(filename)
except FileNotFoundError:
read_configuration()
ctx.default_map = config
@click.command(name="duffy")
@click.option(
"-c",
"--config",
type=click.Path(dir_okay=False),
default=DEFAULT_CONFIG_FILE,
callback=init_config,
is_eager=True,
expose_value=False,
help="Read option defaults from the specified YAML file.",
show_default=True,
)
@click.option(
"--reload/--no-reload", default=False, help="Automatically reload if the code is changed."
)
@click.option("-H", "--host", help="Set the host address to listen on.")
@click.option(
"-p",
"--port",
type=click.IntRange(1, 65535, clamp=True),
help="Set the port value."
)
@click.option(
"-l",
"--loglevel",
"loglevel",
type=click.Choice(list(uvicorn.config.LOG_LEVELS.keys()), case_sensitive=False),
help="Set the log level.",
default="info",
)
@click.version_option(version=__version__, prog_name="Duffy")
def main(reload, host, port, loglevel):
"""
Duffy is the middle layer running ci.centos.org that manages the
provisioning, maintenance and teardown / rebuild of the Nodes
(physical hardware for now, VMs coming soon) that are used to run
the tests in the CI Cluster.
"""
# Report for duty
print(" * Starting Duffy...")
print(f" * Host address : {host}")
print(f" * Port number : {port}")
print(f" * Log level : {loglevel}")
# Convert loglevel string back to int value
logname = loglevel
loglevel = uvicorn.config.LOG_LEVELS[loglevel.lower()]
uvicorn_log_config = config.get("logging", uvicorn.config.LOGGING_CONFIG).copy()
if uvicorn_log_config.get("loggers", {}).get("duffy"):
uvicorn_log_config["loggers"]["duffy"]["level"] = loglevel
import pprint as ppr
ppr.pprint(uvicorn_log_config)
logging.basicConfig(level=uvicorn.config.LOG_LEVELS[logname])
logging.debug("Hello")
log.debug("Hello")
# Start the show
uvicorn.run(
"duffy.app.main:app",
host=host,
port=port,
log_level=loglevel,
reload=reload,
log_config=uvicorn_log_config,
)
(venv) [t0xic0der@fedorable duffy]$ duffy -l debug
 * Starting Duffy...
 * Host address : 127.0.0.1
 * Port number  : 8080
 * Log level    : debug
{'disable_existing_loggers': False,
 'formatters': {'access': {'()': 'uvicorn.logging.AccessFormatter',
                           'fmt': '%(levelprefix)s %(client_addr)s - '
                                  '"%(request_line)s" %(status_code)s'},
                'default': {'()': 'uvicorn.logging.DefaultFormatter',
                            'fmt': '%(levelprefix)s %(message)s',
                            'use_colors': None}},
 'handlers': {'access': {'class': 'logging.StreamHandler',
                         'formatter': 'access',
                         'stream': 'ext://sys.stdout'},
              'default': {'class': 'logging.StreamHandler',
                          'formatter': 'default',
                          'stream': 'ext://sys.stderr'}},
 'loggers': {'uvicorn': {'handlers': ['default'], 'level': 'INFO'},
             'uvicorn.access': {'handlers': ['access'],
                                'level': 'INFO',
                                'propagate': False},
             'uvicorn.error': {'level': 'INFO'}},
 'version': 1}
DEBUG:root:Hello
DEBUG:duffy.app.cli:Hello
DEBUG:asyncio:Using selector: EpollSelector
INFO:     Started server process [113097]
INFO:uvicorn.error:Started server process [113097]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
DEBUG:duffy.app.main:node is done
INFO:duffy.app.main:this is an info
WARNING:duffy.app.main:this is a warning
ERROR:duffy.app.main:this is an error
CRITICAL:duffy.app.main:AAAAAAAAAAAAAAAARGH!
INFO:     127.0.0.1:45154 - "GET /Node/done HTTP/1.1" 200 OK
^CINFO:     Shutting down
INFO:uvicorn.error:Shutting down
INFO:     Waiting for application shutdown.
INFO:uvicorn.error:Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:uvicorn.error:Application shutdown complete.
INFO:     Finished server process [113097]
INFO:uvicorn.error:Finished server process [113097]
import logging
import click
import uvicorn
from ..configuration import config, read_configuration
from ..version import __version__
DEFAULT_CONFIG_FILE = "/etc/duffy.yaml"
log = logging.getLogger(__name__)
def init_config(ctx, param, filename):
try:
read_configuration(filename)
except FileNotFoundError:
read_configuration()
ctx.default_map = config
@click.command(name="duffy")
@click.option(
"-c",
"--config",
type=click.Path(dir_okay=False),
default=DEFAULT_CONFIG_FILE,
callback=init_config,
is_eager=True,
expose_value=False,
help="Read option defaults from the specified YAML file.",
show_default=True,
)
@click.option(
"--reload/--no-reload", default=False, help="Automatically reload if the code is changed."
)
@click.option("-H", "--host", help="Set the host address to listen on.")
@click.option(
"-p",
"--port",
type=click.IntRange(1, 65535, clamp=True),
help="Set the port value."
)
@click.option(
"-l",
"--loglevel",
"loglevel",
type=click.Choice(list(uvicorn.config.LOG_LEVELS.keys()), case_sensitive=False),
help="Set the log level.",
default="info",
)
@click.version_option(version=__version__, prog_name="Duffy")
def main(reload, host, port, loglevel):
"""
Duffy is the middle layer running ci.centos.org that manages the
provisioning, maintenance and teardown / rebuild of the Nodes
(physical hardware for now, VMs coming soon) that are used to run
the tests in the CI Cluster.
"""
# Report for duty
print(" * Starting Duffy...")
print(f" * Host address : {host}")
print(f" * Port number : {port}")
print(f" * Log level : {loglevel}")
# Convert loglevel string back to int value
loglevel = uvicorn.config.LOG_LEVELS[loglevel.lower()]
uvicorn_log_config = config.get("logging", uvicorn.config.LOGGING_CONFIG).copy()
if uvicorn_log_config.get("loggers", {}).get("duffy"):
uvicorn_log_config["loggers"]["duffy"]["level"] = loglevel
import pprint as ppr
ppr.pprint(uvicorn_log_config)
logging.basicConfig(level=logging.DEBUG)
logging.debug("Hello")
log.debug("Hello")
# Start the show
uvicorn.run(
"duffy.app.main:app",
host=host,
port=port,
log_level=loglevel,
reload=reload,
log_config=uvicorn_log_config,
)
(venv) [t0xic0der@fedorable duffy]$ duffy -l debug
 * Starting Duffy...
 * Host address : 127.0.0.1
 * Port number  : 8080
 * Log level    : debug
{'disable_existing_loggers': False,
 'formatters': {'access': {'()': 'uvicorn.logging.AccessFormatter',
                           'fmt': '%(levelprefix)s %(client_addr)s - '
                                  '"%(request_line)s" %(status_code)s'},
                'default': {'()': 'uvicorn.logging.DefaultFormatter',
                            'fmt': '%(levelprefix)s %(message)s',
                            'use_colors': None}},
 'handlers': {'access': {'class': 'logging.StreamHandler',
                         'formatter': 'access',
                         'stream': 'ext://sys.stdout'},
              'default': {'class': 'logging.StreamHandler',
                          'formatter': 'default',
                          'stream': 'ext://sys.stderr'}},
 'loggers': {'uvicorn': {'handlers': ['default'], 'level': 'INFO'},
             'uvicorn.access': {'handlers': ['access'],
                                'level': 'INFO',
                                'propagate': False},
             'uvicorn.error': {'level': 'INFO'}},
 'version': 1}
DEBUG:root:Hello
DEBUG:duffy.app.cli:Hello
DEBUG:asyncio:Using selector: EpollSelector
INFO:     Started server process [112252]
INFO:uvicorn.error:Started server process [112252]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to quit)
DEBUG:duffy.app.main:node is done
INFO:duffy.app.main:this is an info
WARNING:duffy.app.main:this is a warning
ERROR:duffy.app.main:this is an error
CRITICAL:duffy.app.main:AAAAAAAAAAAAAAAARGH!
INFO:     127.0.0.1:45146 - "GET /Node/done HTTP/1.1" 200 OK
^CINFO:     Shutting down
INFO:uvicorn.error:Shutting down
INFO:     Waiting for application shutdown.
INFO:uvicorn.error:Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:uvicorn.error:Application shutdown complete.
INFO:     Finished server process [112252]
INFO:uvicorn.error:Finished server process [112252]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment