
import os
import logging

from turbo.conf import app_config


formatter = logging.Formatter('%(levelname)s:%(asctime)s %(name)s:%(lineno)d:%(funcName)s %(message)s')


def init_file_logger(logger, log_path, log_size, log_count):
    fh = logging.handlers.RotatingFileHandler(log_path, maxBytes=log_size, backupCount=log_count)
    fh.setLevel(app_config.get_log_level)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    

def init_logger(logger):
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)


def getLogger(currfile=None, log_path=None, log_size=500*1024*1024, log_count=3):

    if not logging.root.handlers:
        root_logger = logging.getLogger()
        root_logger.setLevel(app_config.get_log_level)
        init_logger(root_logger)
        if log_path:
            init_file_logger(root_logger, log_path, log_size, log_count)

    path = os.path.abspath(currfile)
    if os.path.isfile(path):
        file_name = os.path.basename(path)
        module_name = file_name[0:file_name.rfind('.py')]
        logger_name_list = [module_name]

        # find project root dir util find it or to root dir '/'
        while True:
            # root path check
            path = os.path.dirname(path)
            if path == '/':
                break

            # project root path
            dirname = os.path.basename(path)
            if dirname == app_config.project_name:
                break
            logger_name_list.append(dirname)

        logger_name_list.reverse()

        return logging.getLogger('.'.join(logger_name_list))

    if isinstance(currfile, basestring) and currfile.strip():
        logger = logging.getLogger(currfile)
        logger.setLevel(app_config.get_log_level)

        if logger.handlers:
            return logger

        return logging.getLogger(currfile)

    return logging.getLogger()

# Logger objects for internal turbo use
app_log = getLogger('turbo.app')
model_log = getLogger('turbo.model')
util_log = getLogger('turbo.util')
helper_log = getLogger('turbo.helper')
@@ -18,16 +18,40 @@
  logging
+%0Aimport logging.handlers
 %0A%0Afrom t
@@ -79,17 +79,70 @@
 config%0A%0A
-%0A
+_level = app_config.get_log_level or logging.warning%0A_
 formatte
@@ -239,24 +239,25 @@
 e)s')%0A%0A%0Adef 
+_
 init_file_lo
@@ -264,24 +264,31 @@
 gger(logger,
+ level,
  log_path, l
@@ -308,16 +308,344 @@
 count):%0A
+    %0A    level = _level if level is None else level%0A    if level not in %5Blogging.DEBUG, logging.ERROR, logging.NOTSET, logging.WARNING, logging.INFO%5D:%0A        level = logging.DEBUG%0A%0A    for h in logger.handlers:%0A        if isinstance(h, logging.handlers.RotatingFileHandler):%0A            if h.level == :%0A                return%0A%0A
     fh =
@@ -788,32 +788,33 @@
 fh.setFormatter(
+_
 formatter)%0A    l
@@ -844,21 +844,29 @@
   %0A%0Adef 
+_
 init_
+stream_
 logger(l
@@ -957,16 +957,17 @@
 rmatter(
+_
 formatte
@@ -1021,24 +1021,36 @@
 rrfile=None,
+ level=None,
  log_path=No
@@ -1095,267 +1095,73 @@
 3):%0A
-%0A
     
-if no
+# ini
 t logg
-ing.root.handlers:%0A        root_logger = logging.getLogger()%0A        root_logger.setLevel(app_config.get_log_level)%0A        init_logger(root_logger)%0A        if log_path:%0A            init_file_logger(root_logger, log_path, log_size, log_count)%0A
+er first%0A    logger = None%0A%0A    # py module logger
 %0A   
@@ -1222,16 +1222,20 @@
 e(path):
+    
 %0A       
@@ -1808,38 +1808,40 @@
 erse()%0A%0A        
-return
+logger =
  logging.getLogg
@@ -1872,16 +1872,35 @@
 list))%0A%0A
+    #normal logger%0A
     if i
@@ -1952,16 +1952,19 @@
 strip():
+   
 %0A       
@@ -2051,27 +2051,164 @@
 _level)%0A
-%0A        if
+        logger = logging.getLogger(currfile)%0A%0A    if not logger:%0A        logger = logging.getLogger()%0A%0A    if logger == logging.root:%0A        if not
  logger.
@@ -2233,64 +2233,175 @@
     
-return 
+_init_stream_logger(
 logger
+)
 %0A%0A    
-    return logging.getLogger(currfile
+level = _level if level is None else level = %0A%0A    if log_path:%0A        _init_file_logger(logger, level, log_path, log_size, log_count
 )%0A%0A 
@@ -2414,31 +2414,20 @@
 urn logg
-ing.getLogg
 er
-()
+%0A%0A
 %0A%0A# Logg

fix log.py apply to many user case