Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logging vs print
#1
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:
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.
Reply


Messages In This Thread
logging vs print - by syafiq14 - Aug-28-2021, 04:26 AM
RE: logging vs print - by bowlofred - Aug-28-2021, 06:19 AM
RE: logging vs print - by snippsat - Aug-28-2021, 10:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logging messages ahead of print messages vindo 6 3,452 Jun-18-2019, 02:45 PM
Last Post: vindo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020