# Path: boiler/log/file.py
# def file_logger(app, level=None):
#     """
#     Get file logger
#     Returns configured fire logger ready to be attached to app
# 
#     :param app:         application instance
#     :param level:       log this level
#     :return:            RotatingFileHandler
#     """
#     path = os.path.join(os.getcwd(), 'var', 'logs',  'app.log')
# 
#     max_bytes = 1024 * 1024 * 2
#     file_handler = RotatingFileHandler(
#         filename=path,
#         mode='a',
#         maxBytes=max_bytes,
#         backupCount=10
#     )
# 
#     if level is None: level = logging.INFO
#     file_handler.setLevel(level)
# 
#     log_format  = '%(asctime)s %(levelname)s: %(message)s'
#     log_format += ' [in %(pathname)s:%(lineno)d]'
#     file_handler.setFormatter(logging.Formatter(log_format))
# 
#     return file_handler
# 
# Path: boiler/log/mail.py
# def mail_logger(app, level = None):
#     """
#     Get mail logger
#     Returns configured instance of mail logger ready to be attached to app.
# 
#     Important: app.config['DEBUG'] must be False!
# 
#     :param app:         application instance
#     :param level:       mail errors of this level
#     :return:            SMTPHandler
#     """
#     credentials = None
#     if app.config['MAIL_USERNAME'] and app.config['MAIL_PASSWORD']:
#         credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
# 
#     secure = None
#     if app.config['MAIL_USE_TLS']:
#         secure = tuple()
# 
#     # @todo: move to configuration
#     config = dict(
#         mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
#         fromaddr=app.config['MAIL_DEFAULT_SENDER'],
#         toaddrs=app.config['ADMINS'],
#         credentials = credentials,
#         subject='Application exception',
#         secure = secure,
#         timeout=1.0
#     )
# 
#     mail_handler = SMTPHandler(**config)
# 
#     if level is None: level = logging.ERROR
#     mail_handler.setLevel(level)
# 
#     mail_log_format = '''
#     Message type:       %(levelname)s
#     Location:           %(pathname)s:%(lineno)d
#     Module:             %(module)s
#     Function:           %(funcName)s
#     Time:               %(asctime)s
# 
#     Message:
# 
#     %(message)s
#     '''
# 
#     mail_handler.setFormatter(logging.Formatter(mail_log_format))
#     return mail_handler


import logging
from boiler.log.file import file_logger
from boiler.log.mail import mail_logger


def logging_feature(app):
    """
    Add logging
    Accepts flask application and registers logging functionality within it
    """

    # this is important because otherwise only log warn, err and crit
    app.logger.setLevel(logging.INFO)

    # enable loggers
    email_exceptions = app.config.get('LOGGING_EMAIL_EXCEPTIONS_TO_ADMINS')
    if email_exceptions and not app.debug and not app.testing:
        # config.debug=False

# Path: boiler/log/file.py
# def file_logger(app, level=None):
#     """
#     Get file logger
#     Returns configured fire logger ready to be attached to app
# 
#     :param app:         application instance
#     :param level:       log this level
#     :return:            RotatingFileHandler
#     """
#     path = os.path.join(os.getcwd(), 'var', 'logs',  'app.log')
# 
#     max_bytes = 1024 * 1024 * 2
#     file_handler = RotatingFileHandler(
#         filename=path,
#         mode='a',
#         maxBytes=max_bytes,
#         backupCount=10
#     )
# 
#     if level is None: level = logging.INFO
#     file_handler.setLevel(level)
# 
#     log_format  = '%(asctime)s %(levelname)s: %(message)s'
#     log_format += ' [in %(pathname)s:%(lineno)d]'
#     file_handler.setFormatter(logging.Formatter(log_format))
# 
#     return file_handler
# 
# Path: boiler/log/mail.py
# def mail_logger(app, level = None):
#     """
#     Get mail logger
#     Returns configured instance of mail logger ready to be attached to app.
# 
#     Important: app.config['DEBUG'] must be False!
# 
#     :param app:         application instance
#     :param level:       mail errors of this level
#     :return:            SMTPHandler
#     """
#     credentials = None
#     if app.config['MAIL_USERNAME'] and app.config['MAIL_PASSWORD']:
#         credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
# 
#     secure = None
#     if app.config['MAIL_USE_TLS']:
#         secure = tuple()
# 
#     # @todo: move to configuration
#     config = dict(
#         mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
#         fromaddr=app.config['MAIL_DEFAULT_SENDER'],
#         toaddrs=app.config['ADMINS'],
#         credentials = credentials,
#         subject='Application exception',
#         secure = secure,
#         timeout=1.0
#     )
# 
#     mail_handler = SMTPHandler(**config)
# 
#     if level is None: level = logging.ERROR
#     mail_handler.setLevel(level)
# 
#     mail_log_format = '''
#     Message type:       %(levelname)s
#     Location:           %(pathname)s:%(lineno)d
#     Module:             %(module)s
#     Function:           %(funcName)s
#     Time:               %(asctime)s
# 
#     Message:
# 
#     %(message)s
#     '''
# 
#     mail_handler.setFormatter(logging.Formatter(mail_log_format))
#     return mail_handler


# Path: boiler/feature/logging.py
import logging
from boiler.log.file import file_logger
from boiler.log.mail import mail_logger



def logging_feature(app):
    """
    Add logging
    Accepts flask application and registers logging functionality within it
    """

    # this is important because otherwise only log warn, err and crit
    app.logger.setLevel(logging.INFO)

    # enable loggers
    email_exceptions = app.config.get('LOGGING_EMAIL_EXCEPTIONS_TO_ADMINS')
    if email_exceptions and not app.debug and not app.testing:
        # config.debug=False

        mail_handler = mail_logger(app)