# Copyright 2017 The Forseti Security Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A basic util that wraps logging.

Setup logging for Forseti Security. Logs to console and syslog.
"""

import logging
import logging.handlers


LOG_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(funcName)s %(message)s'
LOGGERS = {}
LOGLEVELS = {
    'debug': logging.DEBUG,
    'info' : logging.INFO,
    'warning' : logging.WARN,
    'error' : logging.ERROR,
}
LOGLEVEL = logging.INFO


def get_logger(module_name):
    """Setup the logger.

    Args:
        module_name (str): The name of the mdule to describe the log entry.

    Returns:
        logger: An instance of the configured logger.
    """
    # TODO: Move this into a configuration file.
    formatter = logging.Formatter(LOG_FORMAT)
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(formatter)
    syslog_handler = logging.handlers.SysLogHandler()
    syslog_handler.setFormatter(formatter)
    logger_instance = logging.getLogger(module_name)
    logger_instance.addHandler(console_handler)
    logger_instance.addHandler(syslog_handler)
    logger_instance.setLevel(LOGLEVEL)
    LOGGERS[module_name] = logger_instance
    return logger_instance

def _map_logger(func):
    """Map function to current loggers.

    Args:
        func (function): Function to call on every logger.
    """
    for logger in LOGGERS.itervalues():
        func(logger)

def set_logger_level(level):
    """Modify log level of existing loggers as well as the default
       for new loggers.

    Args:
        level (int): The log level to set the loggers to.
    """
    # pylint: disable=global-statement
    global LOGLEVEL
    LOGLEVEL = level
    _map_logger(lambda logger: logger.setLevel(level))

def set_logger_level_from_config(level_name):
    """Set the logger level from a config value.

    Args:
        level_name (str): The log level name. The accepted values are
            in the LOGLEVELS variable.
    """
    set_logger_level(LOGLEVELS.get(level_name, LOGLEVEL))

@@ -758,21 +758,27 @@
 s%0A%0A%0A
+DEFAULT_
 LOG_F
-ORMA
+M
 T = 
+(
 '%25(a
@@ -814,16 +814,38 @@
 ame)-8s 
+'%0A                   '
 %25(funcNa
@@ -861,16 +861,131 @@
 ssage)s'
+)%0ASYSLOG_LOG_FMT = ('%5Bforseti-security%5D %25(name)-12s %25(levelname)-8s '%0A                  '%25(funcName)s %25(message)s')
 %0ALOGGERS
@@ -1416,54 +1416,8 @@
 le.%0A
-    formatter = logging.Formatter(LOG_FORMAT)%0A
     
@@ -1487,34 +1487,60 @@
 etFormatter(
-formatter)
+logging.Formatter(DEFAULT_LOG_FMT))%0A
 %0A    syslog_
@@ -1618,18 +1618,43 @@
 ter(
-formatter)
+logging.Formatter(SYSLOG_LOG_FMT))%0A
 %0A   

Add the log tag [forseti] to syslog entries (#609)