Add Remote_Addr to seahub_django_request.log

Hi there,
Here is my Problem:
I use Seafile Server 5.1.3 on an ubuntu server. Right now If somebody is accessing a wrong download or upload-link of my server, I receive a message like this in seahub_django_request.log:

2016-10-04 20:04:30,850 [WARNING] django.request:170 get_response Not Found: /seafile/d/db0fbbbbd12

What I would like to achieve is that the log entry also includes the requesting IP-Adresse.

What I did already:
I know that this is an django error message that is created in /seafile-server-latest/seahub/seahub/settings.py. There is this paragraph:

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'standard': {
        'format': '%(asctime)s [%(levelname)s] %(name)s:%(lineno)s %(funcName)s %(message)s'
    },
},

I tried to add %(remote_addr)s but as far as I have no django knowledge this did not work.
Can anybody help how I can add the remote_ip to this logging entry?

Thanks and best regards
Christoph

Ok I tried to learn django and python within some hours.

  1. At first I tried to find a solution in /seahub/thirdparty/Django-1.8.10-py2.7.egg/django/utils/log.py. There I found out that seafile already has a function to determine the own ip. But I didn’t managed to add the ip to the log files.

  2. Then I tried to change /seahub/seahub/views/file.py. There I found a lot of “raise Http404” entries. So I tried to add a custom log entry like: logger.warn(‘File not found. ip: %s’ (ip)). In the end I gave up because I didn’t find the right function and also I didn’t managed to add the ip.

  3. Then I tried to write my own middleware. So I added in the settings.py in the MIDDLEWARE_CLASSES the entry: ‘seahub.base.ionas.RequestLoggingMiddleware’. Then I create a file ionas.py under base with the following content:

    import logging
    import datetime
    logger = logging.getLogger(name)

    class RequestLoggingMiddleware(object):
    def process_request(self, request)

     ip_address = request.META.get('REMOTE_ADDR')
     url = request.META.get('PATH_INFO')
     #code = response.status_code()
     logger.warn('|| IP: %s, URL: %s' % (ip_address, url ))
    

This created entries in seahub.log but I need additional infos like the response status to check if the page is 404.

Can anybody please help me to finish this task. I am quite sure for a experienced django programmer or python developer it will be straigt forward.
Best regards
Christoph

1 Like

ok. finaly I made it with an own middleware.

I will create two fail2ban rules to protect trial-and-error attempts for download- or uploadlinks and for the login to seafile.

Then I will publish my result here.
Best regards
Christoph

1 Like

I think you need to reimplement the process_response method in your middleware class.

Just curious, why not use the http access logs of nginx/apache?

Hi lins05,
thanks for confirming that I have to reimplement the process_response method. That is exactly what I did. More information will follow.
Now to your other proposal: In the logs of nginx (var/log/nginx/access.log and error.log) there are of cause entries if I access existing urls of seafile. As soon as I access a page that does not exist and seafile delivers a 404 page there is no entry.

Here is my nginx conf. If it is possible to add an entry to the error.log in every case that a 404 is returned it would help me a lot. Hint: I use seafile in a root directory.

location / {
fastcgi_pass    127.0.0.1:8000;
fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
fastcgi_param   PATH_INFO           $fastcgi_script_name;
fastcgi_param   SERVER_PROTOCOL     $server_protocol;
fastcgi_param   QUERY_STRING        $query_string;
fastcgi_param   REQUEST_METHOD      $request_method;
fastcgi_param   CONTENT_TYPE        $content_type;
fastcgi_param   CONTENT_LENGTH      $content_length;
fastcgi_param   SERVER_ADDR         $server_addr;
fastcgi_param   SERVER_PORT         $server_port;
fastcgi_param   SERVER_NAME         $server_name;
fastcgi_param   REMOTE_ADDR         $remote_addr;
fastcgi_param   HTTPS               on;
fastcgi_param   HTTP_SCHEME         https;
access_log      /var/log/nginx/seahub.access.log;
error_log       /var/log/nginx/seahub.error.log;
}

location /seafhttp {
rewrite ^/seafhttp(.*)$ $1 break;
proxy_pass http://127.0.0.1:8082;
client_max_body_size 0;
proxy_connect_timeout  36000s;
proxy_read_timeout  36000s;
}

location /media {
rewrite ^/media(.*)$ /media$1 break;
root /home/seafile/seafile-server-latest/seahub;
}

Whether 404 is logged is decided by the nginx log_not_found directive, and it defaults to “on”, so i don’t know why 404 is not logged in your case. Maybe you should search for “log_not_found” in your /etc/nginx directory to see if there is some global settings that turns it off.