Open In App

Difference between Logging and Print in Python

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads