Saturday, October 7, 2017

Django collectstatic command with settings flag returning incorrect file path for logging, throwing error.

I've recently started restructuring a project to use two scoops style configuration, with a `config` package, with `settings` inside it.  Within that package, there's _base.py, development.py, staging.py and production.py.  Since this is deployed as a WSGI app with Apache, I'm using a json file for environment variables, such as AWS credentials, db username/passwords and such.  In short, following suggestions listed at https://medium.com/@ayarshabeer/django-best-practice-settings-file-for-multiple-environments-6d71c6966ee2

When deploying on staging, I have the wsgi app running through apache.  In addition, manage.py $COMMAND is working with the `--settings=config.settings.staging` flag set.  That is, with the exception of collectstatic.

(staging)ubuntu@farmer-stage:~/public_html/django/project$ ./manage.py collectstatic --help
Using configuration [config.settings.staging]
Using configuration [config.settings.staging]
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/.virtualenvs/staging/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/.virtualenvs/staging/lib/python2.7/site-packages/django/core/management/__init__.py", line 328, in execute
    django.setup()
  File "/home/ubuntu/.virtualenvs/staging/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/ubuntu/.virtualenvs/staging/lib/python2.7/site-packages/django/utils/log.py", line 86, in configure_logging
    logging_config_func(logging_settings)
  File "/usr/local/lib/python2.7.11/lib/python2.7/logging/config.py", line 794, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/local/lib/python2.7.11/lib/python2.7/logging/config.py", line 576, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler u'file': [Errno 2] No such file or directory: u'/home/ubuntu/public_html/django/logs/django_project.log'

In _base, I have the following defined:

environ_path = os.path.join(BASE_DIR, '../.env.json')
with open(environ_path) as f:
secrets = json.loads(f.read())


def get_secret(secret):
try:
return secrets[secret]
except KeyError:
raise ImproperlyConfigured('Required variable [{}] not configured'.format(secret))


def set_settings_module(module):
if module in ['config.settings.development', 'config.settings.staging', 'config.settings.production']:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', module)
print "Using configuration [{}]".format(module)
else:
raise ImproperlyConfigured('Wrong settings module for configuration')


set_settings_module(get_secret('django_settings_module'))

LOGGING_DEFAULTS = {
'version': 1,
'disable_existing_loggers': 'False',
'handlers': {
'file': {
'level': 'INFO',
'filename': None,
'class': 'logging.FileHandler',
'formatter': 'verbose'
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'loggers': {
'django': {
'level': 'INFO',
'handlers': ['file', 'console'],
'propagate': True
}
}
}


and in staging.py, I override the filename for the env specific path via

LOGGING_PATH = os.path.join(BASE_DIR, '../../../logs/django/django_project.log')

When running via apache wsgi or manage.py shell_plus, it works as expected.  However, when I attempt to collect the staticfiles, I get the error output at the start of this report.  But this doesn't happen in my vagrant dev environment.

I'm running python 2.7 with django 1.8.16


Any help is appreciated!

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/27849a10-c364-452b-a67f-2e2d8aae7471%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment