Skip to content

Commit 49eddd3

Browse files
Added: Logger
1 parent 06c0b2b commit 49eddd3

File tree

15 files changed

+265
-56
lines changed

15 files changed

+265
-56
lines changed

src/server/Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.PHONY:
2+
clean system-packages python-packages install tests run all
3+
4+
clean:
5+
find . -type f -name '*.pyc' -delete
6+
find . -type d -name '*pycache*' -delete
7+
find . -type f -name '*.log' -delete
8+
9+
system-packages:
10+
sudo apt install python-pip -y
11+
12+
python-packages:
13+
pip install -r requirements.txt
14+
15+
install:
16+
system-packages python-packages
17+
18+
tests:
19+
python manage.py test
20+
21+
run:
22+
python manage.py run
23+
24+
all:
25+
clean install tests run
26+
27+
dbinit:
28+
python manage.py db init
29+
30+
migrate:
31+
python manage.py db migrate
32+
python manage.py db upgrade

src/server/app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""" The folder to include all the unnittests according to requirement """

src/server/app/main/__init__.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
1-
from flask import Flask
1+
""" Creation of Flask object along with configuration based on environment name specified in the settings file"""
2+
import logging
3+
from datetime import datetime as dt
4+
from flask import Flask, request
25
from flask_sqlalchemy import SQLAlchemy
36
from flask_bcrypt import Bcrypt
47

58
from .settings import config_by_name
9+
from app.main.utils.LogSetup import LogSetup
610

11+
logs = LogSetup()
712
db = SQLAlchemy()
813
flask_bcrypt = Bcrypt()
914

1015

1116
def create_app(config_name):
17+
"""
18+
creates the Flask object
19+
20+
Args:
21+
config_name (str): string to define the environment settings the Flask object will be configured with
22+
23+
Returns:
24+
object: Flask object configured with the configuration parameters from settings based on the environment name specified
25+
"""
1226
app = Flask(__name__)
1327
app.config.from_object(config_by_name[config_name])
1428
db.init_app(app)
29+
logs.init_app(app)
1530
flask_bcrypt.init_app(app)
1631

32+
33+
@app.after_request
34+
def after_request(response):
35+
""" Logging after every request. """
36+
logger = logging.getLogger("app.access")
37+
logger.info(
38+
"%s [%s] %s %s %s %s %s %s %s",
39+
request.remote_addr,
40+
dt.utcnow().strftime("%d/%b/%Y:%H:%M:%S.%f")[:-3],
41+
request.method,
42+
request.path,
43+
request.scheme,
44+
response.status,
45+
response.content_length,
46+
request.referrer,
47+
request.user_agent,
48+
)
49+
return response
50+
51+
1752
return app
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
from app.main.models.todo import Todo
2-
from app.main.models.deleted_todos import RemovedTodo
1+
""" import all the models in the folder here"""

src/server/app/main/models/deleted_todos.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/server/app/main/models/todo.py

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
from .blueprint_todo import todo
1+
# import resprctive blueprints and flask REstful resources
2+
from .blueprint_test import bp
23

4+
def add_resources(app):
5+
"""
6+
Method to add resources to app context
7+
8+
Args:
9+
app (object): object of Flask representing the app in context
10+
"""
11+
pass
312

413
def register_blueprints(app):
5-
app.register_blueprint(todo, url_prefix="/todo")
14+
"""
15+
Method to add blueprints to app context
16+
17+
Args:
18+
app (object): object of Flask representing the app in context
19+
"""
20+
app.register_blueprint(bp)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Blueprint, current_app
2+
3+
bp = Blueprint("public", __name__)
4+
5+
@bp.route("/")
6+
def hello_world():
7+
current_app.logger.info("Here I am")
8+
current_app.logger.warning(" Warning you")
9+
current_app.logger.error("But you can't hear me")
10+
11+
return "hello hello_world"

src/server/app/main/routes/blueprint_todo.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""" Add the services in the folder and import them based on requirement """

0 commit comments

Comments
 (0)