Commits

Alexander Lyabah committed 9ebe534

split the middlewares and add a save_files

Comments (0)

Files changed (8)

djucsvlog/DjLogger.py

 from ucsvlog.utils import unicoder
 import settings as my_settings
 from django.conf import settings
-from djucsvlog.fields import request as request_fields
-from djucsvlog.fields import response as response_fields
-from djucsvlog.fields import exception_info as exception_fields
 
 if my_settings.PRINT:
     class Logger(OriginalLogger):
     def_req_log = None #fields, which will be logged in request
     def_res_log = None #fields, which will be logged in response
     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_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):
-        return self.arr_lambda_by_name(my_settings.REQUEST_FIELDS,request_fields)
-    def iget_def_res_log(self):
-        return self.arr_lambda_by_name(my_settings.RESPONSE_FIELDS,response_fields)
-    def iget_count_log_fields(self):
-        return my_settings.COUNT_FIELDS or (max(len(self.def_req_log),len(self.def_res_log))+2)
     def iget_level(self):
         return my_settings.LOG_NAMES
     def iget_loglev(self):
         return my_settings.LOG_DEF
     def iget_func_fields(self):
         return my_settings.LOG_BASE
-    def iget_exception_vars_max_length(self):
-        return my_settings.EXCEPTION_VARS_MAX_LENGTH
     
     
-    def process_request(self,request):
-        self.mid_a_log('REQ',self.arr_funcs(self.def_req_log,request))
-    def process_response(self,request,response):
-        self.mid_c_log('REQ',self.arr_funcs(self.def_res_log,request,response))
-        if my_settings.FLUSH_RESPONSE:
-            self.flush()
-
-    def process_exception(self,exception,exR,close_name='REQ'):
-        #save head exception information
-        def_exc_log = self.arr_lambda_by_name(my_settings.EXCEPTION_FIELDS,exception_fields)
-        self.err(['TOP']+self.arr_funcs(def_exc_log,exception))
-        
-        #save frames info
-        def_exc_log = self.arr_lambda_by_name(my_settings.EXCEPTION_STACK_FIELDS,exception_fields)
-        frames = exR.get_traceback_frames()
-        frames.reverse()
-        for frame in frames:
-            self.err(['MIDDLE']+self.arr_funcs(def_exc_log,frame,exR))
-        
-        #save close info
-        def_exc_log = self.arr_lambda_by_name(my_settings.EXCEPTION_CLOSE,exception_fields)
-        self.mid_c_log(close_name,self.arr_funcs(def_exc_log,exception,exR))
-
-        if my_settings.FLUSH_RESPONSE:
-            self.flush()
+    
 
 if my_settings.THREAD_LOCALS:
     try:

djucsvlog/__init__.py

 __all__ = ['glog']
-from djucsvlog.middleware import glog
+from djucsvlog.glog import glog

djucsvlog/fields/exception_info.py

     return value
 
 def svars(frame,exR):
-    from djucsvlog.middleware import glog
+    import djucsvlog.settings as my_settings
     vars = frame['vars']
     ret = ''
     for v in vars:
-        ret += ' - %s:%s\n' %(v[0],unicoder_truncate(v[1],glog.iget_exception_vars_max_length()))
+        ret += ' - %s:%s\n' %(v[0],unicoder_truncate(v[1],my_settings.EXCEPTION_VARS_MAX_LENGTH))
     return ret
 
 def emeta(exception,exR):

djucsvlog/fields/request.py

 
 def browser_uuid(request):
     from djucsvlog.glog import glog
+    import uuid
+    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
     return glog.browser_uuid
 
 def remote_addr(request):
     if 'HTTP_ACCEPT_ENCODING' in meta:
         ret += ' Accept Encoding:'+meta['HTTP_ACCEPT_ENCODING']
     return ret
+
+def read_in_chunks(file_object, chunk_size=1024):
+    """Lazy function (generator) to read a file piece by piece.
+    Default chunk size: 1k.
+    Stolen from http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python
+    """
+    while True:
+        data = file_object.read(chunk_size)
+        if not data:
+            break
+        yield data
+import os
+def find_place_to_store(name):
+    from random import randint
+    while True:
+        full_path = os.path.join(my_settings.REQ_SAVE_FILES_FOLDER,name)
+        if not os.path.exists(full_path):
+            return full_path
+        point_cunks = name.split('.')
+        if len(point_cunks) == 1:
+            #without exstension
+            name += str(randint(0,9))
+        else:
+            #with extenstion add randint after name and before extension
+            name = '%s%s.%s' % ('.'.join(point_cunks[:-1]), randint(0,9),point_cunks[-1])
+            
+        
+
+def save_files(request):
+    assert my_settings.REQ_SAVE_FILES_FOLDER , 'UCSVLOG_REQ_SAVE_FILES_FOLDER must be set for using save_files'
+
+    if not request.FILES:
+        return '{}'
+    ret = ''
+    for field,file in request.FILES.items():
+        store_filename = find_place_to_store(file.name)
+        ret += '\n"%s":%s,' %(field,json.dumps([file.name,store_filename,file.content_type,file.size]))
+        
+        fh = open(store_filename,'wb')
+        for piece in read_in_chunks(file.file):
+            fh.write(piece)
+        fh.close()
+    
+    return '{'+ret+'}'
+        

djucsvlog/glog.py

-
+__all__ = ['glog']
 import settings as my_settings
 from ucsvlog.utils import import_name
-Logger = import_name(my_settings.CLASS)
-glog = Logger()
+from djucsvlog.DjLogger import DjLogger
+glog = DjLogger()
 
 for mod in my_settings.COMPONENTS:
     try:

djucsvlog/middleware.py

 from django.db import transaction
 from glog import glog
 import settings as my_settings
+from djucsvlog.fields import request as request_fields
+from djucsvlog.fields import response as response_fields
+from djucsvlog.fields import exception_info as exception_fields
+
 
 class LogRequestInfo(object):
+    def __init__(self,*args,**kwargs):
+        self.def_req_log = self.iget_def_req_log() 
+        self.def_res_log = self.iget_def_res_log()
+        self.mid_log,self.mid_a_log,self.mid_c_log = glog.get_trio_log(my_settings.MIDDLEWARE_LOG_NAME)
+        return super(LogRequestInfo,self).__init__(*args,**kwargs)
+    
+    def iget_def_req_log(self):
+        return glog.arr_lambda_by_name(my_settings.REQUEST_FIELDS,request_fields)
+    
+    def iget_def_res_log(self):
+        return glog.arr_lambda_by_name(my_settings.RESPONSE_FIELDS,response_fields)
+    
+    def iget_exception_vars_max_length(self):
+        return my_settings.EXCEPTION_VARS_MAX_LENGTH
+    
     def process_request(self,request):
-        glog.process_request(request)
+        self.mid_a_log(my_settings.REQ_LOG_NAME,glog.arr_funcs(self.def_req_log,request))
     def process_response(self,request,response):
-        glog.process_response(request, response)
+        self.mid_c_log(my_settings.REQ_LOG_NAME,glog.arr_funcs(self.def_res_log,request,response))
+        if my_settings.FLUSH_RESPONSE:
+            glog.flush()
+        if glog.browser_uuid:
+            response.set_cookie(my_settings.BROWSER_UUID_COOKIE, glog.browser_uuid, max_age=my_settings.BROWSER_UUID_COOKIE_MAX_AGE)
         return response
+
+    def _process_exception(self,exception,exR,close_name=my_settings.REQ_LOG_NAME):
+        #save head exception information
+        def_exc_log = glog.arr_lambda_by_name(my_settings.EXCEPTION_FIELDS,exception_fields)
+        glog.err([my_settings.EXCEPTION_TOP_NAME]+glog.arr_funcs(def_exc_log,exception))
+        
+        #save frames info
+        def_exc_log = glog.arr_lambda_by_name(my_settings.EXCEPTION_STACK_FIELDS,exception_fields)
+        frames = exR.get_traceback_frames()
+        frames.reverse()
+        for frame in frames:
+            glog.err([my_settings.EXCEPTION_MIDDLE_NAME]+glog.arr_funcs(def_exc_log,frame,exR))
+        
+        #save close info
+        def_exc_log = glog.arr_lambda_by_name(my_settings.EXCEPTION_CLOSE,exception_fields)
+        self.mid_c_log(close_name,glog.arr_funcs(def_exc_log,exception,exR))
+
+        if my_settings.FLUSH_RESPONSE:
+            glog.flush()
+    
     def process_exception(self, request, exception):
         if exception.__class__ == Http404:
             return
             transaction.rollback()
         except transaction.TransactionManagementError:
             pass
-        glog.process_exception(exception,exR)
+        self._process_exception(exception,exR)
 
+class LogViewInfo(object):
+    def __init__(self,*args,**kwargs): 
+        self.mid_log,self.mid_a_log,self.mid_c_log = glog.get_trio_log(my_settings.MIDDLEWARE_LOG_NAME)
+        return super(LogViewInfo,self).__init__(*args,**kwargs)
+        
+    def process_request(self,request):
+        self.mid_a_log(my_settings.VIEW_LOG_NAME,glog.arr_funcs(glog.arr_lambda_by_name(my_settings.VIEW_OPEN_FIELDS,request_fields),request))
+    
+    def process_response(self,request,response):
+        if my_settings.VIEW_CLOSE_FIELDS:
+            self.mid_c_log(my_settings.VIEW_LOG_NAME,glog.arr_funcs(glog.arr_lambda_by_name(my_settings.VIEW_CLOSE_FIELDS,request_fields),request,response))
+        return response
+        
+        
 
-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

 def get(key, default):
     globals()[key] = getattr(settings, set_prefix+key, default)
 get('MIDDLEWARE_LOG_NAME','log')
-get('REQUEST_FIELDS',['sessionid','userid','remote_addr','path','request_form_data','http_user_agent']) # request logged fields
+get('REQ_LOG_NAME','REQ')
+get('VIEW_LOG_NAME','IN')
+get('EXCEPTION_TOP_NAME','TOP')
+get('EXCEPTION_MIDDLE_NAME','IN')
+get('VIEW_OPEN_FIELDS',['sessionid','userid'])
+get('VIEW_CLOSE_FIELDS',None) 
+get('REQUEST_FIELDS',['remote_addr','path','request_form_data','http_user_agent']) # request logged fields
 get('RESPONSE_FIELDS',['ctype','content']) #response logged fields
 get('RESPONSE_CONTENT_LOG_TYPES',['text/json','text/xml','application/json','application/xml'])
 get('EXCEPTION_FIELDS',['estr','etype'])
 get('BUFFERING',0)
 get('FLUSH_RESPONSE',True)
 get('LOG_BASE',['stacksize','filename','lineno','fname']) # list of fields, which will be show in every log line
-get('CLASS','djucsvlog.DjLogger.DjLogger')
 get('PRINT',False)
 get('THREAD_LOCALS',False)
 get('RELATED_FOLDER',None)
 get('REQ_REMOTE_ADDR_REAL_IP','HTTP_X_FORWARDED_FOR')
 get('REQ_REMOTE_ADDR_ANONYMOUSE','A')
 get('REQ_HTTP_HOST_NOHOST','_NH_')
+get('REQ_SAVE_FILES_FOLDER',None)
 
 
 ##
 # and all information about optional setting you can find in ucsvlog/settings.py 
 
 UCSVLOG_FILE = 'log.ucsv'
+UCSVLOG_REQ_SAVE_FILES_FOLDER = PROJECT_ROOT
 UCSVLOG_LOG_NAMES = ['err','imp','inf','dbg','log'] ##ADD OPTIONAL
-UCSVLOG_REQUEST_FIELDS = ['path','language_code','request_form_data','userid','sessionid','remote_addr','http_user_agent']
-UCSVLOG_COMPONENTS = (
-            'djucsvlog.components.sql',
-    )
+UCSVLOG_REQUEST_FIELDS = ['remote_addr','path','request_form_data','http_user_agent','browser_uuid','save_files']  ##ADD OPTIONAL
+#UCSVLOG_COMPONENTS = (
+#            'djucsvlog.components.sql',
+#    )
 UCSVLOG_PRINT = True
 #CSVLOG_OFF = True
 
 )
 
 MIDDLEWARE_CLASSES = (
+    'djucsvlog.middleware.LogRequestInfo', ##ADD (at the top of list)
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.locale.LocaleMiddleware', 
     'django.contrib.auth.middleware.AuthenticationMiddleware',
-    'djucsvlog.middleware.LogRequestInfo', ##ADD (at the bottom of list)
+    'djucsvlog.middleware.LogViewInfo', ##ADD (at the bottom of list)
 )
 
 ROOT_URLCONF = 'django-ucsvlog.urls'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.