Steps

Step 1: Import the logging module in your Python file using the command:

import logging

Step 2: Define a logger object in the Python code using the following command:

logger = logging.getLogger(__name__)

Step 3: Define the format for the log message using the following command:

formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

Step 4: Create a file handler for the logs using the following command:

file_handler = logging.FileHandler('log_file.log')

Step 5: Set the format of the log message using the formatter object defined in step 3 using the following command:

file_handler.setFormatter(formatter)

Step 6: Add the file handler to the logger object created in step 2 using the following command:

logger.addHandler(file_handler)

Step 7: Define a decorator function

The decorator function should have the following structure:

def logger_decorator(func):
    def wrapper(*args, **kwargs):
        logger.info(f'{func.__name__} function is running')
        return func(*args, **kwargs)
    return wrapper

In this example, the logger is set to output an information message when the function is executed.

Step 8: Add the decorator to the function you want to log

by adding @logger_decorator before the function definition. For example:

@logger_decorator
def my_function():
    # function code here