Skip to content

Commit

Permalink
add logging abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroroiz committed Aug 27, 2024
1 parent ef1d611 commit 4f52f20
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.6.1
6.6.1-alpha-1
12 changes: 4 additions & 8 deletions confidant/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import logging

import boto3
import guard
from flask import Flask
from flask_sslify import SSLify

from confidant import settings
from confidant.utils import misc
from confidant.routes import (
blind_credentials,
certificates,
Expand All @@ -17,11 +15,6 @@
jwks,
)

if not settings.get('DEBUG'):
boto3.set_stream_logger(level=logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)
logging.getLogger('pynamodb').setLevel(logging.WARNING)

CSP_POLICY = {
'default-src': ["'self'"],
'style-src': [
Expand All @@ -44,6 +37,9 @@ def create_app():

app.wsgi_app = guard.ContentSecurityPolicy(app.wsgi_app, CSP_POLICY)

init_logging_func = misc.load_module(settings.get('INIT_LOGGING_MODULE'))
init_logging_func()

if settings.REDIS_URL_FLASK_SESSIONS:
import redis
from flask_session import Session
Expand Down
30 changes: 30 additions & 0 deletions confidant/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logging
import boto3

from confidant import settings

def init_logging():
logging.getLogger(__name__).info('Initializing logging')
if not settings.get('DEBUG'):
boto3.set_stream_logger(level=logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)
logging.getLogger('pynamodb').setLevel(logging.WARNING)


def get_logger(name=__name__):
return logging.getLogger(name)


def logging_abstraction(log_level='INFO', msg='', name=__name__):
logger = get_logger(name)

if log_level == 'INFO' and msg:
logger.info(msg)
elif log_level == 'ERROR' and msg:
logger.error(msg)
elif log_level == 'DEBUG' and msg:
logger.debug(msg)
elif log_level == 'CRITICAL' and msg:
logger.critical(msg)
elif log_level == 'WARNING' and msg:
logger.warning(msg)
8 changes: 6 additions & 2 deletions confidant/routes/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
blueprint = blueprints.Blueprint('credentials', __name__)

acl_module_check = misc.load_module(settings.ACL_MODULE)
logging_module = misc.load_module(settings.get('LOGGING_MODULE'))

VALUE_LENGTH = 50


Expand Down Expand Up @@ -203,8 +205,10 @@ def get_credential(id):
try:
credential = Credential.get(id)
except DoesNotExist:
logger.warning(
'Item with id {0} does not exist.'.format(id)
logging_module(
log_level='WARNING',
msg='Item with id {0} does not exist.'.format(id),
name=__name__,
)
return jsonify({}), 404
if credential.data_type != 'credential':
Expand Down
4 changes: 4 additions & 0 deletions confidant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,7 @@ def get(name, default=None):

# Module that will perform an external ACL check on API endpoints
ACL_MODULE = str_env('ACL_MODULE', 'confidant.authnz.rbac:default_acl')

# Logging
INIT_LOGGING_MODULE = str_env('LOGGING_MODULE', 'confidant.logging:init_logging')
LOGGING_MODULE = str_env('LOGGING_MODULE', 'confidant.logging:logging_abstraction')

0 comments on commit 4f52f20

Please sign in to comment.