# -*- coding: utf-8 -*-
"""
    salt.log
    ~~~~~~~~

    This is were Salt's logging get's setup.


    :copyright: © 2011 :email:`Pedro Algarvio (pedro@algarvio.me)`
    :license: Apache 2.0, see LICENSE for more details.
"""

import logging

LOG_LEVELS = {
    "none": logging.NOTSET,
    "info": logging.INFO,
    "warn": logging.WARNING,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "none": logging.CRITICAL,
    "debug": logging.DEBUG,
    "trace": 5,
    "garbage": 1
}

LoggingLoggerClass = logging.getLoggerClass()


class Logging(LoggingLoggerClass):
    def garbage(self, msg, *args, **kwargs):
        return LoggingLoggerClass.log(self, 1, msg, *args, **kwargs)

    def trace(self, msg, *args, **kwargs):
        return LoggingLoggerClass.log(self, 5, msg, *args, **kwargs)


def setup_console_logger(log_level):
    """
    Setup the console logger
    """
    import logging
    if logging.getLoggerClass() is not Logging:
        logging.setLoggerClass(Logging)
        logging.addLevelName(5, 'TRACE')
        logging.addLevelName(1, 'GARBAGE')
        # Set the root logger at the lowest level possible
        logging.getLogger().setLevel(1)

    level = LOG_LEVELS.get(log_level.lower(), logging.ERROR)

    rootLogger = logging.getLogger()
    handler = logging.StreamHandler()

    handler.setLevel(level)
    formatter = logging.Formatter(
        '%(asctime)s,%(msecs)03.0f [%(name)-15s][%(levelname)-8s] %(message)s',
        datefmt="%H:%M:%S"
    )

    handler.setFormatter(formatter)
    rootLogger.addHandler(handler)


def setup_logfile_logger(log_path, log_level):
    """
    Setup the logfile logger
    """
    import logging
    if logging.getLoggerClass() is not Logging:
        logging.setLoggerClass(Logging)
        logging.addLevelName(5, 'TRACE')
        logging.addLevelName(1, 'GARBAGE')
        # Set the root logger at the lowest level possible
        logging.getLogger().setLevel(1)


    level = LOG_LEVELS.get(log_level.lower(), logging.ERROR)

    rootLogger = logging.getLogger()
    import logging.handlers
    handler = getattr(
        logging.handlers, 'WatchedFileHandler', logging.FileHandler)(
            log_path, 'a', 'utf-8', delay=0
    )

    handler.setLevel(level)
    formatter = logging.Formatter(
        '%(asctime)s [%(name)-15s][%(levelname)-8s] %(message)s',
    )

    handler.setFormatter(formatter)
    rootLogger.addHandler(handler)

def set_logger_level(logger_name, log_level):
    """
    Tweak a specific logger's logging level
    """
    import logging
    logging.getLogger(logger_name).setLevel(
        LOG_LEVELS.get(log_level.lower(), logging.ERROR)
    )

@@ -237,16 +237,40 @@
  logging
+%0Aimport logging.handlers
 %0A%0ALOG_LE
@@ -562,17 +562,16 @@
 lass()%0A%0A
-%0A
 class Lo
@@ -829,109 +829,205 @@
 s)%0A%0A
-%0A
 def 
-setup_console_logger(log_level
+getLogger(name):%0A    init()%0A    return logging.getLogger(name)%0A%0Adef init(
 ):%0A    
-%22%22%22
+'''
 %0A    
-Setup the console logger%0A    %22%22%22%0A    import logging
+Replace the default system logger with a version that includes trace()%0A    and garbage() methods.%0A    '''
 %0A   
@@ -1295,16 +1295,109 @@
 vel(1)%0A%0A
+def setup_console_logger(log_level):%0A    %22%22%22%0A    Setup the console logger%0A    %22%22%22%0A    init()%0A
     leve
@@ -1877,294 +1877,13 @@
    i
-mport logging%0A    if logging.getLoggerClass() is not Logging:%0A        logging.setLoggerClass(Logging)%0A        logging.addLevelName(5, 'TRACE')%0A        logging.addLevelName(1, 'GARBAGE')%0A        # Set the root logger at the lowest level possible%0A        logging.getLogger().setLevel(1)%0A%0A
+nit()
 %0A   
@@ -1982,36 +1982,8 @@
 r()%0A
-    import logging.handlers%0A
     
@@ -2441,29 +2441,21 @@
 %22%22%0A    i
-mport logging
+nit()
 %0A    log

Add getLogger() method to initialize and get logger without import ordering problems