Logging to stdout in Python
-
Log to
stdoutWith thelogging.basicConfig()Function in Python -
Log to
stdoutWith thelogging.StreamHandler()Function in Python
This tutorial will introduce the methods to write logs to a file and stdout in Python.
Log to stdout With the logging.basicConfig() Function in Python
If we want to print our logs to the console window and write the logs inside a file, we can use the logging.basicConfig() function for this process. The logging.basicConfig() function configures some basic parameters for logging in Python. We can use the stream parameter of this function to specify that we also want this log to be printed on the console window. The rest of the configuration remains the same as writing a log to a file with the logging.basicConfig() function. The following code demonstrates how we can write logs to the console window.
import logging
import sys
#Creating and Configuring Logger
Log_Format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename = "logfile.log",
stream = sys.stdout,
filemode = "w",
format = Log_Format,
level = logging.ERROR)
logger = logging.getLogger()
#Testing our Logger
logger.error("Our First Error Message")
Output:
2021-06-13 05:15:24,093 - root - ERROR - Our First Error Message
Log to stdout With the logging.StreamHandler() Function in Python
We can also use the logging.StreamHandler() function to write logs to the console window in Python. By passing sys.stdout to the logging.StreamHandler() function, we can create a stream handler that can print the log message to the console window. We can then add this stream handler to our logger object with the addHandler() function. The following code snippet shows us how we can print log messages to the console window with the logging.StreamHandler() function.
import logging
import sys
logger = logging.getLogger()
fileHandler = logging.FileHandler("logfile.log")
streamHandler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
fileHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
logger.addHandler(fileHandler)
logger.error("This is the first error")
Output:
2021-06-13 05:03:37,510 - root - ERROR - This is the first error
We wrote the log message This is the first error to the file logfile.log and the console window with the logging.FileHandler() and logging.StreamHandler() functions in the code above. We first created a logger object that we will use to write logs with the logging.getLogger() function. Then, we created a file handler fileHandler and assigned logging.FileHandler('logfile.log') to it.
After that, we created a stream handler, streamHandler, and assigned logging.StreamHandler(sys.stdout). After that, we created a formatter to format our output with logging.Formatter() function and set the formatter for both fileHandler and streamHandler with the setFormatter() function. Following the previous step, we added both new handlers to our logger object with the addHandler() function. In the end, we wrote the error message to our file and the console window with logger.error('This is the first error').
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn