import uuid
import logging
from mothernature import Environment
from colorlog import ColoredFormatter

class Builder(object):

    def env(self, env_file, env_name="DEV"):
        """Environment Management

        We should load all configuration based on current environment mode.
        Thanks to mothernature -> https://github.com/femmerling/mothernature

        Args:
            self (Builder): Current object instance
            env_file (str): Yaml file that need to load
            env_name (Optional[str]): Environment name that needed to load on
                initialize engine.  By default is DEV.

        Returns:
            Yaml object

        Raises:
            IOError: If given yaml file not found
        """
        env = Environment(env_file, env_name)
        config = env.get_config()
        return config

    def settings(self, config):
        """Tornado Settings

        Build settings which will loaded by Tornado.

        Args:
            self (Builder): Current object instance
            configs (yaml): Yaml object used to load environment variables

        Returns:
            Dictionaries
        """
        setting = {
            "debug": config.get('DEBUG'),
            "compress_response": config.get('COMPRESS_RESPONSE'),
            "cookie_secret": uuid.uuid1().hex,
            "xsrf_cookies": config.get('XSRF'),
            "static_hash_cache": config.get('STATIC_HASH_CACHE'),
            "static_path": config.get('STATIC_PATH'),
            "static_url_prefix": config.get('STATIC_URL_PREFIX') 
        }

        return setting

    def logs(self, configs, logger):
        """Set Logging Level

        Use internal Tornado's logging helper.

        Args:
            self (Builder): Current object instance
            configs (yaml): Yaml object used to load environment variables
            logger (callback): Logger function
        """
        levels = {
            'DEBUG': logging.DEBUG,
            'INFO': logging.INFO,
            'ERROR': logging.ERROR,
            'WARNING': logging.WARNING,
            'CRITICAL': logging.CRITICAL 
        }

        # Set default logging to logging info
        log_level = logging.INFO
        log_level_config = configs.get('LOG_LEVEL')

        # Set if log_level_config available on 
        # registered levels
        if log_level_config in levels:
            log_level = levels[log_level_config]

        # Reset logging level
        logger.setLevel(log_level)
@@ -20,16 +20,49 @@
 logging%0A
+from core.session import Session%0A
 from mot
@@ -1169,32 +1169,160 @@
 ies%0A        %22%22%22%0A
+%0A        # build session configurations%0A        session = Session(config)%0A        session_settings = session.get_used_config()%0A%0A
         setting 
@@ -1712,17 +1712,16 @@
 PREFIX')
- 
 %0A       
@@ -1716,32 +1716,247 @@
 IX')%0A        %7D%0A%0A
+        # We doesn't need to add session settings if current%0A        # lifecycle just give us an empty dictionary%0A        if len(session_settings.keys()) %3E 0:%0A            setting.update(session = session_settings)%0A%0A
         return s
@@ -2483,17 +2483,16 @@
 CRITICAL
- 
 %0A       
@@ -2673,17 +2673,16 @@
 lable on
- 
 %0A       
@@ -2855,8 +2855,9 @@
 g_level)
+%0A

Add session configuration at global application settings