<jupyter_start><jupyter_text>Logging Module Report status, error, warning and informational messages![alt text](./img/timber.jpg "Title")  Benefits- allows all Python modules to use the same logger logic- 3rd party modules use the same logger logic as standard library  Parts of Logging Module1. Each module has a `Logger`2. Information is `add` to the `log`3. Invoking the logger creates a `LogRecord`4. A `Logger` can have multiple `handlers` which process the messages in different ways  Persistence of Log Messages - Goal: save messages to a file Simple Example<jupyter_code>import logging

LOG_FILENAME = './logging_file_example.txt'

logging.basicConfig(
    filename=LOG_FILENAME,
    level=logging.DEBUG,
)

logging.debug('This message should go to the log file')

with open(LOG_FILENAME, 'rt') as f:
    body = f.read()

print('FILE:')
print(body)<jupyter_output>FILE:
DEBUG:root:This message should go to the log file<jupyter_text>Rotating Log Files- allows for the rotation of files based on **SIZE**- good to use this when a script is run multiple times- `RotatingFileHandler` handles the creation of the rotating pattern Rotating Log File Example<jupyter_code>import glob, os
import logging
import logging.handlers

LOG_FILENAME = './logging_rotatingfile_example.txt'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyRotatingLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
    
    LOG_FILENAME,
    mode='w',
    maxBytes=20,
    backupCount=5
)
my_logger.addHandler(handler)

# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

# close the handlers
handlers = my_logger.handlers[:]
for handler in handlers:
    handler.close()
    my_logger.removeHandler(handler)
# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)
for filename in sorted(logfiles):
    print(filename)
    os.remove(filename)<jupyter_output>.\logging_rotatingfile_example.txt
.\logging_rotatingfile_example.txt.1
.\logging_rotatingfile_example.txt.2
.\logging_rotatingfile_example.txt.3
.\logging_rotatingfile_example.txt.4
.\logging_rotatingfile_example.txt.5
{'fraction_non_alphanumeric': 0.08889891696750903, 'fraction_numerical': 0.006768953068592058, 'mean_word_length': 5.09065934065934, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '538744', 'license': 'Apache-2.0', 'n_tokens_mistral': 686, 'n_tokens_neox': 670, 'n_words': 220, 'path': 'lecture/Lesson 04/Lesson 04 Part 01 - Logging .ipynb', 'repo_name': 'shaheen19/Adv_Py_Scripting_for_GIS_Course', 'size': '2216'}