For django to get docker-compose.yml environment variable use the following:
https://stackoverflow.com/questions/4906977/how-can-i-access-environment-variables-in-python
# Returns `None` if the key doesn't exist
print(os.environ.get('KEY_THAT_MIGHT_EXIST'))
# Returns `default_value` if the key doesn't exist
print(os.environ.get('KEY_THAT_MIGHT_EXIST', default_value))
# Returns `default_value` if the key doesn't exist
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))------------------------------------------------------------------------------------------------------------
as stated above, django uses TIME_ZONE value in settings.py
To specify in docker-compose timezone and retrieve in django :
app:
user: "${uID}:${gID}"
build:
context: .
ports:
- "8888:8888"
volumes:
- ./app:/app
command: >
sh -c "python manage.py wait_for_db_conn &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8888"
environment:
- TZ=America/Vancouver
then in settings.py
TIME_ZONE = os.environ.get('TZ', 'UTC')
timzone : https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
-----------------------------------------------------------------------------------------
https://docs.python.org/2/library/logging.handlers.html
https://stackoverflow.com/questions/4906977/how-can-i-access-environment-variables-in-python
for python logger to create a my_app.log but the log will be moved to a new log file named my_app.log.20170623 when the current day ends at midnight use :
from logging.handlers import TimedRotatingFileHandler
logname = "my_app.log"
handler = TimedRotatingFileHandler(logname, when="midnight", backupCount=30)
handler.suffix = "%Y%m%d"
logger.addHandler(handler)To use it in settings.py :
BASE_DIR = Path(__file__).resolve().parent.parent
MY_FOLDER = os.path.join(BASE_DIR, '/app/myLog/devMode/')
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '[%(asctime)s] %(levelname)s %(message)s',
},
'verbose': {
'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'file': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': MY_FOLDER,
'when': 'midnight',
'interval': 1,
'backupCount': 365,
"encoding": "utf8",
'formatter': 'verbose'
},
},
'loggers': {
# this is speicfied in INSTALLED_APPS in settings.py and in the APPs view.py :
# import logging
# logger = logging.getLogger('myApp')
'myApp': {
'handlers': ['console', 'file'],
'level': 'INFO',
'propagate': True,
},
'django': {
'handlers': ['console', 'file'],
'level': 'ERROR',
'propagate': True,
},
'django.request': {
'handlers': ['console', 'file'],
'level': 'ERROR',
'propagate': True,
},
},
# If backupCount is nonzero, at most backupCount files will be kept, and if more would be created when rollover occurs, the oldest one is deleted. The deletion logic uses the interval to determine which files to delete, so changing the interval may leave old files lying around.
BackUpCount : 0
# means # of when value below. Here means 1 day
Interval : 1
# means o create a my_app.log but the log will be moved to a new log file named my_app.log.20170623 when the current day ends at midnight
when: 'midnight'
No comments:
Post a Comment