Python library for logging messages.
import logging
"""
Homepage manual: https://docs.python.org/2/howto/logging.html
Levels: When it’s used
DEBUG: Detailed information, typically of interest only when diagnosing problems.
INFO: Confirmation that things are working as expected.
WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR: Due to a more serious problem, the software has not been able to perform some function.
CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
"""
# logging into file log
logging.basicConfig(filename='example.log',level=logging.DEBUG) # level = minimum level to be displayedd.
logging.info('Start ...')
# logging by screen
logging.basicConfig(level=logging.INFO)
logging.info("Start ...")
# logging by screen with format
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
FORMAT = '%(levelname)s: %(message)s'
logging.basicConfig(
level=logging.INFO,
format=FORMAT,
)
# close logger
try:
logging.flust()
logging.close()
except:
pass
# get object (logger)
logger = logging.getLogger(__name__)
import logging
# config
logging.basicConfig(level=logging.INFO)
# get object (logger)
logger = logging.getLogger(__name__)
# use objecty
logger.info("Start ...")
# Reference: https://stackoverflow.com/questions/16947234/python-logging-across-multiple-modules
### MAIN SCRIPT: myapp.py ######################################################
import logging
import mylib
def main():
logging.basicConfig(filename='myapp.log', level=logging.INFO)
logging.info('Started')
mylib.do_something()
logging.info('Finished')
if __name__ == '__main__':
main()
### MODULE SCRIPT: mylib.py ####################################################
import logging
def do_something():
logging.info('Doing something from module')
### OUTPUT #####################################################################
"""
INFO:root:Started
INFO:root:Doing something from module
INFO:root:Finished
"""
"""
If it it necessary catch the logger in modules / submodules:
`logger = logging.getLogger(__name__)`
"""
import logging
def build_logger(log_name:str,
path_log_file:str,
min_level_console = logging.INFO,
min_level_file = logging.INFO,
FORMAT:str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')->logging.getLogger:
"""Build a logger to log in console and in a file.
Args:
log_name (str): Log name.
path_log_file (str): Path file where it will be written the log.
min_level_console (_type_, optional): Minimum status to be displayed by console. Defaults to logging.INFO.
min_level_file (_type_, optional): Minimum status to be displayed in a file. Defaults to logging.INFO.
FORMAT (str): Output format. Defaults to '%(asctime)s - %(name)s - %(levelname)s - %(message)s'.
Returns:
logging.getLogger: _description_
"""
# configure logger
logger = logging.getLogger(log_name)
logger.setLevel(logging.INFO)
logger.propagate = False
# console display handler
console_handler = logging.StreamHandler()
console_handler.setLevel(min_level_console)
# file display handler
file_handler = logging.FileHandler(path_log_file, mode='a', encoding='utf-8')
file_handler.setLevel(min_level_file)
# format output
formatter = logging.Formatter(FORMAT)
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# add handerls to the logger
if not logger.hasHandlers():
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# return
return logger