Commits

Alexander Lyabah committed dd911e1

ucsvlog==0.1.2 and djucsvlog_clear_old_files

Comments (0)

Files changed (24)

 syntax: glob
 *.pyc
+*.swp
 log.csv
-main
+main
File contents unchanged.

__init__.py

File contents unchanged.

djucsvlog/DjLogger.py

     def __init__(self):
         self.def_req_log = self.iget_def_req_log() 
         self.def_res_log = self.iget_def_res_log()
+        self.browser_uuid = None  # for browser uuid middleware
+
         super(DjLogger,self).__init__(
                     self.iget_action_log(),
                     self.iget_level(),
                     self.iget_default(),
                     self.iget_loglev(),
                     self.iget_func_fields(),
-                    self.iget_buffering()
+                    self.iget_buffering(),
+                    self.iget_related_folder()
                 )
         self.mid_log,self.mid_a_log,self.mid_c_log = self.get_trio_log(my_settings.MIDDLEWARE_LOG_NAME)
 
+    def iget_related_folder(self):
+        return my_settings.RELATED_FOLDER    
     def iget_buffering(self):
         return my_settings.BUFFERING    
     def iget_def_req_log(self):

djucsvlog/fields/request.py

-
+import json
+import djucsvlog.settings as my_settings
 def readable_dict(dd):
+    if not dd:
+        return '{}'
     ret = ''
     for kd,vd in dd.items():
-        ret += ' - %s:%s\n' %(unicode(kd),vd)
-    return ret
+        ret += '\n"%s":%s,' %(unicode(kd),json.dumps(vd))
+    return '{'+ret[:-1]+'\n}'
 
 def path(request):
     return request.path
     return readable_dict(request.POST)
 
 def files(request):
+    if not request.FILES:
+        return '{}'
     ret = ''
     for field,file in request.FILES.items():
-        ret += ' - %s %s %s %s\n' %(field,file.name,file.content_type,file.size)
-    return ret
+        ret += '\n"%s":%s,' %(field,json.dumps([file.name,file.content_type,file.size]))
+    return '{'+ret[:-1]+'\n}'
 
 def cookies(request):
     return readable_dict(request.COOKIES)
 def request_form_data(request):
     ret = ''
     r_get = get(request)
-    if r_get:
-        ret+=' -- GET --\n'+r_get
+    if r_get != '{}':
+        ret+='\n"G":'+r_get+','
     
     r_post = post(request)
-    if r_post:
-        ret+=' -- POST --\n'+r_post
+    if r_post != '{}':
+        ret+='\n"P":'+r_post+','
     
     r_files = files(request)
-    if r_files:
-        ret+=' -- FILES --\n'+r_files
-        
-    return ret
+    if r_files != '{}':
+        ret+='\n"F":'+r_files+','
+    if not ret:
+        return '{}'
+    return '{'+ret[:-1]+'\n}'
 
 def request_data(request):
     ret = request_form_data(request)
     
+    
     r_cookies = cookies(request)
-    if r_cookies:
-        ret+=' -- COOKIES --\n'+r_cookies
+    if  r_cookies == '{}':
+        return ret
     
-    return ret
+    if ret == '{}':
+        return '{"C":'+r_cookies+'}'
+
+    return ret[:-1] + ',"C":'+r_cookies+'}'
 
 def userid(request):
     return (request.user.id or '0')
         return request.session._session_key
     return ''
 
+def browser_uuid(request):
+    from djucsvlog.glog import glog
+    return glog.browser_uuid
+
 def remote_addr(request):
-    return request.META.get('REMOTE_ADDR','ANONYMOUS')
+    return request.META.get(my_settings.REQ_REMOTE_ADDR_REAL_IP,request.META.get('REMOTE_ADDR', my_settings.REQ_REMOTE_ADDR_ANONYMOUSE))
+
+def http_host(request):
+    return request.META.get('HTTP_HOST',my_settings.REQ_HTTP_HOST_NOHOST)
 
 def http_user_agent(request):
     meta = request.META
         ret+= ' Accept Language:'+meta['HTTP_ACCEPT_LANGUAGE']
     if 'HTTP_ACCEPT_ENCODING' in meta:
         ret += ' Accept Encoding:'+meta['HTTP_ACCEPT_ENCODING']
-    return ret
+    return ret

djucsvlog/glog.py

+
+import settings as my_settings
+from ucsvlog.utils import import_name
+Logger = import_name(my_settings.CLASS)
+glog = Logger()
+
+for mod in my_settings.COMPONENTS:
+    try:
+        __import__(mod, {}, {}, [''])
+    except ImportError,e:
+        raise e

djucsvlog/management/__init__.py

Empty file added.

djucsvlog/management/commands/__init__.py

Empty file added.

djucsvlog/management/commands/djucsvlog_clear_old_files.py

+import os
+import time
+
+from django.core.management.base import BaseCommand, CommandError
+from optparse import make_option
+
+from djucsvlog.glog import glog
+import djucsvlog.settings as my_settings
+from ucsvlog.log_decorators import a_log_call
+
+
+class Command(BaseCommand):
+    option_list = BaseCommand.option_list + (
+                make_option('--folder',
+                            dest='folder',
+                            default = my_settings.CLEAR_OLD_FOLDER,
+                            help='path to the folder, which has to be inspect'
+                            ),
+                make_option('--perion',
+                            dest='period',
+                            default = my_settings.CLEAR_OLD_PERIOD,
+                            help='period of old logs'
+                            ),
+                make_option('--remove',
+                            dest='remove_files',
+                            action='store_true',
+                            default = False,
+                            help='period of old logs'
+                            ),
+                make_option('--archive',
+                            dest='archive_mask',
+                            default = my_settings.CLEAR_OLD_ARCHIVE,
+                            help='archive mask'
+                            ),
+                        )
+
+    period_sec = None # calculated value of period. From days to sec
+    filtered_files = None # array of file abs name which has to be clear
+    abs_folder = None # absolute folder for logs clear
+
+
+    @a_log_call(glog)
+    def handle(self,folder,period, remove_files, archive_mask, *args,**kwargs):
+        self.period_sec = period * 24 * 60 * 60
+        self.filtered_files = []
+
+        if folder:
+            folder = os.path.abspath(folder)
+        else:
+            assert my_settings.FILE, 'You have to defile folder attr, settings.UCSVLOG_CLEAR_OLD_FOLDER or UCSVLOG_FILE'
+
+            #getting search folder from FILE settings
+            abs_folder = os.path.abspath(my_settings.FILE)
+            abs_folder_els = []
+            for el in abs_folder.split('/')[1:]:
+                if  el.find('%') + 1:
+                    break
+                abs_folder_els.append(el)
+            folder = '/' + '/'.join(abs_folder_els)
+
+        self.abs_folder = folder
+
+        os.path.walk(folder,self.walk_directory,None)
+        if not self.filtered_files:
+            glog.log('nothing to do')
+            return
+
+        if remove_files:
+            self.remove_filtered_files()
+            return
+
+        if archive_mask:
+            self.archive_filtered_files(archive_mask)
+            self.remove_filtered_files()
+            return
+
+
+    def remove_filtered_files(self):
+        for item in self.filtered_files:
+            glog.log(['unlink',item])
+            #os.unlink(item)
+
+    def archive_filtered_files(self,archive_mask):
+        import tarfile
+        def reset_abs_folder(tarinfo, abs_folder = self.abs_folder):
+            tarinfo.name = tarinfo.name[len(abs_folder):]
+            return tarinfo
+        tar = tarfile.open(archive_mask % glog.action_log_template_params(), "w:gz")
+        for item in self.filtered_files:
+            tar.add(item, filter=reset_abs_folder)
+        tar.close()
+
+
+
+
+
+    def walk_directory(self,args,dirname,names):
+        for filename in names:
+            full_name = os.path.join(dirname, filename)
+            if not os.path.isfile(full_name):
+                continue
+            if ( time.time() - os.path.getctime(full_name) ) > self.period_sec:
+                glog.log(['walk',full_name,'Y'])
+                self.filtered_files.append(full_name)
+            else:
+                glog.log(['walk',full_name,'N'])

djucsvlog/middleware.py

 import sys
+import datetime
 
 from django.views.debug import ExceptionReporter
 from django.http import Http404
 from django.db import transaction
-
+from glog import glog
 import settings as my_settings
-from ucsvlog.utils import import_name
-Logger = import_name(my_settings.CLASS)
-glog = Logger()
-
-for mod in my_settings.COMPONENTS:
-    try:
-        __import__(mod, {}, {}, [''])
-    except ImportError,e:
-        raise e
 
 class LogRequestInfo(object):
     def process_request(self,request):
         except transaction.TransactionManagementError:
             pass
         glog.process_exception(exception,exR)
+
+
+import uuid
+class BrowserUUID(object):
+    def process_request(self,request):
+        browser_uuid = request.COOKIES.get(my_settings.BROWSER_UUID_COOKIE)
+        if not browser_uuid:
+            browser_uuid = uuid.uuid4().get_hex()
+
+        glog.browser_uuid = browser_uuid
+
+    def process_response(self,request,response):
+        response.set_cookie(my_settings.BROWSER_UUID_COOKIE, glog.browser_uuid, max_age=my_settings.BROWSER_UUID_COOKIE_MAX_AGE)
+        return response

djucsvlog/settings.py

 get('CLASS','djucsvlog.DjLogger.DjLogger')
 get('PRINT',False)
 get('THREAD_LOCALS',False)
+get('RELATED_FOLDER',None)
+get('FILE',None)
+
+
+##
+#Fieald settings
+
+
+##
+#REQ
+
+get('REQ_REMOTE_ADDR_REAL_IP','HTTP_X_FORWARDED_FOR')
+get('REQ_REMOTE_ADDR_ANONYMOUSE','A')
+get('REQ_HTTP_HOST_NOHOST','_NH_')
+
+
+##
+#Browser uuid middleware
+get('BROWSER_UUID_COOKIE','ucsvlog_follow')
+get('BROWSER_UUID_COOKIE_MAX_AGE',60*60*24*256) #cookie for one year
+
+
+##
+# Command djucsvlog_clear_old_files
+get('CLEAR_OLD_FOLDER',None)
+get('CLEAR_OLD_PERIOD',7) # period in days of logs keeping
+get('CLEAR_OLD_ARCHIVE',None) 

glog.py

File contents unchanged.

manage.py

File contents unchanged.

myapp/__init__.py

File contents unchanged.

myapp/management/__init__.py

File contents unchanged.

myapp/management/commands/__init__.py

File contents unchanged.

myapp/management/commands/test_exception_command.py

File contents unchanged.

myapp/management/commands/test_logged_command.py

File contents unchanged.

myapp/models.py

File contents unchanged.

myapp/urls.py

File contents unchanged.

myapp/views.py

File contents unchanged.

settings.py

File contents unchanged.

urls.py

File contents unchanged.

views.py

File contents unchanged.