"""
deliverybot_libs/logging.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Common logging functionality
"""
import logging
from logging.handlers import RotatingFileHandler
import os

_formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')


def _get_log_file_path(log_name, log_settings):
    return os.path.join(log_settings['directory'], log_name)


def _get_handler(log_name, log_settings):
    handler = RotatingFileHandler(_get_log_file_path(log_name, log_settings),
                                  maxBytes=log_settings['size'],
                                  backupCount=log_settings['backup'])
    handler.setFormatter(_formatter)
    return handler


def setup_logger(service, log_name, log_settings):
    logger = logging.getLogger(log_name)
    logger.setLevel(log_settings['level'])
    logger.addHandler(_get_handler(log_name, log_settings))
