Skip to content

Commit d61432f

Browse files
Merge pull request #12 from aleyipsoftwire/exercise-13
Exercise 13
2 parents 3e24096 + 5482bba commit d61432f

File tree

7 files changed

+438
-283
lines changed

7 files changed

+438
-283
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
FLASK_APP=todo_app/app
44

55
MONGODB_PRIMARY_CONNECTION_STRING=<DELIBERATELY MISSING>
6+
LOGGLY_TOKEN=<DELIBERATELY MISSING>
7+
LOG_LEVEL=<DELIBERATELY MISSING>

.env.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
FLASK_APP=todo_app/app
44

55
MONGODB_PRIMARY_CONNECTION_STRING=mongodb://fakemongo.com
6-

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ RUN curl -sSL https://install.python-poetry.org | python3 -
1111

1212
# Install dependencies
1313
WORKDIR /app
14+
1415
COPY poetry.lock pyproject.toml /app/
15-
RUN poetry config virtualenvs.create false --local && poetry install --no-interaction --no-ansi
1616

1717
COPY todo_app /app/todo_app
1818

19+
RUN poetry config virtualenvs.create false --local && poetry install --no-interaction --no-ansi
20+
1921
EXPOSE 8000
2022

2123
FROM base AS test

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,9 @@ We are enforcing HTTPS with Azure App service.
181181

182182
Azure Cosmos DB is encrypted at rest and in transport.
183183
See https://learn.microsoft.com/en-us/azure/cosmos-db/database-encryption-at-rest for more details.
184+
185+
## Logging
186+
187+
Minimum level of shown logs can be configured by the `LOG_LEVEL` env var.
188+
189+
Populate the `LOGGLY_TOKEN` env var for logs to be sent to Loggly.

poetry.lock

Lines changed: 401 additions & 279 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ gunicorn = "^21.2.0"
1414
pymongo = "^4.8.0"
1515
mongomock = "^4.2.0.post1"
1616
safety = "^3.2.9"
17-
18-
[tool.poetry.dev-dependencies]
17+
loggly-python-handler = "^1.0.1"
1918

2019
[build-system]
2120
requires = ["poetry>=0.12"]

todo_app/app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import os
2+
from logging import Formatter
3+
14
from flask import Flask, redirect, render_template, request
5+
from loggly.handlers import HTTPSHandler
26

37
from todo_app.data.models import ViewModel
48
from todo_app.data.mongo_items import get_mongo_items, add_mongo_item, update_mongo_item_status, ItemStatus
@@ -7,15 +11,29 @@
711
def create_app():
812
app = Flask(__name__)
913

14+
app.logger.setLevel(os.getenv("LOG_LEVEL", "ERROR"))
15+
16+
if os.getenv('LOGGLY_TOKEN') is not None:
17+
print(f'https://logs-01.loggly.com/inputs/{os.getenv("LOGGLY_TOKEN")}/tag/todo-app')
18+
handler = HTTPSHandler(f'https://logs-01.loggly.com/inputs/{os.getenv("LOGGLY_TOKEN")}/tag/todo-app')
19+
handler.setFormatter(Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s"))
20+
app.logger.addHandler(handler)
21+
1022
@app.route('/')
1123
def index():
1224
items = get_mongo_items()
25+
26+
app.logger.info(f'Found {len(items)} items')
27+
1328
item_view_model = ViewModel(items)
1429
return render_template('index.html', view_model=item_view_model)
1530

1631
@app.route('/add', methods=['POST'])
1732
def add():
1833
title = request.form.get('title')
34+
35+
app.logger.info(f'Adding new item: {title}')
36+
1937
add_mongo_item(title)
2038
return redirect('/')
2139

@@ -24,8 +42,15 @@ def update_status():
2442
item_id = request.form.get('item_id')
2543
status = request.form.get('status')
2644

45+
app.logger.info(f'Updating status of item {item_id} to {status}')
46+
2747
update_mongo_item_status(item_id, ItemStatus(status))
2848

2949
return redirect('/')
3050

51+
@app.errorhandler(Exception)
52+
def handle_exception(e):
53+
app.logger.warning(f'Exception: {e}')
54+
return e
55+
3156
return app

0 commit comments

Comments
 (0)