Design applications to be modular. It’s a software design best practice. Modular programming is writing multiple independent programs that perform a single function but work together to achieve an overarching outcome. The benefit to this design is the smaller parts can be easily created and tested. New functionality can be slipped into the larger whole without interfering with other functions.
Modular programming is the approach of dividing up a program into smaller parts, called functions. Developing a function in modular programming is a sub-program that performs a clear task within a program. These tasks are narrow in scope and can be reused over and over again throughout the entire application. Functions need to be clear and simple to reduce complexity and increase readability.
The Python programming language can follow modular programming. Python is so robust and popular, many different libraries can be imported to achieve different tasks. This flexibility is great for functionality and usability in our code, but can lead to bad design principles. Developers will expand the use case for a specific function when it was designed to only do one specific task. An example is adding logging to a Python program.
Logging is used in every step of an application to trace the state of the application. Python has a good and robust logging library that can be imported into a Python application. So, developers will add it to a function, breaking the software design best practice. The function now performs two tasks in one.
In addition to the software design best practice, logging in Python can be a challenge. Developing a Python script in development can produce hundreds if not thousands of logs. This number increases once the application moves into production.
Proper application development includes building robust error reporting that uses the five log levels to provide good output information from the application. However, if you put logging into a module, the logic for these five levels must be included to handle the different logging cases. These levels add complexity to your module and increase the risk of multiple breaking points.
An example of a simple function written in Python is shown below. This is to show the makeup of a function and to see it does one thing: print “Hello World” to the screen.
def my_function():
print(“Hello World”)
my_function()
The next example shows a function written in Python outputting “Hello World” but also printing a message based on the log level. Not the most complex function, but it shows how adding logging to a function can increase the syntax while increasing the chance of something breaking.
import logging
log = logging.getLogger(__name__)
def my_function():
Print(“Hello World”)
log.debug(‘Debug message’)
log.info(‘Info message’)
log.warning(‘Warning message’)
log.error(‘Error message’)
log.critical(‘Critical message’)
my_function()
Using a software-as-a-service like SolarWinds® Papertrail™ can help us reshape our code to reduce breaking points and get us back track of making functions do one thing really well. Papertrail lets developers consume the logging service instead of spending development time recreating the wheel. Developers can configure the Python application to use the robust logging library and send the output to Papertrail instead. They can use the tools built inside Papertrail to troubleshoot their application.
Papertrail offers a tail logging console to monitor logs as they come into the system live. A live tail for logs works in your browser so no additional applications or plugins are required. Logs can be paused while they come into the system to help look for issues or patterns across the environment.
Papertrail aggregates logs to make it easier to find an issue with an application. Having a central location for logs reduces the time to resolution because there’s one place to search for our logs instead of jumping around from system to system looking for the issue.
Python is a popular computer language that’s flexible and an easy platform to develop on because of the robust libraries it provides, like logging. Modules should perform one task well and log everything it does. Reduce the complexity of your modules and consume services like Papertrail for reusable steps throughout the application.
Develop your code using functions to make your Python application more modular. Use this style throughout the application to create robust logging functions to provide good detailed information about the application. Use existing libraries or use libraries provided by Papertrail without rewriting code. Use the built-in aggregator for a central point to collect all logs. Use the filter to search on specific log levels for a single app or multiple applications. Use Papertrail to unlock your super powers.