Skip to content

Instantly share code, notes, and snippets.

@davydany
Created May 2, 2023 02:42
Show Gist options
  • Save davydany/3a010757c772e860907af5745ba0297b to your computer and use it in GitHub Desktop.
Save davydany/3a010757c772e860907af5745ba0297b to your computer and use it in GitHub Desktop.
A simple function that analyzes your logging configuration in Django and prints it out in human readable terms so your management can understand the logging policy of your application.
import os
def print_logging_params(logging_dict):
# Print handlers
if 'handlers' in logging_dict:
print('* Handlers:')
for name, handler in logging_dict['handlers'].items():
print(f" * {name.capitalize()} '{name}'")
if 'class' in handler:
print(f" * This is a handler called '{name}', which uses the '{handler['class']}' class.")
if 'filename' in handler:
print(f" * It logs the output to '{os.path.abspath(handler['filename'])}'.")
if 'maxBytes' in handler:
print(f" * It has a Maximum size of {handler['maxBytes']/(1024*1024):.2f}MB.")
if 'backupCount' in handler:
print(f" * It keeps up to {handler['backupCount']} backups.")
# Print filters
if 'filters' in logging_dict:
print('* Filters:')
for name, filter in logging_dict['filters'].items():
print(f" * {name.capitalize()} '{name}'")
print(f" * This is a filter called '{name}', which uses the '{filter['()']}' class.")
# Print formatters
if 'formatters' in logging_dict:
print('* Formatters:')
for name, formatter in logging_dict['formatters'].items():
print(f" * {name.capitalize()} '{name}'")
if 'format' in formatter:
print(f" * The format string is '{formatter['format']}'.")
# Print loggers
if 'loggers' in logging_dict:
print('* Loggers:')
for name, logger in logging_dict['loggers'].items():
print(f" * {name.capitalize()} '{name}'")
if 'handlers' in logger:
print(f" * It uses the handlers '{', '.join(logger['handlers'])}'.")
if 'filters' in logger:
print(f" * It uses the filters '{', '.join(logger['filters'])}'.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment