Skip to content

Commit 9ed1cc6

Browse files
added basic logging and updated environment variables
- added github actions testing - updated enviroment variables in settings - added tests for logging files - updated setting to check for testing
1 parent 835f688 commit 9ed1cc6

File tree

11 files changed

+171
-4
lines changed

11 files changed

+171
-4
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
2+
DEBUG=True
3+
DB_HOST=db
4+
DB_PORT=5432
5+
DB_NAME=term_db
6+
DB_USER=sadilar
7+
DB_PASSWORD=sadilar
8+
LOGGING_FILE=debug.log
9+
LOGGING_HANDLERS_LEVEL=INFO
10+
LOGGING_LOGGERS_LEVEL=INFO
11+
LOGGING_LOGGERS_DJANGO_LEVEL=INFO

.env.testing

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
2+
DEBUG=True
3+
DB_HOST=db
4+
DB_PORT=5432
5+
DB_NAME=term_db
6+
DB_USER=sadilar
7+
DB_PASSWORD=sadilar
8+
LOGGING_FILE=debug.log
9+
LOGGING_HANDLERS_LEVEL=INFO
10+
LOGGING_LOGGERS_LEVEL=INFO
11+
LOGGING_LOGGERS_DJANGO_LEVEL=INFO

.github/workflows/testing.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Testing Django
2+
on: [ pull_request, push ] # activates the workflow when there is a push or pull request in the repo
3+
jobs:
4+
test_project:
5+
runs-on: ubuntu-latest # operating system your code will run on
6+
steps:
7+
- uses: actions/checkout@v2
8+
- uses: actions/setup-python@v2
9+
- name: Install Dependencies
10+
run: |
11+
python -m pip install --upgrade pip
12+
pip install -r requirements-test.txt
13+
- name: Run linting tools
14+
run: |
15+
cd app/
16+
ruff format .
17+
- name: Create logging folder
18+
run: |
19+
sudo mkdir -p /logging
20+
sudo chown runner:runner /logging
21+
- name: Run Tests
22+
run: |
23+
cp .env.testing app/.env
24+
cd app/
25+
mkdir -p static_files
26+
python manage.py test

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ db.sqlite3
1616
db.sqlite3-journal
1717
media
1818

19-
# macOS template
20-
# General
19+
# General Files
2120
.DS_Store
2221
.AppleDouble
2322
.LSOverride
@@ -30,6 +29,10 @@ venv/
3029
ENV/
3130
env.bak/
3231
venv.bak/
32+
33+
#folders
3334
app/static_files/
3435
/app/documents/
3536
app/media/
37+
/app/logging/
38+
/logging/

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@ dev-quick-install:
112112
@make load-fixtures
113113
echo "Creating superuser"
114114
@make create-super-user
115+
116+
shell:
117+
clear
118+
docker exec -it sadilar-terminology-web bash

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@ About the project:
2424
3. Run `make run` to run the docker container
2525
4. Run `make stop` to stop the docker container
2626

27+
## Production
2728

2829
### Plugins installed
30+
2931
#### Django Simple History
3032

3133
https://django-simple-history.readthedocs.io/en/latest/
34+
35+
#### Basic setup for production
36+
37+
### environment variables
38+
39+
please use .env.example as example
40+
41+
42+
## Production
43+
44+
Docker Volumes for production:
45+
46+
/media
47+
/logs

app/app/settings.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
"""
1212

1313
import os
14+
import sys
1415
from pathlib import Path
1516

17+
import environ
18+
1619
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1720
BASE_DIR = Path(__file__).resolve().parent.parent
1821

22+
# Take environment variables from .env file
23+
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))
24+
1925
# Quick-start development settings - unsuitable for production
2026
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
2127

@@ -98,6 +104,9 @@
98104
}
99105
}
100106

107+
if "test" in sys.argv or "test_coverage" in sys.argv: # Covers regular testing and django-coverage
108+
DATABASES["default"]["ENGINE"] = "django.db.backends.sqlite3"
109+
101110
# Password validation
102111
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
103112

@@ -154,3 +163,61 @@
154163
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
155164

156165
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
166+
167+
LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/"))
168+
169+
# Check if the application is under testing
170+
if "test" in sys.argv:
171+
DEBUG = False
172+
else:
173+
DEBUG = True
174+
175+
if DEBUG:
176+
LOGGING = {
177+
"version": 1,
178+
"disable_existing_loggers": False,
179+
"handlers": {
180+
"console": {
181+
"class": "logging.StreamHandler",
182+
},
183+
},
184+
"root": {
185+
"handlers": ["console"],
186+
"level": "DEBUG",
187+
},
188+
}
189+
else:
190+
LOGGING = {
191+
"version": 1,
192+
"disable_existing_loggers": False,
193+
"handlers": {
194+
"console": {
195+
"class": "logging.StreamHandler",
196+
},
197+
"file": {
198+
"level": os.environ.get("LOGGING_HANDLERS_LEVEL", "WARNING"),
199+
"class": "logging.FileHandler",
200+
"filename": os.path.join(
201+
LOGGING_FOLDER_DEFAULT, os.environ.get("LOGGING_FILE", "debug.log")
202+
),
203+
"formatter": "verbose",
204+
},
205+
},
206+
"root": {
207+
"handlers": ["console", "file"],
208+
"level": os.environ.get("LOGGING_LOGGERS_LEVEL", "WARNING"),
209+
},
210+
"loggers": {
211+
"django": {
212+
"handlers": ["file"],
213+
"level": os.environ.get("LOGGING_LOGGERS_DJANGO_LEVEL", "WARNING"),
214+
"propagate": True,
215+
},
216+
},
217+
"formatters": {
218+
"verbose": {
219+
"format": "{asctime} {levelname} - {name} {module}.py (line: {lineno:d}). - {message}",
220+
"style": "{",
221+
},
222+
},
223+
}

app/general/tests/test_logging.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import logging
2+
import os
3+
import sys
4+
5+
from django.test import TestCase
6+
7+
8+
class LoggingTest(TestCase):
9+
def setUp(self):
10+
LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/"))
11+
self.logger = logging.getLogger("django")
12+
self.log_file = os.path.join(LOGGING_FOLDER_DEFAULT, "debug.log")
13+
14+
def test_log_file_created(self):
15+
"""Test if the log file is created."""
16+
self.logger.error("This is a test error message.")
17+
18+
self.assertTrue(os.path.exists(self.log_file))
19+
20+
def test_log_message(self):
21+
"""Test if the log message is written to the file."""
22+
with open(self.log_file, "r") as f:
23+
content = f.read()
24+
self.assertIn("This is a test error message.", content)

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
command: python manage.py runserver 0.0.0.0:8000
2323
volumes:
2424
- ./app:/app
25+
- ./logging:/logging
2526
ports:
2627
- "8000:8000"
2728
depends_on:

requirements-test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-r requirements.txt
2+
django-extensions
3+
ruff

0 commit comments

Comments
 (0)