logs.py 2.09 KB
#!/usr/bin/env python3

"""LOGGER

"""


# Standard library imports
import logging

# Third party imports
# None

# Local application imports
# None




logger = None
# Aliases for logger:
#def log_d(msg:str): logger.debug(msg)
def log_d(msg:str, *args, **kwargs): logger.debug(msg, *args, **kwargs)
def log_i(msg:str, *args, **kwargs): logger.info(msg, *args, **kwargs)
def log_w(msg:str, *args, **kwargs): logger.warning(msg, *args, **kwargs)
def log_e(msg:str, *args, **kwargs): logger.error(msg, *args, **kwargs)
def log_c(msg:str, *args, **kwargs): logger.critical(msg, *args, **kwargs)

    

#def set_logger(self):
def set_logger(DEBUG=False):
    global logger
    '''
    # Logger configuration
    # Log all events, starting from DEBUG level
    '''
    
    # Basic configuration
    '''
    #logging.basicConfig(level=logging.DEBUG)
    logging.basicConfig(level=logging.DEBUG, filename='client.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logging.debug("Client instanciated")
    '''
    
    # Advanced configuration
    logger = logging.getLogger(__name__)
    # Absolument necessaire, sinon rien ne marche !!!
    logger.setLevel(logging.DEBUG)
    
    # Create handlers for writing to console and file
    c_handler = logging.StreamHandler()
    f_handler = logging.FileHandler('client.log')
    
    # Set level for each handler
    # DEBUG < INFO < WARNING < ERROR < CRITICAL
    #if self.DEBUG:
    if DEBUG:
        c_handler.setLevel(logging.DEBUG)
        f_handler.setLevel(logging.DEBUG)
    else:
        c_handler.setLevel(logging.INFO)
        f_handler.setLevel(logging.INFO)
    
    # Set format for each handler
    #c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
    c_format = logging.Formatter('(%(name)s): %(message)s')
    f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    c_handler.setFormatter(c_format)
    f_handler.setFormatter(f_format)
    
    # Set handlers (for console and file)
    logger.addHandler(c_handler)
    logger.addHandler(f_handler)
    
    #return logger