Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk logging with python (django)

How do you manage your application logs in AWS elastic beanstalk? Which file you write you application logs to?

I'm using the following Logging configuration in my development environment but this doesn't work when I deploy in AWS.

DEBUG_LOG_DIR = BASE_DIR + "/django_debug.log" LOGGING = {     'version': 1,     'disable_existing_loggers': True,     # How to format the output     'formatters': {         'standard': {             'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",             'datefmt' : "%d/%b/%Y %H:%M:%S"         },     },     # Log handlers (where to go)     'handlers': {         'null': {             'level':'DEBUG',             'class':'django.utils.log.NullHandler',         },         'log_file': {             'level':'DEBUG',             'class':'logging.handlers.RotatingFileHandler',             'filename': DEBUG_LOG_DIR,             'maxBytes': 50000,             'backupCount': 2,             'formatter': 'standard',         },         'console':{             'level':'INFO',             'class':'logging.StreamHandler',             'formatter': 'standard'         },         'mail_admins': {             'level': 'ERROR',             'class': 'django.utils.log.AdminEmailHandler',         },     },     # Loggers (where does the log come from)     'loggers': {         'repackager': {             'handlers': ['console', 'log_file'],             'level': 'DEBUG',             'propagate': True,         },         'django': {             'handlers':['console'],             'propagate': True,             'level':'WARN',         },         'django.db.backends': {             'handlers': ['console', 'log_file'],             'level': 'WARN',             'propagate': False,         },         '': {             'handlers': ['console', 'log_file'],             'level': 'DEBUG',         },     } } 
like image 793
user1126167 Avatar asked Dec 18 '14 18:12

user1126167


People also ask

How do I add logging to Django project?

First Steps With Logging Begin by updating the app/views.py file with the following code: import logging from django. http import HttpResponse # This retrieves a Python logging instance (or creates it) logger = logging. getLogger(__name__) def index(request): # Send the Test!!

Can Django be used with AWS?

Django comes with a development server that helps run Django projects in localhost. However, to make your web application available worldwide you will need a host machine. Amazon Web Services (AWS) provides EC2 (Elastic Compute Cloud) which is the most popular choice to host Django web applications.

Can you SSH into Elastic Beanstalk?

You can view a list of Amazon EC2 instances running your AWS Elastic Beanstalk application environment through the Elastic Beanstalk console. You can connect to the instances using any SSH client. You can connect to the instances running Windows using Remote Desktop.


2 Answers

I had a similar issue but on Elastic Beanstalk, so I created a config file (e.g. applogs.config) in .ebextensions folder of the app. This creates the app-logs folder if it is not there already and sets the file permissions and owner so that the app can write its logs there.

commands:   00_create_dir:     command: mkdir -p /var/log/app-logs   01_change_permissions:     command: chmod g+s /var/log/app-logs   02_change_owner:     command: chown wsgi:wsgi /var/log/app-logs 

Finally, in your Django settings:

LOGGING = {     'version': 1,     'disable_existing_loggers': False,     'handlers': {         'file': {             'level': 'DEBUG',             'class': 'logging.FileHandler',             'filename': '/var/log/app-logs/django.log',         },     },     'loggers': {         'django': {             'handlers': ['file'],             'level': 'DEBUG',             'propagate': True,         },     }, } 

Aditionally, if you want your log to be accessible from beanstalk logs using the web, add this to your file in .ebextensions

files:   "/opt/elasticbeanstalk/tasks/taillogs.d/django.conf":     mode: "000755"     owner: root     group: root     content: |       /var/log/app-logs/django.log 
like image 122
Steve Dunlop Avatar answered Oct 05 '22 08:10

Steve Dunlop


Ok, I figured out a way to do it.

First I connected via ssh to ec2 machine, then I create a folder in /var/log called app_logs with root user:

mkdir /var/log/app_logs 

After that I did the follow:

cd /var/log/ chmod g+s app_logs/ setfacl -d -m g::rw app_logs/ chown wsgi:wsgi app_logs/ 

That ensures that all the files created in this folder will have wsgi as owner and will be writable for the group that the file belongs. I had to do that because I noticed that the log file created by django app had root as owner and owner group but the application runs through wsgi user.

Finally I changed DEBUG_LOG_DIR to /var/log/app_logs/django_debug.log

like image 38
user1126167 Avatar answered Oct 05 '22 08:10

user1126167