0

I am fairly new in backend development. I was trying to write some log with Django rest framework. I set up the WSGI mode and the 000-default.conf file is

<VirtualHost *:80>
ServerAdmin user@gmail.com
DocumentRoot /home/ubuntu/myproject_backend/myproject

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/ubuntu/myproject_backend/myproject/static

<Directory /home/ubuntu/myproject_backend/myproject/static>
Require all granted
</Directory>

<Directory /home/ubuntu/myproject_backend/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIDaemonProcess myproject python-path=/home/ubuntu/myproject_backend/myproject python-home=/home/ubuntu/myproject_backend/env
WSGIProcessGroup myproject 
WSGIScriptAlias / /home/ubuntu/myproject_backend/myproject/myproject/wsgi.py
WSGIPassAuthorization On
</VirtualHost>

I added the LOGGING in the setting.py

LOGGING = {
    'version': 1,
    # Version of logging
    'disable_existing_loggers': False,
    #disable logging
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'my_log.log',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': env("LOG_LEVEL"),
            'propagate': True,
        },
    },
}

I tried with a simple warning log

logging.warning("Logging Tutorials")

Now the log is working when I was testing locally with the runserver command. However, when I pushed the code to the server it's getting permission denied writing the error.log file.

PermissionError: [Errno 13] Permission denied

I was wondering how do I give the permission and which user? the server is a ubuntu OS.

I know there are a few posts about this issue. However, I could not figure out what exactly do I have to do. Thanks in advance.

sadat
  • 4,004
  • 2
  • 29
  • 49

1 Answers1

0

Logs that you write yourself as best put in the base directory of the project or a subdirectory. For example, I put logs in var/log below the base directory. However, this assumes that the running user has permission to write in that directory and that's not always the case and depends on the deployment.

In order to have the log written on the same location both local and production, use BASE_DIR that is pre-defined in your settings file:


LOGGING = {
    'version': 1, # Version of logging
    'disable_existing_loggers': False, #disable logging
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': f'{BASE_DIR}/my_log.log',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': env("LOG_LEVEL"),
            'propagate': True,
        },
    },
}