Skip to content

Commit

Permalink
Logging using after_request method & hydrus_logger added (#575)
Browse files Browse the repository at this point in the history
* after_request method added.
* hydrus_logger & LogConfig file added.
* structlog dependency added.
* iri & status code added.
* reposition logger object.
* Log level set to python env variable
* Console deubug configured
* "Object_created_at : None" Fixed
* level fix
  • Loading branch information
Purvanshsingh authored May 21, 2021
1 parent 35c3ea7 commit cbcce67
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
28 changes: 28 additions & 0 deletions hydrus/LogConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[loggers]
keys=root

[handlers]
keys = FileHandler,StreamHandler

[formatters]
keys=simpleFormatter, ConsoleFormatter

[logger_root]
handlers=FileHandler, StreamHandler

[handler_FileHandler]
class=FileHandler
args=('hydrus.log',)

[handler_StreamHandler]
class = StreamHandler

formatter=ConsoleFormatter
args=(sys.stdout,)

[formatter_ConsoleFormatter]
format=

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
17 changes: 15 additions & 2 deletions hydrus/app_factory.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from flask import Flask, redirect
from flask import Flask, redirect, request
import uuid
from flask_cors import CORS
from flask_restful import Api

from hydrus_logger import HydrusLogger
from hydrus.resources import (Index, Vocab, Contexts, Entrypoint,
ItemCollection, Item, Items, ItemMember)

logger = HydrusLogger().get_logger()


def app_factory(api_name: str = "api", vocab_route: str = "vocab") -> Flask:
"""
Expand All @@ -20,6 +23,16 @@ def app_factory(api_name: str = "api", vocab_route: str = "vocab") -> Flask:
app.url_map.strict_slashes = False
api = Api(app)

@app.after_request
def logging_request_response(response):
if response.location and response.status_code != 302:
logger.info(" {} {}:{} Object_created_at : {}, status_code : {} "
.format(uuid.uuid4(), request.method, request.path, response.location, response.status))
else:
logger.info(" {} {}:{} status_code : {} "
.format(uuid.uuid4(), request.method, request.path, response.status))
return response

# Redirecting root_path to root_path/api_name
if api_name:
@app.route("/")
Expand Down
15 changes: 15 additions & 0 deletions hydrus/hydrus_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
import logging.config
import structlog
import os

class HydrusLogger:
def __init__(self):
logging.basicConfig(level = os.environ.get('LOGLEVEL', 'DEBUG'))
logging.config.fileConfig('LogConfig')
structlog.wrap_logger(logging.getLogger())
structlog.configure(logger_factory=structlog.stdlib.LoggerFactory())
self.logger = structlog.get_logger()

def get_logger(self):
return self.logger
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ six==1.13.0
SQLAlchemy==1.3.12
thespian==3.9.11
Werkzeug==0.16.0
structlog

0 comments on commit cbcce67

Please sign in to comment.