import logging

LOGGING_MAP_IDE = {
    'critical': {'level': logging.CRITICAL, 'value': 50},
    'error': {'level': logging.ERROR, 'value': 40},
    'warning': {'level': logging.WARNING, 'value': 30},
    'info': {'level': logging.INFO, 'value': 20},
    'debug': {'level': logging.DEBUG, 'value': 10},
    'trace': {'level': 5, 'value': 5},
    'notset': {'level': logging.NOTSET, 'value': 0}
}

LOG_LEVELS = ['trace', 'debug', 'info', 'warn', 'error']	## these are the valid logging levels for use with Ignition system.util.setLoggingLevel() function

def getLoggerIDE(name='', level='info'):
    ## insure basic logging is set-up
    logging.basicConfig()
    ## Grab a logger object for the given name
    logger = logging.getLogger(name)
    if level and level in LOGGING_MAP_IDE: 
        level = LOGGING_MAP_IDE[level]['level']
        logger.setLevel(level)
    ## Check if any handler exists for this logger, if not, create a basic handler config.
    ## TODO: Add a functionality to customize handlers, particularly for writing to Ignition gateway console or system.perspective.print() etc
    # if not len(logger.handlers): logging.basicConfig()
    return(logger)

def getLogger(name='', level=None):
	## Grab a logger object for the given name from the Ignition system.util function
	logger = None
	if name not in ['', None]: logger = system.util.getLogger(name)
	## if logger created, level arg passed in and valid, set the logging level accordingly
	if logger and level and level in LOG_LEVELS: system.util.setLoggingLevel(name, level)
	return(logger)
