Skip to content

Commit

Permalink
Remove the singular JsonLogger in favor of the uw-it-flask-gunicorn-j…
Browse files Browse the repository at this point in the history
…son-logger package (#136)

* Remove the singular JsonLogger component and use the new, shared one instead. :)

Please note that the `@logged_timer` function in the new package does not yet have features that are implemented in the current `@timed` decorator in this package, so I haven't stripped that out yet. (i.e., we're still using the existing timers.)

* [Bot] Update version to 2.1.10

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Thomas Thorogood and github-actions[bot] authored Jun 24, 2022
1 parent a2c00aa commit 62f2ad0
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 266 deletions.
21 changes: 2 additions & 19 deletions husky_directory/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging
import os
from datetime import datetime
from logging.config import dictConfig
from typing import List, NoReturn, Optional, Type, cast

import inflection
Expand All @@ -13,6 +11,7 @@
from injector import Injector, Module, provider, singleton
from jinja2.tests import test_undefined
from redis import Redis
from uw_it.flask_util.logger import FlaskJSONLogger
from uw_saml2 import mock, python3_saml
from werkzeug.exceptions import HTTPException, InternalServerError
from werkzeug.local import LocalProxy
Expand All @@ -29,7 +28,6 @@
from .app_config import (
ApplicationConfig,
ApplicationConfigInjectorModule,
YAMLSettingsLoader,
)


Expand Down Expand Up @@ -64,18 +62,6 @@ def provide_request_session(self) -> LocalProxy:
LocalProxy, flask_session
) # Cast this so that IDEs knows what's up; does not affect runtime

@provider
@singleton
def provide_logger(
self, yaml_loader: YAMLSettingsLoader, injector: Injector
) -> logging.Logger:
logger_settings = yaml_loader.load_settings("logging")
dictConfig(logger_settings)
app_logger = logging.getLogger("gunicorn.error").getChild("app")
formatter = app_logger.handlers[0].formatter
formatter.injector = injector
return app_logger

def register_jinja_extensions(self, app: Flask):
"""You can define jinja filters here in order to make them available in our jinja templates."""

Expand Down Expand Up @@ -141,7 +127,6 @@ def provide_app(
self,
injector: Injector,
app_settings: ApplicationConfig,
logger: logging.Logger,
# Any blueprints that are depended on here must
# get registered below, under "App blueprints get registered here."
search_blueprint: SearchBlueprint,
Expand Down Expand Up @@ -171,12 +156,10 @@ def provide_app(
app.register_blueprint(search_blueprint)
app.register_blueprint(saml_blueprint)

# Ensure the application is using the same logger as everything else.
app.logger = logger

# Bind an injector to the app itself to manage the scopes of
# our dependencies appropriate for each request.
FlaskInjector(app=app, injector=injector)
FlaskJSONLogger(app)
self._configure_app_session(app, app_settings)
self._configure_prometheus(app, app_settings, injector)
attach_app_error_handlers(app)
Expand Down
10 changes: 3 additions & 7 deletions husky_directory/blueprints/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from logging import Logger
from typing import Optional

from flask import Blueprint, Request, jsonify
Expand All @@ -8,7 +7,7 @@

from husky_directory.app_config import ApplicationConfig
from husky_directory.services.pws import PersonWebServiceClient
from husky_directory.util import camelize
from husky_directory.util import AppLoggerMixIn, camelize


class HealthReport(BaseModel):
Expand All @@ -24,20 +23,17 @@ class Config:
deployment_id: Optional[str]


class AppBlueprint(Blueprint):
class AppBlueprint(Blueprint, AppLoggerMixIn):
"""Blueprint for root urls within the application."""

@inject
def __init__(
self, app_config: ApplicationConfig, logger: Logger, injector: Injector
):
def __init__(self, app_config: ApplicationConfig, injector: Injector):
super().__init__("uw-directory", __name__)
self.add_url_rule("/status", view_func=self.health)
self.config = app_config
self.start_time = app_config.start_time
self._app_config = app_config
self._injector = injector
self.logger = logger
self.__pws_result = None

@property
Expand Down
9 changes: 5 additions & 4 deletions husky_directory/blueprints/saml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import getpass
import urllib.parse
from logging import Logger
from typing import Dict

import uw_saml2
Expand All @@ -11,19 +10,21 @@
from werkzeug.local import LocalProxy

from husky_directory.app_config import ApplicationConfig
from husky_directory.util import AppLoggerMixIn


class SAMLBlueprint(Blueprint):
class SAMLBlueprint(Blueprint, AppLoggerMixIn):
@inject
def __init__(
self, idp_config: IdpConfig, settings: ApplicationConfig, logger: Logger
self,
idp_config: IdpConfig,
settings: ApplicationConfig,
):
super().__init__("saml", __name__, url_prefix="/saml")
self.idp_config = idp_config
self.add_url_rule("/login", view_func=self.login, methods=["GET", "POST"])
self.add_url_rule("/logout", view_func=self.log_out)
self.auth_settings = settings.auth_settings
self.logger = logger

def process_saml_request(self, request: Request, session: LocalProxy, **kwargs):
dest_url = request.form.get("RelayState") or request.host_url
Expand Down
17 changes: 7 additions & 10 deletions husky_directory/blueprints/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from base64 import b64decode
from logging import Logger
from typing import Optional, Union

from flask import (
Expand Down Expand Up @@ -27,6 +26,7 @@
)
from husky_directory.services.search import DirectorySearchService
from husky_directory.services.vcard import VCardService
from husky_directory.util import AppLoggerMixIn


class ErrorModel(DirectoryBaseModel):
Expand All @@ -46,12 +46,11 @@ class Config(DirectoryBaseModel.Config):


@singleton
class SearchBlueprint(Blueprint):
class SearchBlueprint(Blueprint, AppLoggerMixIn):
@inject
def __init__(self, logger: Logger, injector: Injector):
def __init__(self, injector: Injector):
super().__init__("search", __name__)
self.injector = injector
self.logger = logger
self.add_url_rule("/", view_func=self.index, methods=("GET",))
self.add_url_rule("/", view_func=self.search_listing, methods=("POST",))
self.add_url_rule(
Expand Down Expand Up @@ -84,10 +83,9 @@ def index(request: Request, session: LocalProxy, settings: ApplicationConfig):
200,
)

@staticmethod
def get_person_listing(
self,
request: Request,
logger: Logger,
session: LocalProxy,
service: DirectorySearchService,
):
Expand All @@ -103,7 +101,7 @@ def get_person_listing(
)
except Exception as e:
template = "views/index.html"
logger.exception(str(e))
self.logger.exception(str(e))
SearchBlueprint.handle_search_exception(e, context)
finally:
return (
Expand Down Expand Up @@ -141,11 +139,10 @@ def handle_search_exception(e: Exception, context: RenderingContext):
"email [email protected] describing your problem."
)

@staticmethod
def search_listing(
self,
request: Request,
service: DirectorySearchService,
logger: Logger,
session: LocalProxy,
settings: ApplicationConfig,
):
Expand All @@ -160,7 +157,7 @@ def search_listing(
request_input = SearchDirectoryInput.from_form_input(form_input)
context.search_result = service.search_directory(request_input)
except Exception as e:
logger.exception(str(e))
self.logger.exception(str(e))
SearchBlueprint.handle_search_exception(e, context)
finally:
response: Response = make_response(
Expand Down
120 changes: 0 additions & 120 deletions husky_directory/logging.py

This file was deleted.

7 changes: 2 additions & 5 deletions husky_directory/services/pws.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from collections import namedtuple
from functools import partial
from logging import Logger
from typing import Any, Dict, List, Optional, Type, TypeVar, Union

import requests
Expand All @@ -19,7 +18,7 @@
PersonOutput,
)
from husky_directory.services.auth import AuthService
from husky_directory.util import timed
from husky_directory.util import AppLoggerMixIn, timed

RequestsCertificate = namedtuple("RequestsCertificate", ["cert_path", "key_path"])

Expand Down Expand Up @@ -48,7 +47,7 @@ def clear_namespace(namespace, record: Any) -> bool:


@request
class PersonWebServiceClient:
class PersonWebServiceClient(AppLoggerMixIn):
_GLOBAL_CONSTRAINTS = [
RecordConstraint(
# If the identity has no netid, we are not interested
Expand Down Expand Up @@ -103,7 +102,6 @@ class PersonWebServiceClient:
def __init__(
self,
application_config: ApplicationConfig,
logger: Logger,
auth: AuthService,
):
uwca_cert_path = application_config.auth_settings.uwca_cert_path
Expand All @@ -114,7 +112,6 @@ def __init__(
)
self.host = application_config.pws_settings.pws_host
self.default_path = application_config.pws_settings.pws_default_path
self.logger = logger
self.auth = auth

@property
Expand Down
11 changes: 3 additions & 8 deletions husky_directory/services/reducer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from collections import OrderedDict
from functools import cached_property
from logging import Logger
from typing import Dict, Optional, Tuple

from injector import inject

from husky_directory.models.pws import ListPersonsOutput, NamedIdentity
from husky_directory.models.transforms import ResultBucket
from husky_directory.services.name_analyzer import NameAnalyzer
from husky_directory.util import is_similar, readable_list
from husky_directory.util import AppLoggerMixIn, is_similar, readable_list


class NameQueryResultAnalyzer:
Expand Down Expand Up @@ -84,12 +81,10 @@ def relevant_bucket(self) -> Optional[Tuple[str, int]]:
return f"Name contains {readable}", 7


class NameSearchResultReducer:
@inject
def __init__(self, logger: Logger):
class NameSearchResultReducer(AppLoggerMixIn):
def __init__(self):
self.duplicate_netids = set()
self.duplicate_hit_count = 0
self.logger = logger

def reduce_output(
self,
Expand Down
Loading

0 comments on commit 62f2ad0

Please sign in to comment.