On top of our series on the ELK stack we have a small primer with Python to make our code a little more professional and give us something to work with as we test out our Logging stack and play with the various tools we have available to us within the Technology.
The Python logging library is a powerful tool for adding logging to your Python applications. With the logging library, you can log messages to a file, the console, or a remote logging server, and you can control the level of detail that is included in the logs. In this post, we'll look at how to use the logging library, with examples of code and pictures of the terminal.
On top of this we can make our Python code look more professional by removing the print() statements and giving our software a more formalised way of telling the user what is going on.
A very VERY basic Example
First, let's take a look at a simple example of using the logging library to log a message to the console:
import logging
logging.warning("This is a warning message")
When you run this code, you should see the following output in the console:
This is a warning message
Python logging example
As you can see, the logging library includes the log level (in this case, "WARNING") and the log message in the output. You can also specify a logger name, which will be included in the log output:
import logging
logger = logging.getLogger("logger")
logger.warning("This is a warning message")
This will produce the following output:
This is a warning message
Python logging example with logger name
In addition to logging to the console, you can also log messages to a file. To do this, you'll need to create a logger with a FileHandler:
import logging
logger = logging.getLogger("file_logger")
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler("log.txt")
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
handler.setFormatter(formatter)
add the file handler to the logger:
logger.addHandler(handler)
log some messages:
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
This code will log messages to the file "log.txt" at the specified log level (in this case, info and above). The log file will contain the log level, logger name, and log message, along with the timestamp.
In addition to logging to the console and to a file, you can also log messages to a remote logging server. To do this, you'll need to create a logger with a SocketHandler:
import logging
import socket
logger = logging.getLogger("remote_logger")
logger.setLevel(logging.INFO)
# create a socket handler
handler = logging.SocketHandler("localhost", 514)
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
# add the socket handler to the logger
logger.addHandler(handler)
# log some messages
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
Wrap Up!
There is a Practical example of this in the Python Tutorials Blog talked about previously that deals specifically with this subject, you can have a play for yourself in this Repository.
In conclusion, the Python logging library is a powerful tool for adding logging to your Python applications. Whether you're logging to the console, a file, or a remote logging server, the logging library makes it easy to control the level of detail included in your logs and to customize the log output. With the examples provided in this post, you should be able to get started with the logging library in your own projects.