Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python: Difference between dir() and help()
Next article icon

Difference between Logging and Print in Python

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, print and logging can be used for displaying information, but they serve different purposes. In this article, we will learn what is python logging with some examples and differences between logging and print in Python.

Logging in Python

Logging in Python is a technique to display useful messages and warnings to users. The logging module provides a flexible way to log different messages in various output destinations such as on the console, in files, and on networks. Logging is a best practice for production code. The logging module provides features such as log levels and filtering.

Here are the different levels of logging

  1. Debug: It is used to give detailed information while debugging the program.
  2. Info: It is used when we want to display some general information.
  3. Warning: It is used to display some warning which might affect the program in the future.
  4. Error: It is used to display some error message that has occurred in the program while executing.
  5. Critical: It is used to display the message when something critical has happened to the program that has stopped its execution.

Example 1: Printing the Log Message on the Console

In this example, we use logging to record a message to the console. We created a logger object and then passed the message as a warning. The basicConfig() method is used to configure the logging level, which is set to Info. This will simply print the log message on the console with the configuration information.

Python3
import logging  # creating the logger object logger = logging.getLogger()  # setting logger to a warning message logger.warning('This is a Warning message!')  # configuring the logger to info log levek logging.basicConfig(level=logging.INFO)  # setting the logger to a informative message logging.info("This is an Informative message.") 

Output:

This is a Warning message!  INFO:root:This is an Informative message.

Example 2: Printing the log Message in a File

In this example, we will see how we can modify the message according to our needs. Here, we added the time along with the log level and the message and saved it in a file named 'message.log'.

Python3
import logging  # configuring the logger to display log message # along with log level and time  logging.basicConfig(filename="message.log",                     format='%(asctime)s: %(levelname)s: %(message)s',                     level=logging.INFO)  # setting logger to critical message logging.critical("This is a Critical Message!!!") 

Output:

Log message saved in the message.log file
Log message saved in the message.log file

Print in Python

The print statement is a built-in function in Python that prints the specified value or values to the console. It is mainly used for debugging and is not recommended for logging information in production code.

Example:

The print statement will simply print the values to the console.

Python3
name = "Kumar" age = 21 print("My name is", name, "and I'm", age, "years old.") 

Output:

My name is Kumar and I'm 21 years old.

Difference between Logging and Print in Python

Logging in Python

Print in Python

Record events and errors that occur during the execution of Python programs.Displays the information to the console for the debugging purposes.
Mainly used in the production environment.Mainly for debugging.
Some features are: Log levels, filtering, formatting, and more.There are no good features.
It provides different log levels such as Debug, Info, Error, Warning, and Critical.It does not have any levels, it simply prints whatever is passed to it.

Example:

import logging; 
logging.basicConfig(level=logging.INFO); 
logging.info("Hello")

Output:

Can be configured to log to different output destinations (e.g. console, file, network)

Example:

print("Hello")

Output:

Prints only on the console


Next Article
Python: Difference between dir() and help()

S

subramanyasmgm
Improve
Article Tags :
  • Python
  • difference
Practice Tags :
  • python

Similar Reads

  • Difference between + and , Python Print
    In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
    3 min read
  • Difference between return and print in Python
    In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements. Return Statement
    2 min read
  • Difference between == and is operator in Python
    In Python, == and is operators are both used for comparison but they serve different purposes. The == operator checks for equality of values which means it evaluates whether the values of two objects are the same. On the other hand, is operator checks for identity, meaning it determines whether two
    4 min read
  • Difference between != and is not operator in Python
    In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If
    3 min read
  • Python: Difference between dir() and help()
    In Python, the dir() and help() functions help programmers understand objects and their functionality. dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.help() provides detailed information about an object, including descriptions of its meth
    5 min read
  • Difference between end and sep in Python
    In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed
    2 min read
  • Difference between Method and Function in Python
    Here, key differences between Method and Function in Python are explained. Java is also an OOP language, but there is no concept of Function in it. But Python has both concept of Method and Function. Python Method Method is called by its name, but it is associated to an object (dependent).A method d
    3 min read
  • Difference between Python and Groovy
    Python: It is general-purpose programming which supports both procedural and object-oriented programming concept. As well as it has some features of functional and reflective programming. It is a high-level programming language which is created by Guido van Rossum and first released on February 20,
    3 min read
  • Python - Difference between := and ==
    In this article, we will be discussing the major differences between Walrus(:=) and the Comparison operator (==) := in PythonThe := operator in Python, also known as the walrus operator or assignment expression, was introduced in Python 3.8. It enables assignment and evaluation to happen simultaneou
    2 min read
  • Difference between input() and raw_input() functions in Python
    Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input (
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences