Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: Use dictconfig for more configurable logging. #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ A typical ``settings.toml`` file looks like this::
-----END PUBLIC KEY-----'''


Logging
-------

Logging can be deeply customized via the configuration file as it uses
`dictConfig
<https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig>`_
from the `logging` section, by default it is similar to::

[logging]
version = 1
disable_existing_loggers = false

[logging.formatters.full]
format = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"

[logging.handlers.stderr]
class = "logging.StreamHandler"
stream = "ext://sys.stderr"
level = "DEBUG"
formatter = "full"

[logging.loggers.kisee]
level = "DEBUG"
handlers = ["stderr"]


Sentry
------

Expand Down
44 changes: 25 additions & 19 deletions kisee/kisee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""

import argparse
import logging
import logging.config
import os
import sys
from typing import Any, Mapping, Optional
Expand Down Expand Up @@ -36,19 +36,33 @@

logger = logging.getLogger(__name__)


def setup_logging(loglevel): # pragma: no cover
DEFAULT_LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"full": {
"format": "[%(asctime)s] %(levelname)s:%(name)s:%(message)s",
}
},
"handlers": {
"stderr": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
"level": "DEBUG",
"formatter": "full",
}
},
"loggers": {"kisee": {"level": "DEBUG", "handlers": ["stderr"]}},
}


def setup_logging(settings: dict): # pragma: no cover
"""Setup basic logging
Args:
loglevel (int): minimum loglevel for emitting messages
"""
logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
logging.basicConfig(
level=50 - (loglevel * 10),
stream=sys.stdout,
format=logformat,
datefmt="%Y-%m-%d %H:%M:%S",
)
conf = settings["logging"] if "logging" in settings else DEFAULT_LOGGING_CONFIG
logging.config.dictConfig(conf)
logger.info("Kisee %s starting!", kisee.__version__)


Expand Down Expand Up @@ -77,14 +91,6 @@ def parse_args(program_args=None) -> argparse.Namespace:
program_args = sys.argv[1:]
parser = argparse.ArgumentParser(description="Shape Identity Provider")
parser.add_argument("--settings", default="settings.toml")
parser.add_argument(
"-v",
"--verbose",
dest="loglevel",
default=0,
help="Verbose mode (-vv for more, -vvv, …)",
action="count",
)
return parser.parse_args(program_args)


Expand Down Expand Up @@ -146,8 +152,8 @@ async def on_cleanup_wrapper(app):
def main() -> None: # pragma: no cover
"""Command line entry point."""
args = parse_args()
setup_logging(args.loglevel)
settings = load_conf(args.settings)
setup_logging(settings)
if sentry_sdk:
sentry_sdk.init( # pylint: disable=abstract-class-instantiated
# see https://github.com/getsentry/sentry-python/issues/1081
Expand Down