Created
August 8, 2025 00:09
-
-
Save laisbsc/49014eacbf39fe4bc15466d02ef75131 to your computer and use it in GitHub Desktop.
Django settings.py to showing Logfire configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Django settings for travel_diaries project. | |
| Generated using Django 5.2.4. | |
| """ | |
| import os | |
| from pathlib import Path | |
| from django.conf.global_settings import LOGGING, CSRF_TRUSTED_ORIGINS | |
| from dotenv import load_dotenv | |
| import base64 | |
| import dj_database_url | |
| import logfire | |
| # Build paths inside the project like this: BASE_DIR / 'subdir'. | |
| BASE_DIR = Path(__file__).resolve().parent.parent | |
| # Quick-start development settings - unsuitable for production | |
| # See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ | |
| # SECURITY WARNING: keep the secret key used in production secret! | |
| load_dotenv() | |
| encoded_key = os.environ.get("SECRET_KEY") | |
| if encoded_key: | |
| # SECURITY WARNING: don't run with debug turned on in production! | |
| SECRET_KEY = base64.b64decode(encoded_key).decode('utf-8') | |
| else: | |
| raise Exception("SECRET_KEY environment variable not set. Please set it in your .env file.") | |
| DEBUG = True | |
| ALLOWED_HOSTS = [ | |
| 'localhost', '127.0.0.1', '[::1]', 'django-travel-diary-313641113dc1.herokuapp.com' | |
| ] | |
| # Application definition | |
| INSTALLED_APPS = [ | |
| 'django.contrib.admin', | |
| 'django.contrib.auth', | |
| 'django.contrib.contenttypes', | |
| 'django.contrib.sessions', | |
| 'django.contrib.messages', | |
| 'django.contrib.staticfiles', | |
| 'blog', | |
| 'map', | |
| ] | |
| MIDDLEWARE = [ | |
| 'django.middleware.security.SecurityMiddleware', | |
| 'django.contrib.sessions.middleware.SessionMiddleware', | |
| 'django.middleware.common.CommonMiddleware', | |
| 'django.middleware.csrf.CsrfViewMiddleware', | |
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
| 'django.contrib.messages.middleware.MessageMiddleware', | |
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
| 'travel_diaries.middleware.PageLoadTimeMiddleware', # Custom middleware for metric showing page load time | |
| ] | |
| CSRF_TRUSTED_ORIGINS = [ | |
| 'https://django-travel-diary-313641113dc1.herokuapp.com', | |
| ] | |
| ROOT_URLCONF = 'travel_diaries.urls' | |
| TEMPLATES = [ | |
| { | |
| 'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
| 'DIRS': [], | |
| 'APP_DIRS': True, | |
| 'OPTIONS': { | |
| 'context_processors': [ | |
| 'django.template.context_processors.debug', | |
| 'django.template.context_processors.request', | |
| 'django.contrib.auth.context_processors.auth', | |
| 'django.contrib.messages.context_processors.messages', | |
| ], | |
| }, | |
| }, | |
| ] | |
| WSGI_APPLICATION = 'travel_diaries.wsgi.application' | |
| # Database | |
| if 'DATABASE_URL' in os.environ: | |
| # Heroku: Use PostgreSQL | |
| DATABASES = { | |
| 'default': dj_database_url.config(conn_max_age=600) | |
| } | |
| else: | |
| # Local: Use SQLite | |
| DATABASES = { | |
| 'default': { | |
| 'ENGINE': 'django.db.backends.sqlite3', | |
| 'NAME': BASE_DIR / 'db.sqlite3', | |
| } | |
| } | |
| # Password validation | |
| AUTH_PASSWORD_VALIDATORS = [ | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | |
| }, | |
| ] | |
| # Internationalization | |
| # https://docs.djangoproject.com/en/5.2/topics/i18n/ | |
| LANGUAGE_CODE = 'en-uk' | |
| TIME_ZONE = 'Europe/Dublin' | |
| USE_I18N = True | |
| USE_TZ = True | |
| # Static files (CSS, JavaScript, Images) | |
| # https://docs.djangoproject.com/en/5.2/howto/static-files/ | |
| STATIC_URL = 'static/' | |
| STATIC_ROOT = BASE_DIR / 'static' | |
| # Default primary key field type | |
| # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field | |
| DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | |
| # Logging configuration | |
| LOGGING = { | |
| 'version': 1, | |
| 'disable_existing_loggers': False, | |
| 'handlers': { | |
| 'logfire': { | |
| 'class': 'logfire.LogfireLoggingHandler', | |
| }, | |
| }, | |
| 'root': { | |
| 'handlers': ['logfire'], | |
| }, | |
| } | |
| # Configure Django App for Heroku | |
| import django_on_heroku | |
| django_on_heroku.settings(locals()) | |
| # Logfire configuration | |
| # Ensure you have the LOGFIRE_TOKEN environment variable set in your .env file | |
| api_key = os.environ.get("LOGFIRE_TOKEN") | |
| if api_key: | |
| # the following lines should be at the end of the settings.py file | |
| logfire.configure(token=api_key, service_name='django-travel-diary') | |
| # # Instrument system metrics | |
| logfire.instrument_system_metrics({ | |
| 'process.cpu.utilization': None, | |
| 'system.cpu.simple_utilization': None, | |
| 'system.memory.utilization': ['available'], | |
| 'system.swap.utilization': ['used'], | |
| }) | |
| # Instrument Django | |
| logfire.instrument_django() | |
| # Instrument the psycopg library for PostgreSQL | |
| logfire.instrument_psycopg() | |
| print("Logfire instrumentation enabled for Django, system metrics, and PostgreSQL.") | |
| else: | |
| print("LOGFIRE_API_KEY environment variable not set. Logfire is not enabled.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment