Skip to content

Commit bdadf62

Browse files
authored
Merge pull request #72 from RSE-Sheffield/feat/django-logging
Set up Django logging
2 parents e02daf4 + 27bd2cb commit bdadf62

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

SORT/settings.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def cast_to_boolean(obj: Any) -> bool:
181181
"127.0.0.1",
182182
# ...
183183
]
184-
AUTH_USER_MODEL = 'home.User' # FA: replace username with email as unique identifiers
184+
AUTH_USER_MODEL = 'home.User' # FA: replace username with email as unique identifiers
185185

186186
# FA: for production:
187187

@@ -194,3 +194,20 @@ def cast_to_boolean(obj: Any) -> bool:
194194
os.getenv("DJANGO_SESSION_COOKIE_SECURE", not DEBUG)
195195
)
196196
CSRF_COOKIE_SECURE = cast_to_boolean(os.getenv("DJANGO_CSRF_COOKIE_SECURE", not DEBUG))
197+
198+
# Logging
199+
# https://docs.djangoproject.com/en/5.1/topics/logging/
200+
LOGGING = {
201+
"version": 1,
202+
"disable_existing_loggers": False,
203+
# Send log messages to the standard output, which will be sent to the Gunicorn service logs
204+
"handlers": {
205+
"console": {
206+
"class": "logging.StreamHandler",
207+
},
208+
},
209+
"root": {
210+
"handlers": ["console"],
211+
"level": os.getenv("DJANGO_LOG_LEVEL", "WARNING"),
212+
},
213+
}

docs/deployment.md

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This app can be deployed to a web server using the script [`deploy.sh`](../deplo
4242
1. Configure the `.env` file as described below.
4343
2. Run the deployment script: `sudo bash -x deploy.sh`
4444
3. Configure the database
45+
4. Run database migrations
4546

4647
We can run commands and Bash scripts as the superuser (`root`) using the [`sudo` command](https://manpages.ubuntu.com/manpages/noble/en/man8/sudo.8.html).
4748

@@ -147,8 +148,6 @@ We can list users (i.e. database "roles") using [the `\du` command](https://www
147148
psql sort --command "\du"
148149
```
149150

150-
151-
152151
## Grant permissions
153152

154153
We must allow this user the minimum necessary privileges to operate the web app. We authorise the user using the PostgreSQL [grant statement](https://www.postgresql.org/docs/current/sql-grant.html), which we execute in the `psql` tool.
@@ -182,11 +181,63 @@ ALTER DEFAULT PRIVILEGES FOR USER sort GRANT SELECT, INSERT, UPDATE, DELETE ON T
182181

183182
On our PostgreSQL instance, this should create a database named `sort` with a user named `sort` that has all the necessary permissions on the `sort` schema to create, modify, and drop tables and read/write data to those tables.
184183

184+
# Database administration
185+
186+
To open the [`psql` terminal](https://www.postgresql.org/docs/16/app-psql.html), use the [`dbshell`](https://docs.djangoproject.com/en/5.1/ref/django-admin/#dbshell) command. We can pass a command using the [`--` option](https://docs.djangoproject.com/en/5.1/ref/django-admin/#cmdoption-dbshell-0). We need to set up the shell as follows:
187+
188+
```bash
189+
sort_dir="/opt/sort"
190+
venv_dir="$sort_dir/venv"
191+
python="$venv_dir/bin/python"
192+
psql="$python manage.py dbshell --"
193+
194+
cd "$sort_dir"
195+
```
196+
197+
To view the available PostgreSQL commands:
198+
199+
```bash
200+
sudo $psql -c "\?"
201+
```
202+
203+
The `psql` command invokes the Postgres shell:
204+
205+
```bash
206+
$ sudo $psql
207+
sort=>
208+
```
209+
210+
Once you're in the `psql` shell, you can exit using the `\q` command.
211+
212+
## Manage tables
213+
214+
### Run schema migrations
215+
216+
Run the migrations command using [django-admin](https://docs.djangoproject.com/en/5.1/ref/django-admin/)
217+
218+
```bash
219+
sudo $python manage.py migrate
220+
```
221+
222+
### List tables
223+
224+
```bash
225+
sudo $psql -c "\dt"
226+
```
227+
228+
### Drop tables
229+
230+
You can delete tables using the [`DROP TABLE` syntax](https://www.postgresql.org/docs/current/sql-droptable.html).
231+
232+
```sql
233+
DROP TABLE IF EXISTS table_name CASCADE;
234+
```
235+
185236
# Security
186237

187238
## SSL Certificates
188239

189-
See: ITS Wiki [SSL Certificates/Howto](https://itswiki.shef.ac.uk/wiki/SSL_Certificates/Howto) for the commands to generate a Certificate Signing Request (CSR) using [OpenSSL](https://docs.openssl.org/3.3/man1/openssl-req/#options) with an unencrypted private key.
240+
See: ITS Wiki [SSL Certificates/Howto](https://itswiki.shef.ac.uk/wiki/SSL_Certificates/Howto) for the commands to generate a Certificate Signing Request (CSR) using [OpenSSL](https://docs.openssl.org/3.3/man1/openssl-req/#options) with an unencrypted private key.
190241

191242
We can install the private key
192243

@@ -218,27 +269,29 @@ To use the [Django management tool](https://docs.djangoproject.com/en/5.1/ref/dj
218269
sort_dir="/opt/sort"
219270
venv_dir="$sort_dir/venv"
220271
python="$venv_dir/bin/python"
272+
django_admin="$python $sort_dir/manage.py"
273+
221274
cd "$sort_dir"
222275
# Check the Django management tool works
223-
$python "$sort_dir"/manage.py version
276+
sudo $django_admin version
224277
```
225278

226279
View available commands
227280

228281
```bash
229-
$python "$sort_dir"/manage.py help
282+
sudo $django_admin help
230283
```
231284

232285
Migrate the database
233286

234287
```bash
235-
sudo $python manage.py migrate
288+
sudo $django_admin migrate
236289
```
237290

238291
Create a super-user
239292

240293
```bash
241-
sudo $python manage.py createsuperuser
294+
sudo $django_admin createsuperuser
242295
```
243296

244297
Load data

0 commit comments

Comments
 (0)