Aug-28-2021, 04:26 AM
I'm learning to use logging in every project but, I'm starting to question the difference between logging.info vs print() they seem to have the same use. I've read the official documentation at https://docs.python.org/3/howto/logging....d-tutorial. but I still don't understand the difference. can anyone explain the difference with a real example?
I have a simple example of why I'm confused by the two functions:
I have a simple example of why I'm confused by the two functions:
import logging import random module_logger = logging.getLogger(__name__) ms_handler = logging.FileHandler("test_logging.log", mode="w") ms_handler.setLevel(logging.WARNING) ms_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s') ms_handler.setFormatter(ms_format) module_logger.addHandler(ms_handler) def rand_num(): return random.randint(1, 10) def user_input(ipt): user = int(input("> ")) while user != ipt: print("wrong number!") module_logger.warning("wrong number!") user = int(input("> ")) return user def play(): user_input() play()what I see the difference is that logging can log to a file or SMTP or something and have a format to make it clear what's going on in the code.