pythonlogginginfo

logging in file using Python


I did this to save info to my file, but I also got output in my console. What can I do not to see info in my console, but to see it in my file?

logger = logging.getLogger('resource_manager')
logger.setLevel(logging.DEBUG)
info_log = logging.FileHandler('/home/nvidia/Videos/Info.log')
info_log.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
info_log.setLevel(logging.INFO)
logger.addHandler(info_log)

logger.info(f'{(time.time()-delay)}')

Solution

  • You can disable propagation of events from this logger to its parent (the root logger), which will prevent them from being handled by the root logger's StreamHandler (which is what writes to standard error).

    logger = logging.getLogger('resource_manager')
    logger.propagate = False