Difference between Logging and Print in Python
Last Updated :
05 Jun, 2023
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
- Debug: It is used to give detailed information while debugging the program.
- Info: It is used when we want to display some general information.
- Warning: It is used to display some warning which might affect the program in the future.
- Error: It is used to display some error message that has occurred in the program while executing.
- 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
logger = logging.getLogger()
logger.warning( 'This is a Warning message!' )
logging.basicConfig(level = logging.INFO)
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
logging.basicConfig(filename = "message.log" ,
format = '%(asctime)s: %(levelname)s: %(message)s' ,
level = logging.INFO)
logging.critical( "This is a Critical Message!!!" )
|
Output:
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
|
Share your thoughts in the comments
Please Login to comment...