Skip to content

Commit

Permalink
Log level support for django social and requests. Now respecting the …
Browse files Browse the repository at this point in the history
…default log levels specified in settings.py. Related to #37
  • Loading branch information
mtrx1337 committed Feb 5, 2020
1 parent 5086754 commit b5d26ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
7 changes: 3 additions & 4 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ KUBEPORTAL_ADMIN_EMAIL The email address of the superuser.
KUBEPORTAL_EMAIL_HOST The SMTP server used by the web site for sending mails.
KUBEPORTAL_DATABASE_URL The database to be used as URL (see `formatting examples <https://github.com/jacobian/dj-database-url>`), e.g. ``sqlite:////data/kubeportal.sqlite3``.
KUBEPORTAL_REDIRECT_HOSTS Hosts that redirect to the KubePortal web page, typically to perform OAuth authenication. Example: ``grafana.example.com, registry.example.com``.
KUBEPORTAL_LOG_LEVEL Sets the verbosity of the logging.
0=critical problems,
1=warnings,
2=maximum verbosity (default)
KUBEPORTAL_PORTAL_LOG_LEVEL Sets the verbosity of the logging for the admin panel. 0 = critical problems, 1 = warnings, 2 = maximum verbosity
KUBEPORTAL_SOCIAL_LOG_LEVEL Sets the verbosity of the logging for django.social. 0 = critical problems, 1 = warnings, 2 = maximum verbosity
KUBEPORTAL_REQUEST_LOG_LEVEL Sets the verbosity of the logging for requests. 0 = critical problems, 1 = warnings, 2 = maximum verbosity
===================================== ============================================================================


Expand Down
44 changes: 25 additions & 19 deletions kubeportal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import logging, sys
from os import environ

logger = logging.getLogger('KubePortal')
def set_log_level(env_var_name, logger):
'''
Sets the log level based on environment variables for the given logger.
'''
# does the variable exist?
if environ.get(env_var_name):
log_level = int(environ[env_var_name])
if log_level == 0:
logger.setLevel(logging.CRITICAL)
elif log_level == 1:
logger.setLevel(logging.WARNING)
elif log_level == 2:
pass
else:
print("Unknown log level '{}'".format(log_level))
print("Please check your '{}' environment variable.".format(env_var_name))

handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# set log level (default to maximum verbosity)
logger.setLevel(logging.INFO)
if environ.get('KUBEPORTAL_LOG_LEVEL'):
log_level = int(environ['KUBEPORTAL_LOG_LEVEL'])
if log_level == 0:
logger.setLevel(logging.CRITICAL)
elif log_level == 1:
logger.setLevel(logging.WARNING)
elif log_level == 2:
pass
else:
print("Unknown log level '{}'".format(log_level))
print("Please check your KUBEPORTAL_LOG_LEVEL environment variable.")
set_log_level("KUBEPORTAL_REQUEST_LOG_LEVEL", logging.getLogger('django.request'))
set_log_level("KUBEPORTAL_PORTAL_LOG_LEVEL", logging.getLogger('KubePortal'))
set_log_level("KUBEPORTAL_SOCIAL_LOG_LEVEL", logging.getLogger('social'))

handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

default_app_config = 'kubeportal.apps.KubePortalConfig'

0 comments on commit b5d26ca

Please sign in to comment.